当前位置 : IT培训网 > IT培训 > 交流分享 > 简析C++接口与实现的抽象分离方法

简析C++接口与实现的抽象分离方法

时间:2019-02-22 15:54:52  来源:技能培训网  作者:IT培训网  已有:名学员访问该课程
C++接口与实现的抽象分离方法是什么?初学C++语言,这是最为重要的一点,现在IT培训网就对此做详细的解析说明。

C++接口与实现的抽象分离方法是什么?初学C++语言,这是最为重要的一点,现在IT培训网就对此做详细的解析说明。

简析C++接口与实现的抽象分离方法_www.itpxw.cn

C++接口与实现怎样抽象分离?

1 #ifndef I_PERSON_H_

2 #define I_PERSON_H_

3

4 #include

5 #include

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

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

总之,有关C++接口与实现的抽象分离方法如上所述,希望对C++学习小白有所帮助,C++属贵族稀缺语言,大家一定要珍惜好好学习,且行且珍惜!

顶一下
(0)
0%
踩一下
(0)
0%

IT培训0元试听 每期开班座位有限.0元试听抢座开始! IT培训0元试听

  • 姓名 : *
  • 电话 : *
  • QQ : *
  • 留言 :
  • 验证码 : 看不清?点击更换请输入正确的验证码

在线咨询在线咨询

温馨提示 : 请保持手机畅通,咨询老师为您
提供专属一对一报名服务。

------分隔线----------------------------
------分隔线----------------------------

推荐内容

相关热点