作业:继承

最后都变了- 提交于 2019-12-01 07:14:42

继承

类的继承与派生

1.类的继承就是新的类从已有的类那里得到已有的特性。
2.类的派生就是从已有类产生新类的过程。原有的类称为基类,新的类称为派生类。
3.一个派生类可以有多个基类,这种情况称为多继承。
4.直接参与派生的基类称为直接基类,通过基类参与派生的基类称为间接基类。
5.继承方式有三种:公有继承,私有继承,保护继承
公有继承:当类的继承方式为公有时,基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可直接访问。
私有继承:当类的继承方式为私有继承时,基类中的公有成员和保护成员都以私有成员身份出现在派生类中而基类的私有成员在派生类中不可直接访问。
保护继承:保护继承中基类的公有成员都以保护成员的身份出现在派生类中,而基类的私有成员不可直接访问。
6.派生类成员是指除了从基类来的继承成员之外的新增的数据和成员。

  1. C++派生类的定义语法
class 派生类名 :继承方式1,继承方式2... {     派生类成员声明  }; 
例如: class Clock { public: void showTime() {      ...  }  private: int hour,minute,second;  }  class Time:public Clock,private Clock    //继承并派生 { public:         //新增公有成员 void Show() {    showTime();    //调用基类的公有成员函数    ...  } private:        //新增私有成员 int year,month,day; }; 

公有继承实验验证代码

//class.cpp(源程序class) #include<cmath> #include"class.h" #include"R.h" #include<iostream> using namespace std; int main() {     Rectangle rect;     rect.intR(2, 3, 20, 10);     rect.move(3, 2);     cout << "The dete of rect(x,y,w,h,)" << endl;     cout << rect.GetX()<< ',' << rect.GetY() << ',' << rect.GetW() << ',' << rect.GetH()<< endl;     return 0; } 
//class.h(头文件class) #ifndef _CLASS_H #define _CLASS_H  class Point { public:     void initPoint(float x, float y)     {         this->x = x;         this->y = y;     }     void move(float offX, float offY){ x += offX; y += offY; }     float GetX()const{return x;}      float GetY()const{return y;}     private:     float x, y;  }; #endif 
//R.h(头文件R) #ifndef _R_H #define _R_H #include"class.h" class Rectangle : public Point { public:     void intR(float x, float y, float w, float h)     {         initPoint(x, y);         this->w = w;         this->h = h;     }     float GetW(){ return w; }     float GetH(){ return h; } private:     float w, h; }; #endif 


私有继承实验验证代码
//class.h(头文件class) #ifndef _CLASS_H #define _CLASS_H  class Point { public:     void initPoint(float x, float y)     {         this->x = x;         this->y = y;     }     void move(float offX, float offY){ x += offX; y += offY; }     float GetX()const{return x;}      float GetY()const{return y;}     private:     float x, y;  }; #endif 
//R.h(头文件R) #ifndef _R_H #define _R_H #include"class.h" class Rectangle : private Point { public:     void intR(float x, float y, float w, float h)     {         initPoint(x, y);         this->w = w;         this->h = h;     }     float GetW(){ return w; }     float GetH(){ return h; } private:     float w, h; }; #endif 
//class.cpp(源程序class) #include<cmath> #include"class.h" #include"R.h" #include<iostream> using namespace std; int main() {     Rectangle rect;     rect.intR(2, 3, 20, 10);     rect.move(3, 2);     cout << "The dete of rect(x,y,w,h,)" << endl;     cout << rect.GetX()<< ',' << rect.GetY() << ',' << rect.GetW() << ',' << rect.GetH()<< endl;     return 0; }

保护继承实验验证代码

//protected.h class A{     protected:     int x; } 
//protected.cpp(源程序protected) #include"protected.h" #include<iostream> using namespace std; int main() { A a; a.x=5;     //错误!!(无法访问protected成员)  }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!