C++接口与实现的抽象分离

时间:2022-10-27 13:23:55

IPerson.h

 1 #ifndef I_PERSON_H_
 2 #define I_PERSON_H_
 3 
 4 #include <string>
 5 #include <ostream>
6 class IPerson 7 { 8 public: 9 virtual std::string GetName() const = 0; 10 virtual int GetAge() const = 0; 11 virtual std::string GetClassName() const = 0; 12 }; 13 14 std::ostream& operator<<(std::ostream &os, const IPerson &person); 15 16 #endif

 

Person.h

 1 #ifndef PERSON_H_
 2 #define PERSON_H_
 3 
 4 #include "IPerson.h"
 5 
 6 class Person : virtual public IPerson
 7 {
 8 public:
 9     Person(const std::string &name, const int age);
10     virtual ~Person();
11     std::string GetName() const override;
12     int GetAge() const override;
13     std::string GetClassName() const override;
14 private:
15     std::string name;
16     int age;
17 };
18 
19 #endif

 

Person.cpp

 1 #include "Person.h"
 2 
 3 Person::Person(const std::string &name, const int age) :
 4     name(name), 
 5     age(age)
 6 {
 7 }
 8 
 9 Person::~Person()
10 {
11 }
12 
13 std::string Person::GetName() const
14 {
15     return name;
16 }
17 
18 int Person::GetAge() const
19 {
20     return age;
21 }
22 
23 std::string Person::GetClassName() const
24 {
25     return std::string("Person");
26 }
27 
28 std::ostream& operator<<(std::ostream &os, const IPerson &person)
29 {
30     os << "Name: " << person.GetName() << ", "
31         << "Age: " << person.GetAge() << ", ";
32 
33     return os;
34 }

 

IStudent.h

 1 #ifndef I_STUDENT_H_
 2 #define I_STUDENT_H_
 3 
 4 #include "IPerson.h"
 5 
 7 
 8 class IStudent : virtual public IPerson
 9 {
10 public:
11     virtual int GetGrade() const = 0;
12 };
13 
14 std::ostream& operator<<(std::ostream &os, const IStudent &student);
15 
16 #endif

 

Student.h

 1 #ifndef STUDENT_H_
 2 #define STUDENT_H_
 3 
 4 #include "IStudent.h"
 5 #include "Person.h"
 6 
 7 class Student : virtual public IStudent, public Person
 8 {
 9 public:
10     Student(const std::string &name, const int age, const int grade);
11     ~Student();
12 
13     int GetGrade() const override;
14     std::string GetClassName() const override;
15 private:
16     int grade;
17 };
18 
19 #endif

 

Student.cpp

 1 #include "Student.h"
 2 
 3 Student::Student(const std::string &name, const int age, const int grade) :
 4     Person(name, age),
 5     grade(grade)
 6 {
 7 }
 8 
 9 Student::~Student()
10 {
11 }
12 
13 int Student::GetGrade() const
14 {
15     return grade;
16 }
17 
18 std::string Student::GetClassName() const
19 {
20     return std::string("Student");
21 }
22 
23 std::ostream& operator<<(std::ostream &os, const IStudent &student)
24 {
25     const IPerson &person = student;
26     os << person;
27     os << "Grade: " << student.GetGrade() << ", ";
28 
29     return os;
30 }

 

main.cpp

 1 #include <iostream>
 2 #include "Student.h"
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     Student student(string("Leon"), 14, 8);
 9     cout << "Student: " << student << endl;
10     
11     IStudent &iStudent = student;
12     cout << "IStudent: " << iStudent << endl;
13     cout << "ClassName: " << iStudent.GetClassName() << endl;
14 
15     IPerson &iPerson = student;
16     cout << "IPerson: " << iPerson << endl;
17     cout << "ClassName: " << iPerson.GetClassName() << endl;
18 
19     cout << sizeof(IPerson) << endl;
20     cout << sizeof(Person) << endl;
21     cout << sizeof(IStudent) << endl;
22     cout << sizeof(Student) << endl;
23 
24     return 0;
25 }

 

测试结果

Student: Name: Leon, Age: 14, Grade: 8,
IStudent: Name: Leon, Age: 14, Grade: 8,
ClassName: Student
IPerson: Name: Leon, Age: 14,
ClassName: Student
4
48
12
64