Question

In: Computer Science

Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container...

Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container Classes.

Solutions

Expert Solution

SOLUTION :

In this example Car class is the Container class which contains engine thus engine is the Composed class.

Constructors are created for both the classes and composed object variable is declared in the container class Car.

When the container class is called from the main method(invoking constructos by creating object of Car),it is observed that the constructor of Composed class Engine is executed first and then the constructor of Container class is executed.

PROGRAM :

#include <iostream>
using namespace std;

// composed object class
class Engine {
public:
Engine() {
cout<<"Constructor of composed class Engine\n";
  
}
~Engine() {
cout<<"Destructor of Engine class\n";
  
}
};

// container class
class Car{
private:
// e is the composed object of Composed class Engine
   Engine e;
public:
   Car() {
   cout<<"Constructor of container class Car\n";
  
   }  
   ~Car() {
   cout<<"Destructor of Car class\n";
  
   }
};

int main() {
// creating object for container class Car
   Car c;
   return 0;
}

OUTPUT :


Related Solutions

Using C Write a program that will serve as a simple shell. This shell will execute...
Using C Write a program that will serve as a simple shell. This shell will execute an infinite for loop. In each iteration of the loop, the user will be presented with a prompt. When the user enters a command, the shell will tokenize the command, create a child process to execute it and wait for the child process to be over. If the user enters an invalid command, the shell should recognize the situation and show a meaningful message....
Code a simple Editor class with 2 constructors (one default, one with a string for a...
Code a simple Editor class with 2 constructors (one default, one with a string for a file name) The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class) create a short file - at least 5 lines - could be a program main: string fileName("input.txt"); Editor miniVi (fileName); miniVi.displayLines(); C++ explain in detail plz Thank you
Write a simple code for a simple connect the dots game in plain C coding.
Write a simple code for a simple connect the dots game in plain C coding.
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
Write a simple shell in C language and show some outputs.
Write a simple shell in C language and show some outputs.
Write a code for simple racing game (using dots) on c coding.
Write a code for simple racing game (using dots) on c coding.
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
Write a C++ code to print to the user a simple menu of a fast food...
Write a C++ code to print to the user a simple menu of a fast food restaurant. You should allow the user to select his/her preferred burgers and/or drinks and you should display the final bill to the user for payment. The user should be able to select more than one item. You should use the switch statement.
Write the simple shell in C language. Please show some outputs.
Write the simple shell in C language. Please show some outputs.
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT