C++语言复习笔记二

零.OOP

特征:抽象-封装-继承-多态

一.自定义数据类型

1.类

class 类名
{
    private:
        私有成员(本类)
    public:
        公共成员(所有)
    protected:
        保护成员(继承类)
}

2.结构体

class 结构体名
{
    private:
        私有成员(本类)
    public:
        公共成员(所有)
    protected:
        保护成员(继承类)
}
结构体默认成员public,类默认成员private

3.联合体

union 联合体名
{
    private:
        私有成员(本类)
    public:
        公共成员(所有)
    protected:
        保护成员(继承类)
}
联合体内所有成员不能同时存在

4.枚举

enum class 枚举类型名:类型
{
    枚举1,枚举2...枚举n
}

二.构造器

1.定义

class Demo
{
    public:
        //默认生成空函数体的构造器
        //若自定义构造器,系统不再生成默认构造器
        //若要系统生成默认构造器,指明即可
        Demo()=default;
        Demo(int x,int y);
    private:
        int x,y;
}

2.初始化

Demo(int x,int y)
{
    this->x=0;
    this->y=0;
}
Demo():x(0),y(0){}

2.调用

Demo():Demo(0,0){}

三.复制构造器

调用复制构造器的情况:
1.以本类对象初始化新对象
2.实参初始化形参
3.return类的对象

class Point
{
    private:
        int x, y;
    public:
        Point():x(0),y(0){}
        Point(int x, int y);
        Point(const Point& p);
};
Point::Point(int x, int y)
{
    this->x = x;
    this->y = y;
}
Point::Point(const Point& p)
{
    this->x = p.x;
    this->y = p.y;
    cout << "复制构造函数运行中..." << endl;
}

四.析构器

class Point
{
    ~Point();
}

五.作用域,可见性,生存期

局部作用域:形参和代码块中的标识符
类作用域:类内成员
静态生存期:与程序运行期相同(static变量)
动态生存期:从声明开始到作用域结束

六.组合

1.初始化顺序

构造构造器初始化表(按成员变量在类中的定义顺序)
执行构造器体

2.类声明

class 类名;

七.友元

1.友元函数

friend double view(const Point &p1,const Point &p2)
{
    double x = p1.x - p2.x;
    double y = p1.y - p2.y;
    return sqrt(x * x + y * y);
}

2.友元类

class A
{
    friend class B;
public:
    void view() { cout << x << endl; }
private:
    int x;
};
class B
{
public:
    void set(int i)
    {
        a.x = 1;
    }
    void view()
    {
        a.view();
    }
private:
    A a;
};

八.继承

1.单继承

class 子类名:继承方式 父类名
{
    ...
}

2.多继承

class 子类名:继承方式 父类名1,继承方式 父类名2
{
    ...
}

派生类默认包含构造函数和析构函数的所有成员

3.继承方式

方式 可访问
public 父类的public和protected成员
protected 父类的protected成员
private .

4.构造器,析构器

class 父親
{
    public:
        ~父親()
        {
            cout << "父親析構器" << endl;
        }
        父親()
        {
            cout << "父親構造器" << endl;
        }
};
class 兒子:父親
{
    public:
        ~兒子()
        {
            cout << "兒子析構器" << endl;
        }
        兒子()
        {
            cout << "兒子構造器" << endl;
        }
};
class 孫子:兒子
{
    public:
        ~孫子()
        {
            cout << "孫子析構器" << endl;
        }
        孫子()
        {
            cout << "孫子構造器" << endl;
        }
};
/*
父親構造器
兒子構造器
孫子構造器
孫子析構器
兒子析構器
父親析構器
*/

九.多态

1.虚函数

声明"virtual 函数类型 函数名(形参列表);
作用:虚函数存在的唯一目的就是被继承,进而实现运行时多态

class Parent
{
    public:
        virtual void view()
        {
            cout << "Are you ok" << endl;
        }
};
class Child:Parent
{
    public:
        void view()
        {
            cout << "I'm very ok" << endl;
        }
};

2.虚析构函数

virtual~类名();

3.纯虚函数

没有函数体的虚函数
virtual 函数名()=0;

4.抽象类

有虚函数的类为抽象类

十.运算符存重载

1.重载为成员函数

class Point
{
    private:
        int x, y;
    public:
        Point(int x, int y) :x(x), y(y){}
        Point operator+(const Point &p) const;
        void view();
};
Point Point::operator+(const Point& p) const
{
    return Point(this->x + p.x, this->y + p.y);
}
void Point::view()
{
    cout << "(" << this -> x << "," << this->y << ")" << endl;
}

2.重载为非成员函数

class Point
{
    private:
        int x, y;
    public:
        Point(int x, int y) :x(x), y(y){}
        friend Point operator+(const Point& p1,const Point& p2);
        void view();
};
Point operator+(const Point& p1, const Point& p2)
{
    return Point(p1.x+p2.x,p1.y+p2.y);
}
void Point::view()
{
    cout << "(" << this -> x << "," << this->y << ")" << endl;
}

csharper
1 声望3 粉丝

曲终过尽松陵路,回首烟波十四桥。