상속
• 상속의 개념
• 파생 클래스 선언 방법
• 상속의 유형
상속 개념
기존에 정의된 클래스를 가져와 사용
상속과 포함
• 상속은 다른 클래스에서 정의된 멤버를 그대로 이어 받아 사용
• 포함은 다른 클래스를 자신의 멤버로 정의하여 사용하며,
• 다른 클래스를 멤버로 포함하고 있는 클래스를 컨테이너(container) 클래스
상속에 의한 클래스 인스턴스의
상호관계
파생클래스의 선언 방법
class Derived : public Base {
// 파생 클래스의 멤버 정의
}
프로그램 8-1 // 공용 상속
#include <iostream.h>
class Person // 베이스 클래스 {
public:
void GetData(int, char*) ; // 멤버함수 void ShowData();
private:
int itsAge;
char name[20];
};
void Person::GetData(int age, char *n) {
itAge = age;
strcpy(name, n);
}
void ShowData(){
cout << "age="<<itAge << endl;
cout << "name="<<name << endl;
}
class Student: public Person // 파생 클래스 {
public:
void idData(int);
void idShowData();
private:
int itsSid;
};
void Student::idData(int id){
itsSid = id;
}
void Student::idShowData(){
cout <<"Sid="<<itsSid << endl;
}
void main() {
Student s1;
s1.GetData(20, "홍길동“);
s1.ShowData();
s1.idData(20012);
s1.idShowData();
}
age=20
name=홍길동 Sid=20012
프로그램 8-2
//파일명볼링게임
#include<iostream.h>
#include<string.h>
#include<conio.h>
//회원 클래스 class Number { private:
char name[10];
char older[10];
public:
void Data(char *, char *);
void Display();
};
void Number::Data(char *n, char *o) {
strcpy(name, n);
strcpy(older, o);
}
void Number::Display() {
cout<<"이름: "<<name<<"\t"
<<"나이: "<<older<<endl;
}
//성적추가 파생클래스
class Game : public Number { private:
int itsA;
int itsB;
int itsC;
public:
void Data_Game(int, int, int);
void Display_Game();
};
void Game::Data_Game(int a, int b, int c) {
itsA = a;
itsB = b;
itsC = c;
}
void Game::Display_Game() {
cout<<"itsA : "<<itsA<<"\t"
<<"itsB : "<<itsB<<"\t"
<<"itsC : "<<itsC<<endl;
}
void main(void) {
Game Player1, Player2;
Player1.Data("최우대", "22");
Player2.Data("권오웅", "25");
Player1.Display();
Player2.Display();
cout<<"점수추가"<<endl;
Player1.Data_Game(106, 99, 70);
Player2.Data_Game( 90,100, 90);
Player1.Display();
Player1.Display_Game();
Player2.Display();
Player2.Display_Game();
}
이름 : 최우대 나이 : 22 이름 : 권오웅 나이 : 25 점수추가
이름 : 최우대 나이 : 22
itsA : 106 itsB : 99 itsC : 70 이름 : 권오웅 나이 : 25
itsA : 90 itsB :100 itsC : 90 실행결과
상속의 유형
베이스 클래스 멤버 유형
파생 클래스 접근제어자
public protected private
public ○ ○ ○
protected ○ ○ ×
private × × ×
보호 상속
보호 상속(Protected Inheritance)의 경우
베이스 클래스의 공용 및 보호 멤버는 파
생 클래스에서 보호 멤버로 취급된다. 따
라서 파생 클래스의 멤버함수는 베이스 클
래스의 멤버를 접근할 수 있지만 파생 클
래스의 객체는 베이스 클래스의 멤버를 접
근할 수 없게 된다.
프로그램 8-3
#include <iostream.h>
class AA // 베이스 클래스 {
public:
int GetAA( int a ){ //멤버함수 itsAA = a;
return itsAA;
}
void ShowAA() const {cout<<"AA 클래스!"<<endl;}
private:
int itsAA;
};
class BB : public AA // 공용 상속 {
public:
int GetBB(int b){ //멤버함수 itsBB = b;
return itsBB;
}
void Display() {cout<<"BB is derived from AA"<<endl;}
void ShowBB() const
{cout<<"BB: 보호상속 "<<endl;
ShowAA();}
private:
int itsBB;
};
class CC : protected BB // 보호 상속 {
public:
void ShowCC() const // 멤버 함수 {
cout<<"CC : 보호상속 "<<endl;
ShowAA();
} };
void main() {
BB bbb;
CC ccc;
// ① BB 자신
//--- cout<<bbb.GetBB(20)<<endl;
bbb.ShowBB();
bbb.Display();
//--- ccc.ShowCC(); // ② CC 자신
//---
// ③ BB 에서 AA 접근
//--- bbb.ShowAA();
cout<<bbb.GetAA(10)<<endl;
// ④ CC 에서 BB 접근불가
//--- // ccc.ShowBB();
// ccc.Display();
// ⑤ CC 에서 AA 접근불가
//--- // ccc.ShowAA();
// cout<<ccc.GetAA(10);
}
20
BB: 보호상속 AA 클래스!
BB is derived from AA CC : 보호상속
AA 클래스!
AA 클래스!
10
실행결과
#include <iostream.h> //8-4 class AA // 베이스 클래스 {
public:
int GetAA( int a ){ //멤버함수 itsAA = a;
return itsAA;
}
void ShowAA() const {cout<<"AA 클래스!"<<endl;}
private:
int itsAA;
};
class BB : protected AA // 보호 상속 {
public:
int GetBB(int b){ //멤버함수 itsBB = b;
return itsBB;
}
void Display() {cout<<"BB is derived from AA"<<endl;}
void ShowBB() const
{cout<<"BB: 보호상속 "<<endl;
ShowAA();}
private:
int itsBB;
};
class CC : protected BB // 보호 상속 {
public:
void ShowCC() const // 멤버 함수 {
cout<<"CC : 보호상속 "<<endl;
ShowAA();
} };
void main() {
BB bbb;
CC ccc;
// ① BB 자신
//--- cout<<bbb.GetBB(20)<<endl;
bbb.ShowBB();
bbb.Display();
//--- ccc.ShowCC(); // ② CC 자신
//---
// ③ BB 에서 AA 접근불가
//--- // bbb.ShowAA();
// cout<<bbb.GetAA(10)<<endl;
전용 상속
전용 상속(Private Inheritance)에서는 모
든 멤버가 파생 클래스에서 전용으로 취급
되므로, 이 파생 클래스에서 한번 더 파생
된 클래스는 최초의 베이스 클래스 멤버를
접근할 수 없게 된다.
// 전용상속의 선언
class AA 베이스클래스 { };
class BB: private AA {
// BB 클래스 멤버 ...
// AA 클래스 멤버에 접근 가능 ...
};
class CC: public BB // 공용 상속 {
// CC 클래스 멤버 ...
// BB 클래스 멤버 접근 가능 ...
// AA 클래스 멤버는 사용 불가 ...
};
프로그램 8-5
#include <iostream.h>
class AA // 베이스 클래스 {
public:
int GetAA( int a ){ //멤버함수 itsAA = a;
return itsAA;
}
void ShowAA() const {cout<<"AA 클래스!"<<endl;}
private:
int itsAA;
};
class BB : private AA // ① 전용 상속 {
public:
int GetBB(int b){ //멤버함수 itsBB = b;
return itsBB;
}
void Display() {cout<<"BB is derived from AA"<<endl;}
void ShowBB() const
{cout<<"BB: 보호상속 "<<endl;
ShowAA();} // ③ AA 멤버 접근 private:
int itsBB;
};
class CC : public BB // ② 공용 상속 또는 보호 상속 {
public:
void ShowCC() const // 멤버 함수 {
cout<<"CC : 보호상속 "<<endl;
// ShowAA(); //④ AA 멤버에 접근불가 }
};
void main() {
BB bbb;
CC ccc;
cout<<bbb.GetBB(20)<<endl;
bbb.ShowBB();
bbb.Display();
ccc.ShowCC();
}
20
BB: 보호상속 AA 클래스!
BB is derived from AA CC : 보호상속