C/C++教程

C++的面向对象

本文主要是介绍C++的面向对象,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

类的写法

模板:
class Car
{
public:
    ...
private:
    ...
};

实例:
class Car
{
public:
    // 初始化函数没有返回值,参数列表可能为空,函数体可能为空
    Car()
    {
        cout<< "父类无参构造"<<endl;
    }

    Car(int speed,int price)
    {
        this->price = price;
        this->speed = speed;
        cout<< "父类有参构造"<<endl;
    }

    ~Car()
    {
        cout<< "父类析构函数"<<endl;
    }

    void show_info()
    {
        cout<<"base car speed:"<<speed<<" price:"<<price<<endl;
    }
private:
    int price;
    int speed;
};

 

类的继承

模板
class 子类:public 父类
{
public:
    ...
private:
   ...  
};

实例:
class Rect:public Shape
{
public:
    void show_info()
    {
        cout<<"Rect info"<<endl;
    }
};

 

构造函数:

1. 默认构造函数

2. 有参构造函数

3. 拷贝构造函数

class Ele_car:public Car
{
public:
    Ele_car(int speed,int price,int bat):Car(speed,price),battery(bat)
    {
        cout<< "ele_car 构造函数"<< endl;
    }
    Ele_car():Ele_car(20,10,70)
    {
        cout<<"委托构造函数"<<endl;
    }

    // 深拷贝构造函数
    Ele_car(const Ele_car& object)
    {
        ptr = new char;
        *ptr = *object.ptr;
    }


    void show_info();

    static void message()
    {
        cout<< "message: "<<fac<<endl;
    }

    ~Ele_car()
    {
        cout<< "子类析构函数"<<endl;
        delete ptr;
    }

    char* ptr;

private:
    static string fac;
    int battery;
};

 

 

多态:

函数重载,运算符重载,多态

函数重载:
class cube
{
public:

    // 函数重载
    void show_info();
    void show_info(string user);

    cube()
    {

    }

    cube(int len):length(len)
    {

    }

    // 运算符重载
    cube operator+(const cube& object)
    {
        cube temp;
        temp.length = this->length + object.length;
        return temp;
    }

private:
    int length;
};

 

多态:

#include "iostream"
using namespace std;



// 动态多态,虚函数
class Shape
{
public:
    virtual void show_info() = 0;// 含有 纯虚函数的 类 是抽象类, 不能实例化对象
};

class base
{
public:
    virtual void show_info(){   // 含有 虚函数的 类  必须实现,否则就报错

    }
};


class Rect:public Shape
{
public:
    void show_info()
    {
        cout<<"Rect info"<<endl;
    }
};

class Round:public Shape
{
public:
    void show_info()
    {
        cout<<"Round info"<<endl;
    }
};

void print_shape_info(Shape& shape)
{
    shape.show_info();
}


int main()
{
    Rect rect;
    Round round;

    print_shape_info(rect);
    print_shape_info(round);

    return 0;
}

 

虚函数,纯虚函数

虚析构,纯虚析构

#include "iostream"
using namespace std;

// 虚析构与纯虚析构
// 作用:对于子类申请的堆区空间使用父类指针不会释放
class Shape
{
public:
    virtual void show_info() = 0;// 含有 纯虚函数的 类 是抽象类, 不能实例化对象

    virtual ~Shape()
    {
        cout<<"父类虚析构函数"<<endl;
    }
};



class Rect:public Shape
{
public:
    Rect(int height)
    {
        this->height = new int;
        *this->height = height;
    }
    ~Rect()
    {
        delete height;
        cout<<"Rect 的析构函数"<<endl;
    }
    void show_info()
    {
        cout<<"Rect height: "<<*this->height<<endl;
    }

private:
    int* height;
};

class Round:public Shape
{
public:

    void show_info()
    {
        cout<<"Round info"<<endl;
    }

private:
    int* radius;
};

void print_shape_info(Shape& shape)
{
    shape.show_info();
}


int main()
{
    Shape* rect = new Rect(10);
    rect->show_info();
    delete rect;
    return 0;
}

 

这篇关于C++的面向对象的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!