• 코드:
​x
 
1
#include <iostream>
2
using namespace std;
3
​
4
class Person
5
{
6
private:
7
    string name_;
8
    int age_;
9
public:
10
    Person(const string& name, int age);    // 기초 클래스 생성자의 선언 
11
    void ShowPersonInfo();
12
};
13
​
14
class Student : public Person
15
{
16
private:
17
    int student_id_;
18
public:
19
    Student(int sid, const string& name, int age);  // 파생 클래스 생성자의 선언 
20
};
21
​
22
int main(void)
23
{
24
    Student hong(123456789, "길동", 29);
25
    hong.ShowPersonInfo();
26
    
27
    return 0;
28
}
29
​
30
Person::Person(const string& name, int age) // 기초 클래스 생성자의 정의 
31
{
32
    name_ = name;
33
    age_ = age;
34
}
35
​
36
void Person::ShowPersonInfo()
37
{
38
    cout << name_ << "의 나이는 " << age_ << "살입니다." << endl;
39
}
40
​
41
Student::Student(int sid, const string& name, int age) : Person(name, age)  // 파생 클래스 생성자의 정의 
42
{
43
    student_id_ = sid;
44
}
표준입력 & 실행옵션