Use one class method that implements other class methods as well as your own

I am programming in C ++ and I have 3 classes that inherit from each other:

Hatchback -> Car -> Vehicle;

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

Each class has a printing 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 initializes 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 that I want to print all the attributes using one printing method, so when I call,

object.print();

it goes through the vehicle printing method, then the car printing method, then the Hatchback printing method. A.

, , . , ... , !

, , , , Vehicle, Container, Tanker, Flatbed, Motorbike, Transit ETC ETC, , .

Print Vehicle !

, .

, , , , . ​​

! , Noob... !:)

+3
3

, , , :

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

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

Hatchback:: print() Car:: print(), Vehicle:: Print()

+3

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

+2
source

All Articles