본문 바로가기

C++24

[C++]가상함수(Virtual Function), 다형성(Polymorphism) class Base{ public: void BaseFunc() {cout 2023. 6. 9.
[C++] 객체 포인터의 참조관계 우리는 클래스를 기반으로도 포인터 변수를 선언할 수 있다. 예를 들어서 Person이라는 이름의 클래스가 정의되었다면, Person 객체의 주소 값 저장을 위해서 다음과 같이 포인터를 변수를 선언할 수 있다. Person *ptr;//포인터 변수 선언 ptr = new Person();//포인터 변수의 객체 참조 위의 두 문장이 실행되면, 포인터 Ptr은 Person 객체를 가리키게 된다. 그런데 Person형 포인터는 Person 객체뿐만 아니라, Person을 상속하는 유도 클래스의 객체도 가리킬 수 있다. class Student : public Person{ ... }; class PartTimeStudent : public Student{ ... }; Person *ptr = new Studen.. 2023. 6. 9.
[C++]상속(Inheritance)의 이해 상속을 하게 되면, 상속의 대상이 되는 클래스의 멤버까지도 객체 내에 포함이 된다. #include #include using namespace std; class Person{ private: int age; char name[50]; public: Person(int myage, char *myname) : age(myage){ strcpy(name, myname); } void WhatYourName() const{ cout 2023. 5. 14.
[C++]복사 생성자(Copy Constructor) C++에서는 다음과 같이 변수와 참조자를 선언 및 초기화할 수 있다. int num=10; int &ref=num; int num(20); int &ref(num); 위의 두 가지 초기화 방식은 결과적으로 동일하다. 따라서 객체 또한 아래와 같이 2가지 방법으로 선언할 수 있다. Sosimple sim1(15,20); Sosimple sim2=sim1; SoSimple sim2(sim1); Sosimple이라는 클래스가 있고 sim1이라는 객체를 선언 후 sim2에 sim1을 대입하였다. #include using namespace std; class SoSimple{ private: int num1; int num2; public: SoSimple(int n1, int n2) : num1(n1), nu.. 2023. 5. 5.
[C++]연산자 오버로딩의 이해와 유형 함수가 오버로딩 되면, 오버로딩 된 수만큼 다양한 기능을 제공하게 된다. 즉, 이름은 하나이지만 기능은 여러 가지가 되는 것이다. 마찬가지로 연산자의 오버로딩을 통해서, 기존에 존재하던 연산자의 기본 기능 이외에 다른 기능을 추가할 수 있다. #include using namespace std; class Point{ private: int xpos, ypos; public: Point(int x=0, int y=0) : xpos(x), ypos(y){} void ShowPosition() const{ cout 2023. 4. 30.
[C++]클래스와 함수에 대한 friend 선언 friend 선언은 private 멤버의 접근을 허용하는 선언이다. A 클래스가 B 클래스를 대상으로 friend 선언을 하면, B클래스는 A클래스의 private 멤버에 직접 접근이 가능하다. 단, A 클래스도 B 클래스의 private 멤버에 직접 접근이 가능하려면, B 클래스가 A 클래스를 대상으로 friend 선언을 해줘야 한다. #include #include using namespace std; class Girl; class Boy{ private: int height; friend class Girl; public: Boy(int len) : height(len){} void ShowYourFriendInfo(Girl &frn); }; class Girl{ private: char phNu.. 2023. 4. 30.