#include <iostream>
using namespace std;

// 상속?
/// > 부모가 가진 정보를 자식에게 물려주는 것
// 사용이유?
/// > 재활용성
/// > 생산성
/// > 효율성

// protected
/// > 기본적으로 private이지만, 자식클래스에게만 public
///  (클래스 외부에서는 접근이 안되지만 자식클래스는 접근 가능함)

// 부모클래스 : 기초클래스, 상위, 슈퍼, 기반
// 자식클래스 : 유도클래스, 하위, 서브, 파생

class SampleBase	// 기본생성자 생략되어 있음
{
private:
	int pri_Value;

protected:
	int pro_Value;

public:
	int pub_Value;
};

class SampleChild : public SampleBase	// 상속 (자식이라 해도 private은 안됨) 
{										// 상속은 컴파일에서 호출을 했을 뿐 생성자는 상속되지 않음
public:
	void Method()
	{
		//pri_Value = 20;
		pro_Value = 30;
		pub_Value = 10;
	}
};

int main()
{
	SampleChild* child = new SampleChild();
	child->pub_Value = 10;


	return 0;
}

 


 

#include <iostream>
using namespace std;

// Human - Student

enum Gender
{
	Man, Woman
};

class Human
{
private:  /// 우선 private로 만들고 자식클래스에서 접근할 일이 생기면 그것만 protected로 따로 뺌
	const char* name;
	int age;
	Gender gender;

public:	//접근제한자 private니깐 getter setter 만들어 줌
	void SetHumanInfo(const char* name, int age, Gender gender)	
    /// 이름,나이,성별은 바뀔 가능성이 낮기때문에 다 같이 씀
	{
		this->name = name;
		this->age = age;
		this->gender = gender;
	}

	void PrintHumanInfo()
	{
		cout << "이름 : " << name << endl;
		cout << "나이 : " << age << endl;
		cout << "성별 : " << ((gender == Gender::Man) ? "남자" : "여자") << endl;
	}

};

class Student : public Human  /// 부모에 있는 멤버를 자식을 통해서 접근할 수 있느냐를
			/// 부모클래스 앞 접근제한자를 통해 정한다. private Human은 자식이 접근x 보통은 public
{
private:
	int score;

public:
	void SetStudentInfo(const char* name,int age,Gender gender,int score)
	{
		SetHumanInfo(name, age, gender); // 호출부
		this->score = score;
	}
	
	void PrintStudentInfo()
	{
		PrintHumanInfo();
		cout << "C++성적은 : " << score << endl;
	}
};

int main()
{
	//Human* student = new Student();  
    ///student가 사용하고 있는 메모리는 Student의 메모리임. 
    ///그러나 Human이 자료형이므로 Student 안에 Human밖에 못 씀. 자식클래스 멤버를 못 씀.
	
    Student* student = new Student();
    student->SetStudentInfo("김방방", 2, Gender::Man, 100);
    student->PrintStudentInfo();
    
    //클래스 배열 만들 때(?) (배열의 요소가 클래스형식, 클래스는 참조형식, 요소 자체가 포인터)
    //Student** student = new Student*[10];
    //for (int i = 0; i < 10; i++)
    //{
    //	student[i] = new Student(); 
        ///student[i]요소가 Student*이 형식이고, new Student() 이 동적메모리를 참조함
    //}
    //for (int i = 0; i < 10; i++) 
    //{ 
    //  delete student[i]; 
    //  student[i] = nullptr;
    //}
    //delete[] student;
    //student = nullptr;



	delete student;
	student = nullptr;
    
    
	return 0;
}

 


다중상속 : 여러 클래스를 상속받음(c++만 가능)

#include <iostream>
using namespace std;

class A
{
public:
	A() { cout << "A" << endl; }
	~A() { cout << "~A" << endl; }
};

class B
{
public:
	B() { cout << "B" << endl; }
	~B() { cout << "~B" << endl; }
};

class C : public A,public B  // 다중상속
{
public:
	C() { cout << "C" << endl; }
	~C() { cout << "~C" << endl; }
};

int main()
{
	C* c = new C();
    // 생성자는 최상위부터 내려옴 A B C
    // 소멸자는 자신부터 위로 차례차례 올라감 ~C ~B ~A

	delete c;
	c = nullptr;


	return 0;
}

 


 

중첩상속

#include <iostream>
using namespace std;

class A
{
public:
	A() { cout << "A" << endl; }
	~A() { cout << "~A" << endl; }
};

class B : public A
{
public:
	B() { cout << "B" << endl; }
	~B() { cout << "~B" << endl; }
};

class C : public B // 중첩상속 (C는 A,B 다 갖음)
{
public:
	C() { cout << "C" << endl; }
	~C() { cout << "~C" << endl; }
};

int main()
{
	C* c = new C();

	delete c;
	c = nullptr;

	return 0;
}

 

+ Recent posts