C++介面與實現的抽象分離
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: 9virtual std::string GetName() const = 0; 10virtual int GetAge() const = 0; 11virtual 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: 9Person(const std::string &name, const int age); 10virtual ~Person(); 11std::string GetName() const override; 12int GetAge() const override; 13std::string GetClassName() const override; 14 private: 15std::string name; 16int age; 17 }; 18 19 #endif
Person.cpp
1 #include "Person.h" 2 3 Person::Person(const std::string &name, const int age) : 4name(name), 5age(age) 6 { 7 } 8 9 Person::~Person() 10 { 11 } 12 13 std::string Person::GetName() const 14 { 15return name; 16 } 17 18 int Person::GetAge() const 19 { 20return age; 21 } 22 23 std::string Person::GetClassName() const 24 { 25return std::string("Person"); 26 } 27 28 std::ostream& operator<<(std::ostream &os, const IPerson &person) 29 { 30os << "Name: " << person.GetName() << ", " 31<< "Age: " << person.GetAge() << ", "; 32 33return 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: 11virtual 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: 10Student(const std::string &name, const int age, const int grade); 11~Student(); 12 13int GetGrade() const override; 14std::string GetClassName() const override; 15 private: 16int 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) : 4Person(name, age), 5grade(grade) 6 { 7 } 8 9 Student::~Student() 10 { 11 } 12 13 int Student::GetGrade() const 14 { 15return grade; 16 } 17 18 std::string Student::GetClassName() const 19 { 20return std::string("Student"); 21 } 22 23 std::ostream& operator<<(std::ostream &os, const IStudent &student) 24 { 25const IPerson &person = student; 26os << person; 27os << "Grade: " << student.GetGrade() << ", "; 28 29return os; 30 }
main.cpp
1 #include <iostream> 2 #include "Student.h" 3 4 using namespace std; 5 6 int main() 7 { 8Student student(string("Leon"), 14, 8); 9cout << "Student: " << student << endl; 10 11IStudent &iStudent = student; 12cout << "IStudent: " << iStudent << endl; 13cout << "ClassName: " << iStudent.GetClassName() << endl; 14 15IPerson &iPerson = student; 16cout << "IPerson: " << iPerson << endl; 17cout << "ClassName: " << iPerson.GetClassName() << endl; 18 19cout << sizeof(IPerson) << endl; 20cout << sizeof(Person) << endl; 21cout << sizeof(IStudent) << endl; 22cout << sizeof(Student) << endl; 23 24return 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