使用一个类方法来实现其他类方法以及它自己的方法

发布于 2024-10-21 04:24:52 字数 1063 浏览 1 评论 0原文

我正在使用 C++ 进行编程,并且有 3 个相互继承的类:

Hatchback -> Car -> Vehicle;

我想使用 void print() 方法打印每个类的属性,

每个类都有一个打印方法它自己的属性。

void Vehicle::print()
{
   // Vehicle attributes
}

void Car::print()
{
   // Car attributes
}

void Hatchback::print()
{
   // Hatchback Attributes
}

我创建一个对象,在一个构造函数中初始化所有类的所有属性:

Hatchback hatchback("L880UCD", "Vauhall", "Corsa", 
                    "White", "None", "Car", "Hatchback", 
                    3, 5, "Manual", 3, 2, 1);

问题是,我想使用一种打印方法打印所有属性,因此当我调用时,

object.print();

它会先运行车辆打印方法,然后运行汽车打印方法,然后是掀背式打印方法。

我知道我可以打印 Hatchback 打印方法中的所有属性,因为一切都是继承的。如果可能的话,我想避免在一个类方法中打印所有 3 个类数据...我宁愿每个类处理它自己的方法和函数!

不仅如此,我还有很多其他类别,包括所有从车辆继承的汽车、公共汽车和卡车,以及所有从汽车、卡车和公共汽车继承的集装箱、油罐车、平板车、摩托车、运输等。

所以我不想在每个底层派生类中都编写 Print Vehicle!

如果有办法做到这一点,我将非常感谢您的回复。

我尝试过使用虚拟函数,但得出的结论是,它们只是选择在运行时使用哪种方法,而不是合并打印函数。

感谢您的阅读!抱歉,如果这是一个模糊或菜鸟问题...我是 Stack Overflow 的新手! :)

I'm Programing in C++, and I have 3 classes that inherit from each other:

Hatchback -> Car -> Vehicle;

I want to print the attributes from each class using a void print() method,

Each class has a Print method that prints its own attributes.

void Vehicle::print()
{
   // Vehicle attributes
}

void Car::print()
{
   // Car attributes
}

void Hatchback::print()
{
   // Hatchback Attributes
}

I create an object that initialises ALL attributes from ALL classes in one constructor:

Hatchback hatchback("L880UCD", "Vauhall", "Corsa", 
                    "White", "None", "Car", "Hatchback", 
                    3, 5, "Manual", 3, 2, 1);

The problem is, i want to print all the attributes using one print method, so when i call,

object.print();

it runs through the vehicle print method, then the Car print method, then the Hatchback Print method.

I Know that i COULD print all the attributes from the Hatchback print method because everythings inherited. If possible i would like to avoid printing all 3 classes data in one class method... I would rather each class deal with its OWN methods and functions!

Not only that, i have A LOT of other classes including Car, Bus and Lorry that ALL inhert from Vehicle, and Container, Tanker, Flatbed, Motorbike, Transit ETC ETC that ALL inhert from Car, Lorry and Bus.

So I don't want to write Print Vehicle in every single bottom derived class!

If there is a way to do this, i would appreciate a response very much.

I've tried using Virtual Functions but come to the conclusion that they just choose which method to use at run time rather than amalgamating the print functions.

Thanks for reading! Sorry if its a Vague or Noob Question... I'm new to Stack Overflow! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与之呼应 2024-10-28 04:24:52

如果您询问是否可以从派生类调用基类方法,那么是的,您可以:

void Vehicle::print() {
   std::cout << "Vehicle attributes" << std::endl;
}

void Car::print() {
   Vehicle::print();
   std::cout << "Car attributes" << std::endl;
}

If you are asking if you can call a base class method from a derived class, then yes, you can:

void Vehicle::print() {
   std::cout << "Vehicle attributes" << std::endl;
}

void Car::print() {
   Vehicle::print();
   std::cout << "Car attributes" << std::endl;
}
千里故人稀 2024-10-28 04:24:52

让 Hatchback::print() 调用 Car::print(),然后 Car::print() 可以调用 Vehicle::Print()

Have Hatchback::print() call Car::print() which can then call Vehicle::Print()

墨洒年华 2024-10-28 04:24:52

在 Car::print() 中打印出汽车属性,然后调用 Vehicle::print()。
在 Hatchback::print() 中打印出掀背车属性,然后调用 Car::print()
等等

In Car::print() print out the car attributes and then call Vehicle::print().
In Hatchback::print() print out the hatchback attribtues and then call Car::print()
And so on

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文