본문 바로가기
언어 공부/C++

클래스 간의 관계: 상속 - 2(person-student)

by 세희홍 2022. 6. 23.

# person.h

#ifndef PERSON_H
#define PERSON_H
#include <cassert>
#include <iostream>
#include <iomanip>
using namespace std;

class Person
{
private:
    long identity;
public:
    Person();
    Person(long identity);
    Person(const Person& person);
    ~Person();

    void print() const;
};
#endif

 

# student.h

#ifndef STUDENT_H
#define STUDENT_H
#include "person.h"

class Student : public Person
{
private:
    double gpa;
public:
    Student();
    Student(long identity, double gpa);
    Student(const Student& student);
    ~Student();
    
    void print() const;
};
#endif

 

# person.cpp

#include <cassert>
#include "person.h"

// 기본 생성자
Person::Person()
	: identity(0)
{
	cout << this << "부모 클래스 기본 생성자\n";
}
// 매개변수가 있는 생성자  
Person::Person(long id)
	: identity(id)
{
	cout << this << "부모 클래스 매개변수 생성자\n";
	assert(identity >= 100000000 && identity <= 999999999);
}
// 복사 생성자
Person::Person(const Person& person)
	: identity(person.identity)
{
	cout << this << "부모 클래스 복사 생성자\n";
}
// 소멸자      
Person::~Person()
{
	cout << this << "부모 클래스 소멸\n";
}
// 접근자 멤버 함수
void Person::print() const
{
	cout << "Identity: " << identity << endl;
}

 

# student.cpp

#include <cassert>
#include "student.h"

// 기본 생성자
Student::Student()
	: Person(), gpa(0.0)
{
	cout << this << "자식 클래스 기본 생성자\n";
}
// 매개변수가 있는 생성자  
Student::Student(long id, double gp)
	: Person(id), gpa(gp)
{
	cout << this << "자식 클래스 매개변수 생성자\n";
	assert(gpa >= 0.0 && gpa <= 4.0);
}
// 복사 생성자 
Student::Student(const Student& student)
	: Person(student), gpa(student.gpa)
{
	cout << this << "자식 클래스 복사 생성자\n";
}
// 소멸자
Student::~Student()
{
	cout << this << "자식 클래스 소멸\n";

}
// 접근자 멤버 함수
void Student::print() const
{
	Person::print(); // delegation. 부모의 print. 
	cout << "GPA: " << fixed << setprecision(2) << gpa << endl;
}

 

# main.cpp

#include "student.h"

int main()
{
	Student student3;
	cout << "Person 객체의 정보: " << endl;
	student3.print();
	cout << endl;

	Person person(111111111); // Person::Person(long id) : identity(id)
	cout << "Person 객체의 정보: " << endl;
	person.print();
	cout << endl;

	Student student1(222222222, 3.9); //Student::Student(long id, double gp) : Person(id), gpa(gp)
	cout << "Student 객체의 정보: " << endl;
	student1.print();
	cout << endl;

	Student student2(student1); // Student::Student(const Student& student) : Person(student), gpa(student.gpa)
	cout << "Student 객체의 정보: " << endl;
	student2.print();
	cout << endl;

	return 0;
}

0000007576EFF7D8부모 클래스 기본 생성자
0000007576EFF7D8자식 클래스 기본 생성자
Person 객체의 정보:
Identity: 0
GPA: 0.00

0000007576EFF804부모 클래스 매개변수 생성자
Person 객체의 정보:
Identity: 111111111

0000007576EFF828부모 클래스 매개변수 생성자
0000007576EFF828자식 클래스 매개변수 생성자
Student 객체의 정보:
Identity: 222222222
GPA: 3.90

0000007576EFF858부모 클래스 복사 생성자
0000007576EFF858자식 클래스 복사 생성자
Student 객체의 정보:
Identity: 222222222
GPA: 3.90

0000007576EFF858자식 클래스 소멸
0000007576EFF858부모 클래스 소멸
0000007576EFF828자식 클래스 소멸
0000007576EFF828부모 클래스 소멸
0000007576EFF804부모 클래스 소멸
0000007576EFF7D8자식 클래스 소멸
0000007576EFF7D8부모 클래스 소멸

 

'언어 공부 > C++' 카테고리의 다른 글

클래스 간의 관계: 연관  (0) 2022.06.26
클래스 간의 관계: 상속 - 3(person-student-employee)  (0) 2022.06.26
클래스 간의 관계: 상속 - 1  (0) 2022.06.22
string-3  (0) 2022.06.22
string-2  (0) 2022.06.22

댓글