• 검색 결과가 없습니다.

상속

N/A
N/A
Protected

Academic year: 2022

Share "상속"

Copied!
29
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

상속

상속의 개념

파생 클래스 선언 방법

상속의 유형

(2)

상속 개념

기존에 정의된 클래스를 가져와 사용

상속과 포함

상속은 다른 클래스에서 정의된 멤버를 그대로 이어 받아 사용

• 포함은 다른 클래스를 자신의 멤버로 정의하여 사용하며,

• 다른 클래스를 멤버로 포함하고 있는 클래스를 컨테이너(container) 클래스

(3)

상속에 의한 클래스 인스턴스의

상호관계

(4)

파생클래스의 선언 방법

class Derived : public Base {

// 파생 클래스의 멤버 정의

}

(5)

프로그램 8-1 // 공용 상속

#include <iostream.h>

class Person // 베이스 클래스 {

public:

void GetData(int, char*) ; // 멤버함수 void ShowData();

private:

int itsAge;

char name[20];

};

(6)

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;

};

(7)

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)

프로그램 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();

};

(9)

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();

};

(10)

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;

}

(11)

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();

}

(12)

이름 : 최우대 나이 : 22 이름 : 권오웅 나이 : 25 점수추가

이름 : 최우대 나이 : 22

itsA : 106 itsB : 99 itsC : 70 이름 : 권오웅 나이 : 25

itsA : 90 itsB :100 itsC : 90 실행결과

(13)

상속의 유형

베이스 클래스 멤버 유형

파생 클래스 접근제어자

public protected private

public

protected ×

private × × ×

(14)

보호 상속

보호 상속(Protected Inheritance)의 경우

베이스 클래스의 공용 및 보호 멤버는 파

생 클래스에서 보호 멤버로 취급된다. 따

라서 파생 클래스의 멤버함수는 베이스 클

래스의 멤버를 접근할 수 있지만 파생 클

래스의 객체는 베이스 클래스의 멤버를 접

근할 수 없게 된다.

(15)

프로그램 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;

};

(16)

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;

};

(17)

class CC : protected BB // 보호 상속 {

public:

void ShowCC() const // 멤버 함수 {

cout<<"CC : 보호상속 "<<endl;

ShowAA();

} };

void main() {

BB bbb;

CC ccc;

(18)

// ① BB 자신

//--- cout<<bbb.GetBB(20)<<endl;

bbb.ShowBB();

bbb.Display();

//--- ccc.ShowCC(); // ② CC 자신

//---

// ③ BB 에서 AA 접근

//--- bbb.ShowAA();

cout<<bbb.GetAA(10)<<endl;

(19)

// ④ CC 에서 BB 접근불가

//--- // ccc.ShowBB();

// ccc.Display();

// ⑤ CC 에서 AA 접근불가

//--- // ccc.ShowAA();

// cout<<ccc.GetAA(10);

}

(20)

20

BB: 보호상속 AA 클래스!

BB is derived from AA CC : 보호상속

AA 클래스!

AA 클래스!

10

실행결과

(21)

#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;

};

(22)

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;

};

(23)

class CC : protected BB // 보호 상속 {

public:

void ShowCC() const // 멤버 함수 {

cout<<"CC : 보호상속 "<<endl;

ShowAA();

} };

void main() {

BB bbb;

CC ccc;

(24)

// ① BB 자신

//--- cout<<bbb.GetBB(20)<<endl;

bbb.ShowBB();

bbb.Display();

//--- ccc.ShowCC(); // ② CC 자신

//---

// ③ BB 에서 AA 접근불가

//--- // bbb.ShowAA();

// cout<<bbb.GetAA(10)<<endl;

(25)

전용 상속

전용 상속(Private Inheritance)에서는 모

든 멤버가 파생 클래스에서 전용으로 취급

되므로, 이 파생 클래스에서 한번 더 파생

된 클래스는 최초의 베이스 클래스 멤버를

접근할 수 없게 된다.

(26)

// 전용상속의 선언

class AA 베이스클래스 { };

class BB: private AA {

// BB 클래스 멤버 ...

// AA 클래스 멤버에 접근 가능 ...

};

class CC: public BB // 공용 상속 {

// CC 클래스 멤버 ...

// BB 클래스 멤버 접근 가능 ...

// AA 클래스 멤버는 사용 불가 ...

};

(27)

프로그램 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 // ① 전용 상속 {

(28)

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 // ② 공용 상속 또는 보호 상속 {

(29)

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 : 보호상속

참조

관련 문서

상속 세는 상속 개시일 이후 6 개월 내에 현금으로 납부해야 하는데 갑작스럽게 상속 이 개시되면 상속세를 낼 현금을 만들기 위해 상속재산의 일부를 팔아야 할

base 클래스의 접근 제어와 protected 멤버 상속 관계에서의 생성자와 소멸자.. 함수 재정의(function overriding) 디폴트 액세스 지정자와

서로 다른 클래스 객체들 사이의 대입 상속 관계인 객체와 포인터의 관계 가상 함수.. 가상

 IANA(Internet Assigned Numbers Athority)...

• 애플리케이션의 여러 자원들과 메인 프레임 클래스, View 클래스, Document 클래스 등을 하나의 묶 음으로 가지는

물론, 상속순위가 앞에 있는 사람이 상속 포기를 하지 않은 경우에도 상속순위가 뒤에 있는 사람은 먼저

• 한 클래스를 처음부터 새로 만들려고 하면 필요한 변수들과 메소드들을 전부 다 새로 만들어야 한다.. • 그러나 상속을 이용하여 기존 클래스로부터 새

 클래스는 필드와 메소드로