C++
-
[C++] static_member.cppC++/clone code 2022. 6. 30. 10:34
인스턴스 변수 1. 객체가 생성될 때 각각의 객체들은 자신만의 멤버 변수들을 가진다. -> 인스턴스 변수 2. 인스턴스 변수들은 별도의 기억 공간을 가지고 있다. 정적 멤버 변수 static 자료형 변수 이름; 1. 정적 변수는 모든 객체에 공통인 변수가 필요할 때 사용한다. 2. 정적 변수의 실제 정의는 반드시 클래스 외부에서 해야한다. 3. 정적 변수는 객체를 만들지 않고서도 접근될 수 있다. Car :: numberOfCars = 100; c2.numberOfCars = 100; 정적 멤버 함수 1. 인스턴스 변수 사용(X), 정적 변수와 지역 변수만 사용할 수 있다. 2. 정적 멤버 함수에서 정적 멤버 함수를 호출하는 것은 가능하다. 3. this 포인터를 사용할 수 없다. car.cpp 예제 #..
-
[C++] 객체 생성과 메세징하는 다양한 방법C++/class 2022. 6. 29. 19:38
#include #include using namespace std; // Step 1. 클래스 생성 class Car { private: int speed; int gear; string color; public: //초기화 리스트 Car(int s = 0, int g = 1, string c = "white") : speed(s), gear(g), color(c) {} void display(); }; void Car::display() { cout
-
[C++] student.cpp (복사 생성자의 개념)C++/clone code 2022. 6. 29. 00:07
복사 생성자 다른 객체의 내용을 복사해서 새로운 객체를 초기화하는 것 복사 생성자는 자신과 같은 타입의 객체를 매개 변수로 받음 얕은 복사 복사 생성자가 정의되어 있지 않은 경우 컴파일러가 알아서 디폴트 복사 생성자를 추가해줌 Car c1(0, 1, "grey"); Car c2(c1); 얕은 복사의 문제점 #include #include using namespace std; class Student { private: char* name;// 이름 int number;// 학번 public: //생성자 Student(const char* pn, int n) {//const 빠지면 컴파일 오류! name = new char[strlen(pn) + 1];//+1은 null문자 포함한 크기 strcpy(nam..
-
[C++] 생성자 안에서의 동적 메모리 할당C++/class 2022. 6. 28. 23:30
프로그램이 메모리를 할당받는 방법 1. 정적 메모리 할당 - 프로그램이 시작되기 전에 미리 정해진 크기의 메모리를 할당받는 것 - 메모리의 크기는 프로그램이 시작하기 전에 결정되며, 프로그램의 수행 도중에 그 크기가 변경될 수 없음 - ex) int i, j; int buffer[80]; char name [] = "data structure"; 2. 동적 메모리 할당 - 프로그램이 실행 도중에 동적으로 메모리를 할당받는 것 - 프로그램에서 필요한 만큼 메모리를 할당받아서 사용하고, 사용이 끝나면 시스템에 메모리를 반납함 동적 메모리 할당 방법 1. 동적 메모리 할당 - new 키워드를 이용해서 동적으로 메모리를 할당함 - new가 반환하는 것은 할당된 메모리의 주소 - 반환값은 포인터에 저장해야 함 e..
-
[C++] market.cpp (객체와 클래스에 대한 이해)C++/clone code 2022. 6. 28. 19:39
Power C++ p.387 [목표] 사용자로부터 2개의 상품에 대한 정보를 입력받고, 더 저렴한 상품을 비교하여 출력한다. #include #include #include using namespace std; class Product { private: int id; string name; int price; public: void input(); void print(); bool isCheaper(Product other); }; void Product::input() { cout
-
[C++] 계좌 잔액 조회하기.cpp (객체와 클래스에 대한 이해)C++/clone code 2022. 6. 28. 18:04
Power C++ p.390 [목표] 입금, 출금 등의 동작을 하는 클래스 작성 #include #include using namespace std; //step 1. 클래스 생성 class BankAccount{ private: string name; // 예금주 int account; // 계좌 int balance; // 잔액 public: //getter string getName(); int getAccount(); int getBalance(); //setter void setName(string n); void setAccount(int g); void setBalance(int s); //계산 void deposit(int money); void withdraw(int money); //출..
-
[C++] desk_lamp.cpp (객체와 클래스에 대한 이해)C++/clone code 2022. 6. 28. 17:50
Power C++ p.387 [목표] 책상에 있는 램프의 상태를 출력한다. #include #include using namespace std; class DeskLamp { private: bool isOn; public: void turnOn(); void turnOff(); void print(); }; void DeskLamp::turnOn() { isOn = true; } void DeskLamp::turnOff() { isOn = false; } void DeskLamp::print() { if (isOn == true) cout
-
[C++] 클래스에 대한 이해(Car.cpp)C++/class 2022. 6. 24. 10:00
Key Point 1. 클래스란 서로 관련이 있는 변수와 함수들을 한 곳으로 모은 것이다. 2. 객체가 생성될 때 멤버 변수는 별도로 생성되고, 멤버 함수는 공유된다. 3. 멤버 변수와 멤버 함수에 접근하기 위해 .(dot operator)를 사용한다. (점 앞의 객체에게 점 뒤에 있는 것을 가져다 달라는 의미) #include #include using namespace std; // ========== ========== class Car{ // 멤버 변수 public: int speed; int gear; string color; // 멤버 함수 void speedUp(){ speed += 10; } void sppedDown(){ speed -= 10; } }; int main(){ // ====..