C++(私有继承和保护继承)

私有继承和保护继承

  私有继承

    继承的访问控制:

      基类的public和prorected成员:都以private身份出现在派生类中;

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

    访问权限:

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

    基类的private成员;

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

 1 #pragma once
 2 #ifndef _POINT_H
 3 #define _POINT_H
 4 class Point {//基类point类的定义
 5 public://公有函数成员
 6     void initPoint(float x = 0, float y = 0) {
 7         this->x = x;
 8         this->y = y;
 9     }
10     void move(float offX,float offY) {
11         x += offX;
12         y += offY;
13     }
14     float getX() const { return x; }
15     float getY() const { return y; }
16 private:
17     float x, y;
18 };
19 #endif // !_POINT_H
Point.h
 1 #pragma once
 2 #ifndef _RECTANGLE_H
 3 #define _RECTANGLE_H
 4 #include "Point.h"
 5 class Rectangle :private Point {//派生类定义部分
 6 public://新增公有函数成员
 7     void initRectangle(float x, float y, float w, float h) {
 8         initPoint(x, y);
 9         this->w = w;
10         this->h = h;
11     }
12     void move(float offX, float offY) {
13         Point::move(offX, offY);
14     }
15     float getX() const { return Point::getX(); }
16     float getY() const { return Point::getY(); }
17     float getH() const { return h; }
18     float getW() const { return w; }
19 private:
20     float w, h;
21 };
22 #endif // !_RECTANGLE_H
Re
 1 #include <iostream>
 2 #include <cmath>
 3 #include "Rectangle.h"
 4 
 5 int main()
 6 {
 7     Rectangle rect;//定义Rectangle类的对象
 8     rect.initRectangle(2, 3, 20, 10);//设置矩形的数据
 9     rect.move(3,2);
10     std::cout << "The data of rect(x,y,w,h):" <<std:: endl;
11     std::cout << rect.getX() << ","//输出矩形的特征参数
12         << rect.getY() << ","
13         << rect.getW() << ","
14         << rect.getH() << std::endl;
15     //std::cout << "Hello World!
";
16 }
main.c

保护继承(protected)

  继承的访问控制:

    基类的额public和protected成员:都以protected身份出现在派生类中;

    基类的private成员:不可直接访问。

  访问权限:

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

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

   protected成员的特点与作用:

    对建立其所在类对象的模块来说,它与private成员的性质相同。

    对于其派生类来说,它与public成员的性质相同。

    既实现了数据的隐藏,又方便继承,实现代码重用。

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

   protected成员举例:

 1 class A{
 2     protected:
 3         int x;
 4 };
 5 
 6 int main(){
 7     A a;
 8     a.x=5; //ERROR
 9 }
10 
11 class A {
12     protected:
13         int x;
14 
15 };
16 
17 class B:public A{
18     public:
19         void function();
20 };
21 
22 void B::function(){
23     x=5; //correct
24 }
example1
 1 class A{
 2     public:
 3         void setA(int);
 4         void showA() const;
 5     private:
 6         int a;
 7 };
 8 
 9 
10 class B{
11     public:
12         void setB(int);
13         void showB() const;
14     private:
15         int b;
16 };
17 
18 class C:public A,private B{
19     public:
20         void setC(int, int, int);
21         void showC()const;
22     private:
23         int c;
24 }
25 
26 void A::setA(int x){
27     a=x;
28 }
29 
30 void B::setB(int x){
31     b=x;
32 }
33 
34 void C::setC(int x,int y,int z){
35     //派生类成员直接访问基类
36     //公有成员
37     setA(x);
38     setB(y);
39     c=z;
40 }
41 
42 //其他函数实现略
43 int main(){
44     C obj;
45     obj.setA(5);
46     obj.showA();
47     obj.setC(6,7,9);
48     obj.setB(6); //ERROR
49     obj.showB(); //ERROR
50     return 0;
51 }
example2
原文地址:https://www.cnblogs.com/xuelanga000/p/12825409.html