回顾下c++的类

最近因为要搞搞QT的作业,但是好久没写C++,C++类的写法也忘得差不多。
所以看了菜鸟教程回顾一下。

先来讲讲,继承,派生,基类,派生类。

继承和派生其实都是一回事,只是说法不同,角度不同罢了。
如:子类继承了父类,父类派生了子类(子继父,父生子)。

父类 派生了(父亲生的儿子) 子类
基类 派生了 派生类

那么父类 就是基类,子类就是派生类(应该没有继承类这么一说)


访问控制和继承

派生类可以访问基类中所有的非私有成员。因此基类成员如果不想被派生类的成员函数访问,则应在基类中声明为 private。

我们可以根据访问权限总结出不同的访问类型,如下所示:

一个派生类继承了所有的基类方法,但下列情况除外:

  • 基类的构造函数、析构函数和拷贝构造函数。
  • 基类的重载运算符。
  • 基类的友元函数

当一个类派生自基类,该基类可以被继承为 public、protected 或 private 几种类型。继承类型是通过上面讲解的访问修饰符 access-specifier 来指定的。

我们几乎不使用 protected 或 private 继承,通常使用 public 继承。当使用不同类型的继承时,遵循以下几个规则:

  • 公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问。
  • 保护继承(protected): 当一个类派生自保护基类时,基类的公有和保护成员将成为派生类的保护成员。
  • 私有继承(private):当一个类派生自私有基类时,基类的公有和保护成员将成为派生类的私有成员。

代码测试验证

#include <iostream>
using namespace std;

class Shape
{
public:
    void setWidth(int w)
    {
        width = w;
    }
    Shape() //构造函数:跟类名一样的都是构造函数
    {       //如果没有其他构造函数,系统默认添加该构造函数
    }
    Shape(int w, int h) //构造函数
    {
        width = w;
        height = h;
    }

protected:
    int width;
    int height;
    void setHeight(int h)
    {
        height = h;
    };
};

class Rectangle : public Shape //公有继承
{
public:
    int getArea()
    {
        return (width * height);
    }

    void setHeight2(int h) 
    {
        setHeight(h);//基类中该函数为protected,子类可以访问到该函数,实例和其他外部类访问不了
    }
};

int main(void)
{
    Shape shape(1, 5);
    /*shape.setHeight(6);//该函数报错,因为父类中定义了是protect,除了子类可以调用,实例和其他外部类都不能访问调用,我们运行程序前删除它*/

    int area;
    Rectangle Rect;

    
    Rect.setWidth(5);//调用父类public函数
    /*Rect.setHeight(6);//该函数报错,因为父类中定义了是protect,除了子类可以调用,子类实例和其他外部类都不能访问调用,我们运行程序前删除它*/
    Rect.setHeight2(7);//该函数是子类自己定义的函数,函数内调用了父类的setHeight()
    area = Rect.getArea();//调用自己的类

    cout << "Total area:" << area << endl;
}
}

执行结果:
Total area:35

私有继承测试

#include <iostream>
using namespace std;

class Shape
{
public:
    void setWidth(int w)
    {
        width = w;
    }
    void setHeight(int h)
    {
        height = h;
    }
    int getArea()
    {
        return (width * height);
    }

    Shape() //构造函数:跟类名一样的都是构造函数
    {       //如果没有其他构造函数,系统默认添加该构造函数
        cout << "没有参数的构造函数"<<endl;   
    }
    Shape(int w, int h) //构造函数
    {
        width = w;
        height = h;
        cout << "有参数的构造函数,我给该实例的width,height赋值"<<endl;
    }

protected:
    int width;
    int height;
    
};

class Rectangle : protected Shape //公有继承
{
public:
    int getArea()
    {
        return (width * height);
    }
    void setWidth2(int w)
    {
        setWidth(w);//protected继承 父类的public都变成protected,子类也是可以访问到父类的protected属性的
    }

    void setHeight2(int h)
    {
        setHeight(h);//protected继承,父类的public都变成protected,子类也是可以访问到父类的protected属性的
    }

};

int main(void)
{   
    

    Shape shape(1, 5); //会调用有实例的构造函数
    cout << "shape.area:" << shape.getArea() <<endl;

    shape.setHeight(6);
    shape.setWidth(5);
    cout << "shape.area:" << shape.getArea() <<endl;

    Shape shape2;//会调用没有实例的构造函数
    cout << "shape.area:" << shape2.getArea() <<endl;

    int area;
    Rectangle Rect;

    /*Rect.setWidth(5); //因为是protected继承 所有该函数setWidth()从public变成了protected,所以实例和其他外部类访问不了,我们运行程序前删除它
    Rect.setHeight(6);因为是protected继承 所有该函数setWidth()从public变成了protected,所以实例和其他外部类访问不了,我们运行程序前删除它*/
    

    Rect.setWidth2(5);//调用自己的函数
    Rect.setHeight2(6);//调用自己的函数
    area = Rect.getArea(); //调用自己的类

    cout << "Total area:" << area << endl;
}
}

运行结果:
有参数的构造函数,我给该实例的width,height赋值
shape.area:5
shape.area:30
没有参数的构造函数
shape.area:0
没有参数的构造函数
Total area:30

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值