Studies/C++
-
10 클래스 생성을 통한 문제해결 -클래스의 생성자 및 소멸자에 대한 이해Studies/C++ 2022. 2. 4. 18:37
1. 개념 (1) const static 멤버와 mutable class Sosimple{ private: int num1; mutable int num2; public: SoSimple(int n1,int n2) :num1(n1),num2(n2) {} void CopyToNum2() const { num2=num1; } }; const 멤버함수는 변수를 바꿀 수 없는데 num2가 mutable이기 때문에 (예외적으로) const 멤버함수에서도 num2=num1이 가능하다. (2)클래스의 friend 선언 friend 선언은 private 멤버의 접근을 허용하는 선언이다. Class Boy{ private: int height; friend class Girl; public: Boy(int len):he..
-
09 클래스(3)-판매 실습,얕은복사Studies/C++ 2022. 2. 3. 18:13
1.Salesman 클래스 (1)Salesman.h #pragma once #include using namespace std; class Salesman { private: string name; double sales; public: Salesman(); ~Salesman(); Salesman(string name, double sales); void setName(string name); void readInput(); string getSalesmaninfo(); double getSales() { return sales; } }; (2)Salesman.cpp #include "Salesman.h" #include Salesman::Salesman() :name("noname"),sales(0.0..
-
08 클래스(2)-생성자와 소멸자Studies/C++ 2022. 1. 28. 18:39
1.Point 클래스 (1)Point.h #pragma once class Point { private: int x; int y; public: ~Point(); //소멸자 Point(); Point(const int& X, const int& Y); void init(const int& X, const int& Y); void setXY(const int& X, const int& Y); void move(const int& dist); void showPoint() const; int getX() const; int getY() const; }; (2)Point.cpp #include "Point.h" #include using namespace std; Point::~Point() { cout
-
07 클래스(1)Studies/C++ 2022. 1. 25. 19:18
1.램프 클래스 (1)Lamp.h #pragma once class Lamp { private: bool m_isOn; int m_illuminance; public: void init(); void powerOnOff(); void changeLight(); void showStatus(); }; (2)Lamp.cpp #include "Lamp.h" #include using namespace std; void Lamp::init() { m_isOn = false; m_illuminance = 1; } void Lamp::powerOnOff() { m_isOn = !m_isOn; } void Lamp::changeLight() { if (m_isOn) { if (m_illuminance < 3) m_i..
-
06 동적할당Studies/C++ 2022. 1. 24. 01:39
1.개념 int* p= new int[5]; int (*p)[4]=new int[3][4]; int (*p)[3][4]=new int[2][3][4]; #include #include using namespace std; int main() { int col, row; cin >> col >> row; int** parr = new int* [row]; for (int i = 0; i < row; i++) { parr[i] = new int[col]; for (int j = 0; j < col; j++) { parr[i][j] = j; } } for (int i = 0; i < row; i++) delete[] parr[i]; delete[] parr; return 0; } 2. 실습 1 파일로부터 동적..