【继承与多态】C++:继承中的赋值兼容规则,子类的成员函数,虚函数(重写),多态

    实现基类(父类)以及派生类(子类),验证继承与转换--赋值兼容规则:


  1. 子类对象可以赋值给父类对象(切割/切片)

  2. 父类对象不能赋值给子类对象

  3. 父类的指针/引用可以指向子类对象

  4. 子类的指针/引用不能指向父类对象(可以通过强制类型转换完成)


#include<iostream>
using namespace std;

class People    //父类或者基类
{
public:
    void Display()
    {
        cout << "_name" << endl;
    }
protected:
    string _name;
};


class Student:public People        //子类或者派生类
{
protected:
    int _num;
};


void Test()
{
    People p;
    Student s;
    p = s;    //切片
    //s = p;    //无法通过,说明父类对象不可以赋值给子类对象
    People* p1 = &s;    //父类的指针和引用可以指向子类
    People& p2 = s;

    //Student* s1 = &p;    //子类的指针和引用不可以指向父类
    //Student& s2 = p;
    Student* s1 = (Student*)&p;    //可以通过强转实现
    Student& s2 = (Student&)p;

    //p2->_num = 10;    //_num是子类对象,要越界父类对象才能访问到子类对象
    //s2._num = 20;
}


int main()
{
    Test();
    system("pause");
    return 0;
}


    如何书写基类与派生类的默认成员函数呢?如:构造函数、拷贝构造函数、赋值运算符重载、析构函数。

#include<iostream>
using namespace std;

class People
{
public:
    People(const char* name)
        :_name(name)
    {
        cout << "People()" << endl;
    }

    People(const People& p)
        :_name(p._name)
    {
        cout << "People(const People& p)" << endl;
    }

    People& operator=(const People& s)
    {
        if (&s != this)
        {
            cout << "People& operator= (const People& s)"<<endl;
            _name = s._name;
        }
        return *this;
    }

    ~People()
    {
        cout << "~People()" << endl;
    }

protected:
    string _name;
};

class Student:public People
{
public:
    Student(const char* name, int num)
        :People(name)
        , _num(num)
    {
        cout << "Student()" << endl;
    }

    Student(const Student& s)
        :People(s)
        , _num(s._num)
    {
        cout << "Student(const Student& s)" << endl;
    }
    
    Student& operator= (const Student& s)
    {
        if (this != &s)
        {
            cout << "Student& opeartor= (const Student& s)" << endl;
            People::operator=(s);
            _num = s._num;
        }
        return *this;
    }

    ~Student()
    {
        cout << "~Student()" << endl;
    }
protected:
    int _num;
};

void Test()
{
    Student s1("张三",15);
    Student s2(s1);
    Student s3("李四",12);
    s3 = s1;
}


int main()
{
    Test();
    system("pause");
    return 0;
}


虚函数&多态

虚函数:

在类的成员函数前面加上virtual,成为虚函数。

虚函数的重写:

当在子类中定义了一个与父类相同的虚函数时,就称为子类的函数重写了父类的虚函数。

多态:

使用父类的函数或者指针调用函数时,若指向父类的虚函数就调用父类的虚函数,若调用子类的虚函数就调用子类的虚函数。

    如:

#include<iostream>
using namespace std;

class People
{
public:
    virtual void BuyTickets()
    {
        cout << "买票" << endl;
    }
};

class Student :public People
{
public:
    virtual void BuyTickets()
    {
        cout << "买票-半价" << endl;
    }
};

void Fun(People& p)
{
    p.BuyTickets();
}

void Test()
{
    People p;
    Student s;
    Fun(p);//People为父类,则调用父类的虚函数。
    Fun(s);//调用子类的虚函数。
}

int main()
{
    Test();
    system("pause");
    return 0;
}


本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1749895

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值