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

发表于:2019-2-11 10:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:王景略    来源:博客园

#
DoNet
分享:
  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#ifndefPERSON_H_
2#definePERSON_H_
3
4#include"IPerson.h"
5
6classPerson:virtualpublicIPerson
7{
8public:
9Person(conststd::string&name,constintage);
10virtual~Person();
11std::stringGetName()constoverride;
12intGetAge()constoverride;
13std::stringGetClassName()constoverride;
14private:
15std::stringname;
16intage;
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

     上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号