C++多态

1.封装一个动物的基类,类中有私有成员:姓名,颜色,指针成员年纪再封装一个狗这样类,共有继承于动物类,自己拓展的私有成员有:指针成员:腿的个数(整型 int count),共有成员函数:会叫:void speak()要求:分别完成基类和派生类中的:构造函数、析构函数、拷贝构造函数、拷贝赋值函数eg : Dog d1;Dog d2(.....);Dog d3(d2);d1 = d3;

#include <iostream>

using namespace std;

class Animal
{
private:
    string name;
    string color;
    int *age;
public:
    Animal(){}
    Animal(string name, string color, int age):name(name),color(color),age(new int(age))
    {
        cout << "父类有参构造函数" << endl;
    }
    ~Animal()
    {}
    void speak()
    {
        cout << "汪汪" << endl;
    }
    Animal(const Animal &other):name(other.name),color(other.color),age(new int(*other.age))
    {
        cout << "Animal::拷贝构造函数" << endl;
    }
    Animal &operator=(const Animal &other)
    {
        //避免自己给自己赋值
        if(this != &other)
        {
            name=other.name;
            color=other.color;
            age=new int(*other.age);
        }
        cout << "Animal::拷贝构造函数" << endl;
        return *this;
    }
     void show()
     {
         cout << "name: " << name << endl;
         cout << "age: " << *age << endl;
         cout << "color: " << color  << endl;
     }
};

class Dog:public Animal
{
private:
    int *tu;


public:
    Dog(){}
    Dog(int tu,string name,string color,int age):Animal(name,color,age),tu(new int(tu))
    {
        cout << "子类的有参构造"<<endl;
    }
    Dog(const Dog &other):Animal(other),tu(new int(*other.tu))
    {
        cout << "Dog::拷贝构造函数" << endl;
    }

    ~Dog()
    {
        cout << "Dog::析构函数" << endl;
    }
    Dog &operator=(const Dog &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            tu = new int(*other.tu);
        }
        cout << "dog::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        cout <<"子类::show" << endl;
        cout << *tu << endl;
    }

};
int main()
{
        Dog d1(4, "小黄", "黄" , 3);
        Dog d2(d1);
        Dog d3;
        d3 = d2;
        d3.speak();
        d1.show();
        d1.Animal::show();

    return 0;
}

 

 

#include <iostream>

using namespace std;

class Animal
{
private:
    string name;
    string chc;  //特征

public:
    Animal(){}
    Animal(string name,string chc):name(name),chc(chc)
    {}
    virtual void perform() = 0;

    virtual ~Animal()
    {
        cout << "虚析构函数" << endl;
    }
    void show()
    {
        cout << name << endl;
        cout << chc  << endl;
    }
};


class Lion:public Animal
{
private:
   string name;
public:
    Lion(){}
    Lion(string name, string chc, string n):Animal(name,chc),name(n)
    {}
   void perform()
   {
       cout << "它表演吼叫,很有威慑力" << endl << endl;
   }
   ~Lion()
   {
       cout << "Lion::虚构函数" << endl;
   }
   void show()
   {
       cout << name << endl;
   }
};

class Elephant:public Animal
{
private:
    string name;
public:
    Elephant(){}
    Elephant(string name,string chc, string n):Animal(name,chc),name(n)
    {}
    void perform()
    {
        cout << "它会用鼻子吃东西" << endl;
    }
    ~Elephant()
    {
        cout << "Elephant::虚析构函数" << endl;
    }
    void show()
    {
        cout << name << endl;
    }
};
int main()
{

    Lion s("狮子" , "凶猛" , "不要靠近");

    Animal *p = &s; //父类指针指向子类对象
    s.Animal::show();
    p->perform();   //重写虚函数

    Elephant e("大象" , "鼻子长" ," 远离");
    Animal *p1 = &e;
    e.Animal::show();
    p1->perform();
    return 0;
}

 

 

 

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的多态(Polymorphism)是指在父类和子类之间的相互转换,以及在不同对象之间的相互转换。 C++中的多态性有两种:静态多态和动态多态。 1. 静态多态 静态多态是指在编译时就已经确定了函数的调用,也称为编译时多态C++中实现静态多态的方式主要有函数重载和运算符重载。 函数重载是指在同一作用域内定义多个同名函数,但它们的参数列表不同。编译器根据传递给函数的参数类型和数量来确定调用哪个函数。例如: ```c++ void print(int num) { std::cout << "This is an integer: " << num << std::endl; } void print(double num) { std::cout << "This is a double: " << num << std::endl; } int main() { int a = 10; double b = 3.14; print(a); // 调用第一个print函数 print(b); // 调用第二个print函数 } ``` 运算符重载是指对C++中的运算符进行重新定义,使其能够用于自定义的数据类型。例如: ```c++ class Complex { public: Complex(double real, double imag) : m_real(real), m_imag(imag) {} Complex operator+(const Complex& other) const { return Complex(m_real + other.m_real, m_imag + other.m_imag); } private: double m_real; double m_imag; }; int main() { Complex a(1.0, 2.0); Complex b(3.0, 4.0); Complex c = a + b; // 调用Complex类中重载的+运算符 } ``` 2. 动态多态 动态多态是指在运行时根据对象的实际类型来确定调用哪个函数,也称为运行时多态C++中实现动态多态的方式主要有虚函数和纯虚函数。 虚函数是在父类中定义的可以被子类重写的函数,使用virtual关键字声明。当一个对象的指针或引用指向一个子类对象时,调用虚函数时会根据实际的对象类型来确定调用哪个函数。例如: ```c++ class Shape { public: virtual void draw() { std::cout << "Drawing a shape." << std::endl; } }; class Circle : public Shape { public: void draw() override { std::cout << "Drawing a circle." << std::endl; } }; int main() { Shape* shape_ptr = new Circle(); shape_ptr->draw(); // 调用Circle类中重写的draw函数 } ``` 纯虚函数是在父类中定义的没有实现的虚函数,使用纯虚函数声明(如virtual void func() = 0;)。父类中包含纯虚函数的类称为抽象类,抽象类不能被实例化,只能作为基类来派生子类。子类必须实现父类的纯虚函数才能实例化。例如: ```c++ class Shape { public: virtual void draw() = 0; }; class Circle : public Shape { public: void draw() override { std::cout << "Drawing a circle." << std::endl; } }; int main() { Shape* shape_ptr = new Circle(); shape_ptr->draw(); // 调用Circle类中重写的draw函数 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值