Classes
Contents
Composition
We are able to define a class to represent a concept. In certain situations we would like one class to contain an object of another class.File: car1.cpp
#include <iostream> using namespace std ; class engine { public: int cyclinders ; engine() { cout << "Engine constructor." << endl ; cyclinders = 4 ; } engine(int cyclindersP ) { cout << "Engine constructor with an argument." << endl ; cyclinders = cyclindersP ; } }; class car { public: engine engineObj ; }; int main() { car carObject ; cout << "Cylinders : " << carObject.engineObj.cyclinders << endl ; return ( 1 ) ; }We are able to create a car object in the above example and that creates an engine object using the engine's default constructor. What if we wanted to construct the engine object with the constructor that takes an argument ? This is where initialization lists come in.