c++知识学习 (8)

多继承

class Worker:public Person,public job 

多继承的二异性:两个父类有相同名字的方法。  

 

 1 //
 2 //  Person.h
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-8.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 //防止重复引用
10 #ifndef __ArrayTest__Person__
11 #define __ArrayTest__Person__
12 
13 #include <iostream>
14 using namespace std;
15 class Person{
16 //---------成员变量--------
17 public :
18     
19     int age;
20     
21 private :
22     int weight;
23     char  * name;
24     char sex;
25 //---------成员方法--------
26 public:
27     //----构造函数-------
28     Person();
29     //-----构造函数重载-----
30     Person(int age);
31     
32     //------拷贝构造函数-------
33     Person(const Person & per);
34     
35     //----------操作符重载----------
36     Person & operator=(const Person &right);
37     
38     void setWeight(int weight);
39     int getWeight() const;
40     //char * getName() const;
41     //const 指针,保证指针不被修改
42     const char * getName() const;
43     void setName(const char * name);
44     //-----虚函数:子类的可能会重写父类的的方法,当父类的指针指向子类的对象,子类对象调用的是子类自己的方法--------
45     virtual void info() const;
46     //-----纯虚函数--------
47     //virtual void info() const=0;
48     ~Person();
49     static void print();
50     //---类方法重载----
51     static Person * Create();
52     static Person * Create(const char * name);
53 };
54 
55 class Action{
56 private:
57     int x;
58     int y;
59 public:
60     void move(int x,int y);
61     virtual void  info() const;
62 
63 };
64 
65 class Student:public Person,public Action{
66 private:
67     float score;
68 public:
69     void info() const;
70     Student();
71     ~Student();
72  
73 };
74 
75 
76 
77 class Worker:public Person{
78 public :
79     void info() const;
80 };
81 #endif /* defined(__ArrayTest__Person__) */

 

 1 //
 2 //  Person.h
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-8.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 //防止重复引用
10 #ifndef __ArrayTest__Person__
11 #define __ArrayTest__Person__
12 
13 #include <iostream>
14 using namespace std;
15 class Person{
16 //---------成员变量--------
17 public :
18     
19     int age;
20     
21 private :
22     int weight;
23     char  * name;
24     char sex;
25 //---------成员方法--------
26 public:
27     //----构造函数-------
28     Person();
29     //-----构造函数重载-----
30     Person(int age);
31     
32     //------拷贝构造函数-------
33     Person(const Person & per);
34     
35     //----------操作符重载----------
36     Person & operator=(const Person &right);
37     
38     void setWeight(int weight);
39     int getWeight() const;
40     //char * getName() const;
41     //const 指针,保证指针不被修改
42     const char * getName() const;
43     void setName(const char * name);
44     //-----虚函数:子类的可能会重写父类的的方法,当父类的指针指向子类的对象,子类对象调用的是子类自己的方法--------
45     virtual void info() const;
46     //-----纯虚函数--------
47     //virtual void info() const=0;
48     ~Person();
49     static void print();
50     //---类方法重载----
51     static Person * Create();
52     static Person * Create(const char * name);
53 };
54 
55 class Action{
56 private:
57     int x;
58     int y;
59 public:
60     void move(int x,int y);
61     virtual void  info() const;
62 
63 };
64 
65 class Student:public Person,public Action{
66 private:
67     float score;
68 public:
69     void info() const;
70     Student();
71     ~Student();
72  
73 };
74 
75 
76 
77 class Worker:public Person{
78 public :
79     void info() const;
80 };
81 #endif /* defined(__ArrayTest__Person__) */

 

 

  2 //  Person.cpp
  3 //  ArrayTest
  4 //
  5 //  Created by 张学院 on 14-1-8.
  6 //  Copyright (c) 2014年 com.mix. All rights reserved.
  7 //
  8 
  9 #include "Person.h"
 10 //------方法实现格式------
 11 //----返回值 类::方法名 (参数)------
 12 void Person::setWeight(int weight){
 13     //-----this 代表
 14     this->weight=weight;
 15 }
 16 //--------const 编译限制-------
 17 int  Person::getWeight() const{
 18     //weight++;报错
 19     return weight;
 20 }
 21 const char * Person::getName() const{
 22     
 23     return name;
 24     
 25 }
 26 void Person::setName(const char * name){
 27 
 28     strcpy(this->name, name);
 29     
 30 }
 31 void Person::info() const{
 32 
 33     //printf("%s
%d
%c
%d
",name,age,sex,weight);
 34     printf(" this is a person
");
 35 
 36 }
 37 //--------构造函数:初始化成员变量------------
 38 Person::Person(){
 39     printf("call the functon Person()
");
 40     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
 41     //strcpy(name, "a man");
 42     //在堆里分配内存,返回首地址,在堆里面分配的空间一定要记得释放内存!!!!!
 43     name = new char[255];
 44     strcpy(name, "a man");
 45     weight=60;
 46     age=20;
 47     sex='m';
 48 }
 49 //--------构造函数:重载------------
 50 Person::Person(int age){
 51     printf("call the functon Person(int age)
");
 52     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
 53     //strcpy(name, "a man");
 54     name = new char[255];
 55     strcpy(name, "a man");
 56     weight=60;
 57     this->age=age;
 58     sex='m';
 59     
 60 }
 61 
 62 //------拷贝构造函数-------
 63 Person::Person(const Person & person){
 64 /* 自己不实现的拷贝构造函数的话,系统生成的拷贝构造函数
 65     this->name=person.name;
 66     this->age=person.age;
 67     this->weight=person.weight;
 68     this->sex=person.sex;
 69 */
 70  // 自己实现的拷贝构造函数的话
 71     //重新分配内存
 72     this->name= new char[255];
 73     strcpy(this->name, person.name);
 74     this->age=person.age;
 75     this->weight=person.weight;
 76     this->sex=person.sex;
 77     
 78 }
 79 
 80 //-----------析构函数---------
 81 Person::~Person(){
 82     //在析构函数里面释放内存
 83     printf("call the functon ~Person()析构函数 
");
 84     delete [] name;
 85    
 86 }
 87 
 88 //----------操作符重载----------
 89 Person & Person::operator=(const Person &right){
 90     /* 自己不实现的操作符重载的话,系统生成的拷贝构造函数
 91     
 92     this->name=right.name;
 93     this->age=right.age;
 94     this->sex=right.sex;
 95     this->weight=right.weight;
 96       */
 97     //---------不需要再次分配内存-------
 98    // this->name= new char[255];
 99     strcpy(this->name, right.name);
100     this->age=right.age;
101     this->sex=right.sex;
102     this->weight=right.weight;
103     return *this;
104 }
105 //-------不用在加static--------
106 void Person::print(){
107     printf("Person::print()
");
108 
109 }
110 
111 Person * Person::Create(){
112 
113     Person * per= new Person();
114     return per;
115 }
116 Person * Person::Create(const char * name){
117     Person * per=Person::Create();
118     per->setName(name);
119     return per;
120 }
121 //--------------student----------
122 //-------------调用父类的同名的函数,----------------
123 void Student::info() const{
124    // printf("%s
%d
%c
%d %.2f
",name,age,sex,weight,score);
125    // Person::info();
126     //printf("%.2f
",score);
127     Person::info();
128     Action::info();
129     printf(" this is a student
");
130 
131 }
132 //---------子类的构造函数:不需要再调用父类的构造函数------
133 Student::Student(){
134     score=1.00f;
135 
136 }
137 Student::~Student(){
138     printf(" call the functon ~Student()析构函数 
");
139 
140 }
141 
142 void Worker::info() const{
143     printf(" this is a worker
");
144 
145 }
146 
147 void Action::move(int x,int y){
148     this->x=x;
149     this->y=y;
150     
151 }
152 void Action::info() const{
153     printf(" move to %d,%d
",x,y);
154 }
 1 //
 2 //  main.cpp
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-6.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 
11 #include <string>
12 #include "Person.h"
13 
14 int main()
15 {
16     
17     Student stu;
18     stu.move(3,5);
19     
20     
21     Person per;
22     per.info();
23     
24     Action a;
25     a.info();
26     
27     //----如果student没有info方法的,而Person和Action都有info的话,就会报错--------
28     stu.info();
29 }

输出:

call the functon Person()

call the functon Person()

 this is a person

 move to 0,1

 this is a person

 move to 3,5

 this is a student

call the functon ~Person()析构函数 

 call the functon ~Student()析构函数 

call the functon ~Person()析构函数 

 

原文地址:https://www.cnblogs.com/aosting/p/3513919.html