如何运行抽象class函数和继承的函数?
How to run abstract class functions and the inherited functions?
刚学了abstractclass,但不是很懂。是否有可能 运行 抽象 class 函数和继承函数一次全部?..
例如,
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 的 class,它将 运行 class Animals
中的抽象函数
class Zoo : public Animals
{
Animals* animal;
animal->Display();
}
我想要的输出是
This is Dog!
This is Cat!
当我运行这个时,它有错误..还有其他方法可以得到这个输出吗?谢谢:)
首先,有一个语法错误:
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;
}
}
这应该会得到您想要的输出。
Is there a way to only call the base class and run the inherited classes too?
我想你想要这个:
#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;
}
加入 std::unique_ptr
以增加多样性
animal->Display();
导致未定义行为,因为 animal
未初始化,因此首先将其初始化为
Cat my_cat;
Animals* animal = &my_cat;
animal->Display();
或
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;
}
希望对你有帮助。
刚学了abstractclass,但不是很懂。是否有可能 运行 抽象 class 函数和继承函数一次全部?..
例如,
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 的 class,它将 运行 class Animals
中的抽象函数class Zoo : public Animals
{
Animals* animal;
animal->Display();
}
我想要的输出是
This is Dog!
This is Cat!
当我运行这个时,它有错误..还有其他方法可以得到这个输出吗?谢谢:)
首先,有一个语法错误:
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;
}
}
这应该会得到您想要的输出。
Is there a way to only call the base class and run the inherited classes too?
我想你想要这个:
#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;
}
加入 std::unique_ptr
以增加多样性
animal->Display();
导致未定义行为,因为 animal
未初始化,因此首先将其初始化为
Cat my_cat;
Animals* animal = &my_cat;
animal->Display();
或
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;
}
希望对你有帮助。