Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

브래의 슬기로운 코딩 생활

C++프로그래밍 13주차 정리 본문

1-2/C++프로그래밍

C++프로그래밍 13주차 정리

김브래 2022. 11. 24. 18:19

오늘은 클래스 상속에 대해 배웠다.

 

상속은 코드를 재사용하기 위하여 사용한다.

언어별 클래스 상속 형식

대부분 public을 사용한다.

 

상속을 하면 생성자와 소멸자는

생성자는 부모 클래스 먼저 호출이 되고

소멸자는 자식 클래스 먼저 호출이 된다.

이거 아주 중요하다.

 

 

잘 안 쓴다.

과재:

 

#include <iostream>

using std::cout;

using std::endl;

using std::string;

class Man {

protected:

string name;

int age;

public:

Man(string name, int age) {

this->name = name;

this->age = age;

}

void show() {

cout << "이름 : " << name << endl;

cout << "나이 : " << age << endl;

}

};

class Student : public Man {

protected:

string ban;

int haknum;

public:

Student(string name, int age, string ban, int haknum) : Man(name,age) {

this->ban = ban;

this->haknum = haknum;

}

void s_show() {

show();

cout << ": " << ban << endl;

cout << "학번 : " << haknum << endl;

}

};

class Teacher :public Man {

protected:

string pro;

string sub;

public:

Teacher(string name, int age, string pro, string sub):Man(name,age) {

this->pro = pro;

this->sub = sub;

}

void t_show() {

show();

cout << "전공 : " << pro << endl;

cout << "담당과목 : " << sub << endl;

}

};

int main()

{

Student kks("김컴소", 20, "C", 202012000);

Teacher hms("한미소", 40, "전산", "C++프로그래밍");

Student brae("김동현", 22, "A", 202114013);

Teacher hsh("한성현", 40, "전산", "C++프로그래밍");

 

kks.s_show();

hms.t_show();

brae.s_show();

hsh.t_show();

return 0;

}