c++面向对象笔记

1.继承类型

继承形式如下:

class derived-class: access-specifier base-class

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

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

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

2.虚继承

http://blog.csdn.net/iloveyousunna/article/details/78535618

3.运算符重载

大部分的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。

#include <iostream>
using namespace std;
class Box{
public:
    int height;
    int width;
    Box(int height, int width) : height(height), width(width){}
    //Box operator+(const Box& box);
};
Box operator+(const Box b1, const Box b2){
    return Box(b1.height + b2.height, b1.width + b2.width);
}
//Box Box::operator+(const Box& box){
//    return Box(this -> height + box.height, this -> width + box.width);
//}
int main(){
    Box b1(12,34);
    Box b2(12,32);
    Box b3 = b1 + b2;
    cout << b3.height << endl;
    cout << b3.width << endl;
    return 0;
}

4.多态

多态主要是通过virtual实现的,如果基类中不加virtual,这就是所谓的静态多态,或静态链接,函数调用在程序执行前就准备好了,有时候这也被成为早绑定。如果加上virtual,会告诉编译器不要静态链接到该函数,我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被成为动态链接,或后期绑定。

#include <iostream>
using namespace std;

class Shape{
    protected:
        int width,height;
    public:
        Shape(int a=0, int b=0){
            width = a;
            height = b;
        }
        int area(){
            cout << "Parent class area" <<endl;
            return 0;
        }
};

class Rectangle: public Shape{
    public:
        Rectangle(int a=0, int b=0):Shape(a, b){}
        int area(){
            cout << "Rectangle class area" << endl;
            return (width * height);
        }
};

class Triangle: public Shape{
    public:
        Triangle(int a=0, int b=0):Shape(a,b){}
        int area(){
            cout << "Triangle class area" << endl;
            return (width * height / 2);
        }
};

int main(){
    Shape *shape;
    Rectangle rec(10, 7);
    Triangle tri(10, 5);
    shape = &rec;
    shape -> area();
    shape = &tri;
    shape -> area();
    return 0;
}

输出结果:

Parent class area
Parent class area

如果加上virtual:

#include <iostream>
using namespace std;

class Shape{
    protected:
        int width,height;
    public:
        Shape(int a=0, int b=0){
            width = a;
            height = b;
        }
        virtual int area() = 0;
};

class Rectangle: public Shape{
    public:
        Rectangle(int a=0, int b=0):Shape(a, b){}
        int area(){
            cout << "Rectangle class area" << endl;
            return (width * height);
        }
};

class Triangle: public Shape{
    public:
        Triangle(int a=0, int b=0):Shape(a,b){}
        int area(){
            cout << "Triangle class area" << endl;
            return (width * height / 2);
        }
};

int main(){
    Shape *shape;
    Rectangle rec(10, 7);
    Triangle tri(10, 5);
    shape = &rec;
    shape -> area();
    shape = &tri;
    shape -> area();
    return 0;
}

输出结果:

Rectangle class area
Triangle class area

当然这里不一定是纯虚函数,加上virtual就行,父类也可以有自己的实现。

5.纯虚函数

当你在基类中不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。=0告诉编译器,函数没有主体,上面的虚函数是纯虚函数。
如果有一个或者以上的纯虚函数,那么这个类就是抽象类,不能有自己的实例。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值