国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > C++ Notes-Inheritance-02

C++ Notes-Inheritance-02

来源:程序员人生   发布时间:2016-11-19 14:50:18 阅读次数:2149次

继承方式

1、继承方式简介

1、不同的继承方式的影响主要体现在:

(1)派生类成员对其基类成员的访问权限

(2)通过派生类对象对基类成员的访问权限

2、3种继承方式

(1)公有继承(public)

(2)私有继承(private)

(3)保护继承(protected)

2、公有继承(public)

1、继承的访问控制

(1)基类的public和protected成员:访问属性在派生类中保持不变

(2)基类的private成员:不可直接访问

2、访问权限

(1)派生类中的成员函数:可以直接访问基类中的public和protected成员,但不能直接访问基类的private成员

(2)通过派生类的对象:只能访问public成员

3、Code

#include <iostream> #include <cmath> using namespace std; class Point { //基类Point类的定义 public: //公有函数成员 void initPoint(float x = 0, float y = 0) { 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; }; class Rectangle: public Point { //派生类定义部份 public: //新增公有函数成员 void initRectangle(float x, float y, float w, float h) { initPoint(x, y); //调用基类公有成员函数 this->w = w; this->h = h; } float getH() const { return h; } float getW() const { return w; } private: //新增私有数据成员 float w, h; }; int main() { Rectangle rect; //定义Rectangle类的对象 rect.initRectangle(2, 3, 20, 10); //设置矩形的数据 rect.move(3,2); //移动矩形位置 cout << "The data of rect(x,y,w,h): " << endl; //输出矩形的特点参数 cout << rect.getX() <<", "<< rect.getY() << ", "<< rect.getW() << ", "<< rect.getH() << endl; return 0; }

3、私有继承(private)

1、继承的访问控制

(1)基类的public和protected成员:都以private的身份出现在派生类中

(2)基类的private成员:不可直接访问

2、访问权限

(1)派生类中的成员函数:可以直接访问基类中的public和protected成员,但不能直接访问基类的private成员

(2)通过派生类的对象:不能直接访问从基类继承的任何成员

3、Code

值得注意的是,基类中的move函数通过私有继承到派生类后,是不能在类外使用的,如果要在类外使用,可以在派生类的public下重写1个move函数,这样在类外就能够调用了。

#include <iostream> #include <cmath> using namespace std; class Point { //基类Point类的定义 public: //公有函数成员 void initPoint(float x = 0, float y = 0) { 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; }; class Rectangle: private Point { //派生类定义部份 public: //新增公有函数成员 void initRectangle(float x, float y, float w, float h) { initPoint(x, y); //调用基类公有成员函数 this->w = w; this->h = h; } void move(float offX, float offY) //虽然对象不能在类外使用基类的move函数,但是在派生类中可使用基类的move函数 { Point::move(offX, offY); } float getX() const { return Point::getX(); } float getY() const { return Point::getY(); } float getH() const { return h; } float getW() const { return w; } private: //新增私有数据成员 float w, h; }; int main() { Rectangle rect; //定义Rectangle类的对象 rect.initRectangle(2, 3, 20, 10); //设置矩形的数据 rect.move(3,2); //移动矩形位置,要注意此时的move函数是派生类Rectangle中的成员函数 cout << "The data of rect(x,y,w,h): " << endl; //输出矩形的特点参数 cout << rect.getX() <<", "<< rect.getY() << ", "<< rect.getW() << ", "<< rect.getH() << endl; return 0; }

4、保护继承(protected)

1、继承的访问控制

(1)基类的public和protected成员:都以protected的身份出现在派生类中

(2)基类的private成员:不可直接访问

2、访问权限

(1)派生类中的成员函数:可以直接访问基类中的public和protected成员,但不能直接访问基类的private成员

(2)通过派生类的对象:不能直接访问从基类继承的任何成员

3、protected 成员的特点与作用

(1)对建立其所在类对象的模块来讲,它与private成员的性质相同

(2)对其派生类来讲,它与public性质相同

(3)既实现了数据隐藏,又方便继承,实现代码重用

4、protected用处

如果代码是同1组,同1个团队内的人写的,那末我们可以继承使用基类protected的细节。但是如果不是同1组人写的,或是买的类库,那末使用protected的细节就不是那末方便了。

5、Code

class A { protected: int x; }; int main() { A a; a.x = 5;//毛病 } class A { protected: int x; }; class B: public A{ public: void function(); }; void B:function() { x = 5; //正确 }


5、多继承

如果派生类有多个基类,也就是多继承,可以用不同的方式继承每一个基类。

Code:

class A { public: void setA(int x){ a=x; } void showA() const{cout<<a<<endl;}; private: int a; }; class B { public: void setB(int x){ b=x; } void showB() const{cout<<b<<endl;}; private: int b; }; class C : public A, private B { public: void setC(int x, int y, int z) { //派生类成员直接访问基类的公有成员 setA(x); setB(y); c = z; } void showC() const{cout<<c<<endl;}; private: int c; }; int main() { C obj; obj.setA(5); obj.showA(); obj.setC(6,7,9); obj.showC(); // obj.setB(6); 毛病 // obj.showB(); 毛病 return 0; }






生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生