C++基础知识四

一、多态
1.1 概念:在基类指针指向基类对象的时候,就是使用基类的方法和属性,基类指针指向派生类对象的时候,就使用派生类对象和属性,
产生条件:1)要有继承 2)要有虚函数重写(发生在不同作用域中,函数原型相同) 3)基类指针指向派生类对象

#include <iostream>
using namespace std;

class Parent
{
    public:
        virtual void show()   //这里要变成虚函数
        {
            cout<<"this is parent"<<endl;
        }
};

class Child:public Parent      //1.继承
{
    public:
        void show()
        {
            cout<<"this is child"<<endl;    //2.要有虚函数重写
        }
};

int main(int argc, char const *argv[])
{
    Child c;
    Parent p;
    p = c;    //向上转型

    Parent *p1 = new Child;   //3.基类指针指向派生类对象
    p1->show();               //静态联编,编译器会根据p1的类型(Parent)调用Parent里面的show函数

    delete p1;
    p1 = new Parent;
    p1->show();   //相同的语句有不同的执行结果(多态)

    return 0;
}

在这里插入图片描述
1.2 多态的基本原理
在这里插入图片描述

#include <iostream>
using namespace std;

class Parent
{
    public:
        int a;
        virtual void show()   //这里要变成虚函数
        {
            cout<<"this is parent"<<endl;
        }
};

class Child:public Parent
{
    public:
        void show()
        {
            cout<<"this is child"<<endl;
        }
};

int main(int argc, char const *argv[])
{
    Child c;
    Parent p;
    
    cout<<sizeof(c)<<endl;
    cout<<sizeof(p)<<endl;
    cout<<"Parent对象的地址"<<&p<<endl;
    cout<<"成员变量a的起始地址"<<&p.a<<endl;
    cout<<"child对象的地址"<<&c<<endl;
    cout<<"child成员变量的起始地址"<<&c.a<<endl;
    return 0;
}

在这里插入图片描述
1.3 虚析构函数
根据析构规则,只能从当前基类开始往上调用析构函数,并不能调用派生类中的析构函数,通过这种方式,可以正确调用析构函数

#include <iostream>
using namespace std;

class Parent
{
    public:
        Parent()
        {
            cout<<"parent的构造函数"<<endl;
        }

        virtual void show()   //这里要变成虚函数
        {
            cout<<"this is parent"<<endl;
        }

        virtual ~Parent()
        {
            cout<<"parent的析构函数"<<endl;
        }

};

class Child:public Parent      //1.继承
{
    public:
        Child()
        {
            cout<<"Child的构造函数"<<endl;
        }
        void show()
        {
            cout<<"this is child"<<endl;    //要有虚函数重写
        }
        virtual ~Child()
        {
            cout<<"Child的析构函数"<<endl;
        }
};

int main(int argc, char const *argv[])
{
    Parent *p1 = new Child;   //基类指针指向派生类对象
    p1->show();               //静态联编,编译器会根据p1的类型(Parent)调用Parent里面的show函数

    delete p1;
    p1 = NULL;

    return 0;
}

在这里插入图片描述
1.4 动态类型识别
新关键词:dynameic_cast
dynameic_cast:是C++中的新关键词
dynameic_cast:用于基类和派生类之间的转换
dynameic_cast:要求使用的目标类型是多态–》1.要求所在类至少有一个虚函数 2.用于指针转换时,转换失败返回NULL指针 3.用于引用转换,转换失败引发bad_cast异常

自定义类型:

#include <iostream>
using namespace std;

class Parent
{
    private:
        int a;
    
    public:
        enum{ID = 0};

        virtual void show()   //虚函数
        {
            cout<<"this is parent"<<endl;
        }
        virtual int GetId()
        {
            return ID;
        }
};

class Child:public Parent
{
    public:
        int array[102400];

        enum{ID = 1};
        void show()
        {
            cout<<"this is child"<<endl;
        }

        virtual int GetId()
        {
            return ID;
        }
};

void f(Parent *p)
{
    if(p->GetId() == Child::ID)
    {
        Child *c = (Child *)p;
        c->array[102400-1] = 100;
        cout<<"转换成功"<<endl;
    }
    else
    {
        cout<<"转换失败"<<endl;
    }
}

int main(int argc, char const *argv[])
{
    Parent *p1 = new Parent;   
    //Parent *p1 = new Child; 
    f(p1);

    delete p1;

    return 0;
}

在这里插入图片描述
1.5 使用dynameic_cast

#include <iostream>
using namespace std;

class Parent
{
    private:
        int a;
    
    public:
        virtual void show()   //虚函数
        {
            cout<<"this is parent"<<endl;
        }    
};

class Child:public Parent
{
    public:
        int array[102400];
        void show()
        {
            cout<<"this is child"<<endl;
        }
};

void f(Parent *p)
{
    Child *c = dynamic_cast<Child *>(p);
    if( c != NULL)
    {
        c->array[102400-1] = 100;
        cout<<"转换成功"<<endl;
    }
    else
    {
        cout<<"转换失败"<<endl;
    }
}

int main(int argc, char const *argv[])
{
    //Parent *p1 = new Parent;   
    Parent *p1 = new Child; 
    f(p1);

    delete p1;

    return 0;
}

在这里插入图片描述
typeid()函数
功能:用来获取一个表达式的类型信息
typeid的操作对象既可以是表达式也可以是数据类型,使用方法如下:
typeid(Data Type);
typeid(expression);

#include <iostream>
using namespace std;

class Parent
{
    private:
        int a;
    
    public:
        virtual void show()   //虚函数
        {
            cout<<"this is parent"<<endl;
        }    
};

class Child:public Parent
{
    public:
        int array[102400];
        void show()
        {
            cout<<"this is child"<<endl;
        }
};

void f(Parent *p)
{
    //Child *c = dynamic_cast<Child *>(p);
    if( typeid(*p) == typeid(Child))
    {
        Child *c = (Child *)p;
        c->array[102400-1] = 100;
        cout<<"转换成功"<<endl;
    }
    else
    {
        cout<<"转换失败"<<endl;
    }
}

int main(int argc, char const *argv[])
{
    //Parent *p1 = new Parent;   
    int a;
    char ch;
    Parent p1;
    Child c1;

    const type_info &pa = typeid(a);
    const type_info &pb = typeid(ch);
    const type_info &pc = typeid(p1);
    const type_info &pd = typeid(c1);

    cout<<pa.name()<<endl;
    cout<<pb.name()<<endl;
    cout<<pc.name()<<endl;
    cout<<pd.name()<<endl;

    Parent *p = new Child; 
    f(p);

    delete p;

    return 0;
}

在这里插入图片描述
1.6 纯虚函数
在C++中,可以讲函数声明为纯虚函数,语法格式:
virtual 返回值类型 函数名(函数参数) = 0;

二、运算符重载的概念
重载:就是给运算符赋予一个新的含义,使得同一个运算符有不同的功能。
重载的本质就是函数的重载

#include <iostream>

using namespace std;

class Complex
{
    friend Complex operator+(const Complex &c1, const Complex &c2);
    private:
        int m_a;   //实部
        int m_b;   //虚部
    
    public:
        Complex(int a, int b)
        {
            this->m_a = a;
            this->m_b = b;
        }

        void print()
        {
            cout<<m_a<<"+"<<m_b<<"i"<<endl;
        }

    /*
        Complex operator+(const Complex &c)
        {
            Complex t(0,0);
            t.m_a = c.m_a + c.m_a;
            t.m_b = c.m_b + c.m_b;

            return t;
        }
    */
};

Complex operator+(const Complex &c1, const Complex &c2)
{
    Complex t(0,0);
    t.m_a = c1.m_a + c2.m_a;
    t.m_b = c1.m_b + c2.m_b;

    return t;
}


int main(int argc, char const *argv[])
{
    Complex c1(1 , 2);
    Complex c2(2 , 3);
    c1.print();
    Complex t(0 , 0);

    //双目运算符,通常双目运算符重载用类的友元函数,但是有四个要用类的成员函数   =,(),[],->
    t = c1 + c2;    //编译器会转成 t = operator+(c1,c2)
                    //类成员函数会变成 ---t = c1.operator(c2);

    t.print();

    return 0;
}

在这里插入图片描述
运算符重载的规则
1.不能重载的运算符:
1)作用域解析运算符 ::
2)条件运算符 ?:
3)直接成员访问运算符 .
4) 类成员指针引用的运算符 .*
5)sizeof运算符 sizeof

2.重载不能改变运算符的优先级和结合性
3.重载不能改变运算符的用法
4.运算符重载不能有默认参数,否则会改变运算符的操作数
5.运算符重载函数可以作为类的成员函数也可以作为全局函数

运算符重载步骤:
1)写出重载函数名称,比如:operator+
2)根据操作数写函数的形参,如 operator+(int a, Complex &c)
3)根据使用场景写出函数的返回值,如:Complex operator+(int a, Complex &c)
4)完成函数体

2.4 重载输出运算符

#include <iostream>

using namespace std;

class Complex
{
    friend ostream& operator<<(ostream &out, Complex &c);
    friend Complex operator+(const Complex &c1, const Complex &c2);
    private:
        int m_a;   //实部
        int m_b;   //虚部
    
    public:
        Complex(int a, int b)
        {
            this->m_a = a;
            this->m_b = b;
        }

        void print()
        {
            cout<<m_a<<"+"<<m_b<<"i"<<endl;
        }

    /*
        Complex operator+(const Complex &c)
        {
            Complex t(0,0);
            t.m_a = c.m_a + c.m_a;
            t.m_b = c.m_b + c.m_b;

            return t;
        }
    */
};

Complex operator+(const Complex &c1, const Complex &c2)
{
    Complex t(0,0);
    t.m_a = c1.m_a + c2.m_a;
    t.m_b = c1.m_b + c2.m_b;

    return t;
}

//输出运算符重载
ostream& operator<<(ostream &out, Complex &c)
{
    out<<c.m_a<<"+"<<c.m_b<<"i";
    return out;
}


int main(int argc, char const *argv[])
{
    Complex c1(1 , 2);
    Complex c2(2 , 3);
    c1.print();
    Complex t(0 , 0);

    //双目运算符,通常双目运算符重载用类的友元函数,但是有四个要用类的成员函数   =,(),[],->
    t = c1 + c2;    //编译器会转成 t = operator+(c1,c2)
                    //类成员函数会变成 ---t = c1.operator(c2);

    cout<<t<< endl;
    //t.print();


    return 0;
}

2.5 单目运算符

#include <iostream>

using namespace std;

class Complex
{
    friend ostream& operator<<(ostream &out, Complex &c);
    friend Complex operator+(const Complex &c1, const Complex &c2);
    private:
        int m_a;   //实部
        int m_b;   //虚部
    
    public:
        Complex(int a, int b)
        {
            this->m_a = a;
            this->m_b = b;
        }

        void print()
        {
            cout<<m_a<<"+"<<m_b<<"i"<<endl;
        }

        Complex& operator++()   //前置++
        {
            this->m_a++;
            this->m_b++;

            return *this;   
        }

        Complex& operator++(int)   //后置++
        {
            Complex t = *this;
            this->m_a++;
            this->m_b++;

            return t;
        }
 
    /*
        Complex operator+(const Complex &c)
        {
            Complex t(0,0);
            t.m_a = c.m_a + c.m_a;
            t.m_b = c.m_b + c.m_b;

            return t;
        }
    */
};

Complex operator+(const Complex &c1, const Complex &c2)
{
    Complex t(0,0);
    t.m_a = c1.m_a + c2.m_a;
    t.m_b = c1.m_b + c2.m_b;

    return t;
}

//输出运算符重载
ostream& operator<<(ostream &out, Complex &c)
{
    out<<c.m_a<<"+"<<c.m_b<<"i";
    return out;
}


int main(int argc, char const *argv[])
{
    Complex c1(1 , 2);
    Complex c2(2 , 3);
    c1.print();
    Complex t(0 , 0);

    //双目运算符,通常双目运算符重载用类的友元函数,但是有四个要用类的成员函数   =,(),[],->
    t = c1 + c2;    //编译器会转成 t = operator+(c1,c2)
                    //类成员函数会变成 ---t = c1.operator(c2);

    cout<<t<< endl;
    //t.print();

    t = ++c1;
    cout<<t<< endl;

    t = c1++;
    cout<<t<< endl;



    return 0;
}


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值