如何运行抽象类函数和继承函数?

时间:2018-02-07 19:11:35

标签: c++ class inheritance abstract

我刚刚学习了抽象类,但我不太了解。是否可以一次运行抽象类函数和继承函数?..

例如,

class Animals
{
 public:
    virtual void Display() = 0;
};

class Dog : public Animals
{
    void Display()
    {
        cout << "This is Dog!" << endl;
};

class Cat : public Animals
{
    void Display()
    {
        cout << "This is Cat!" << endl;
    }
};

我有另一个名为Zoo的类,它将在类Animal中运行抽象函数

class Zoo : public Animals
{
   Animals* animal;
   animal->Display();
}

我想要的输出是

This is Dog!
This is Cat!

当我运行它时,它有错误..有没有其他方法来获得此输出?谢谢:))

3 个答案:

答案 0 :(得分:0)

首先,出现语法错误:

 class Animals
{
 public:
    virtual void Display() = 0;
};

class Dog : public Animals
{
    void Display()
    {
        cout << "This is Dog!" << endl;
    }
};

class Cat : public Animals
{
    void Display()
    {
        cout << "This is Cat!" << endl;
    }
};

然后,如果要创建Dog和Cat实例,可以为这些类调用新的运算符:

class Zoo : public Animals
{
   void Display() 
   {
       Animals* animal1;
       animal1 = new Cat();
       animal1->Display();
       delete animal1;

       Animals* animal2;
       animal2 = new Dog();
       animal2->Display();
       delete animal2;
   }

}

这应该得到你想要的输出。

答案 1 :(得分:0)

  

有没有办法只调用基类并运行继承的类?

我想你想要这个:

#include <iostream>
#include <memory>

class Animals
{
public:
    virtual void Display() = 0;
    virtual ~Animals() = default;
};

class Dog : public Animals
{
    void Display() override
    {
        std::cout << "This is Dog!" << std::endl;
    }
};

class Cat : public Animals
{
    void Display() override
    {
        std::cout << "This is Cat!" << std::endl;
    }
};

class Goat : public Animals
{
    void Display() override
    {
        std::cout << "This is Goat!" << std::endl;
    }
};

int main()
{
    Animals* animal = new Dog;
    animal->Display();
    delete animal; // delete at some point to avoid memory leak as Dog is allocated on the heap

    Cat cat;
    animal = &cat;
    animal->Display(); // no delete needed, as Cat is allocated on the stack and will cleared when goes out of scope

    std::unique_ptr<Animals> animal2 = std::make_unique<Goat>();
    animal2->Display(); // no delete needed, Goat is allocated on the heap but will be cleared when goes out of scope

    return 0;
}

https://ideone.com/yoVt4G

std::unique_ptr投入混合种类

答案 2 :(得分:0)

animal->Display();导致未定义的行为,因为animal未初始化,因此首先将其初始化为

    Cat my_cat;
    Animals* animal = &my_cat;
    animal->Display();

OR

 Animals* animal = new Cat();
 animal->Display();
 ....
 delete  animal; 

这是您的代码,注释中有解释。

class Animals {
        public:
                virtual void Display() = 0;/*its a PVF , in every derived class it should be re-defined */
};

class Dog : public Animals {
        void Display() {
                cout << "This is Dog!" << endl;
        };
};
class Cat : public Animals {
        void Display() {
                cout << "This is Cat!" << endl;
        }
};

class Zoo : public Animals {
        public :
                void Display() {
                        #if 1
                        Dog my_dog;
                        Animals *animal = &my_dog; /** Display() of Animal class will be invoked  bcz Display() is declared as  virtual */
                        animal->Display();
                        #endif
                        #if 0
                        Animals* animal = new Cat(); /** Display() of Cat class  eill be invoked */
                        animal->Display();
                        delete animal;
                        #endif
                }
};
int main() {
        Zoo my_zoo;
        my_zoo.Display();
        return 0;
}

我希望它可以帮到你。