C++ Composition

October 27, 2015 Posted by WithU Technologies
As we know that each and every object can be separated by
their parts and to make them complete their parts are composed with each other.

For example, a living being like human being have body parts
like head, hand, legs etc. when they are composed within a single frame, they
create a complete human body. As like a mobile phone also have different parts
like battery, camera, sensors, display etc. which are composed together to
create a complete mobile phone.

Thus this process of composition is also applied in programming. Where the different classes are separated in different files with its own header and source file. And later they are composed together within a single class file or parent class file to execute the program from main function.

Let’s take an example of a person who have a Name, Date of Birth and Gender as like we all have:

#include <iostream>
using namespace std;

class BirthDate{   //Class for Birthday Input and Output

public:

BirthDate(int d, int m, int y)   //Constructor with three parameters

:date(d),month(m), year(y)      //Member initializer list is used

{

}

void DisplayDate(){  //Function to Display Birthday

      cout<<"BirthDay: "<<date<<"/"<<month<<"/"<<year<<endl;

}

private:

int date;  

int month;

int year;

};

class Gender{    //Class for Gender Input and Output

public:

      Gender(char s) //Constructor with one parameter

    :sex(s)        //Member initializer     

    {

      }

      void DisplayGender(){  //Function to Output the Gender

            cout<<"Gender:
"
<<sex<<endl; 

      }

private:

      char sex;  

};

class person{        //Main person class where we compose all the other classes

public:

      person(std::string n,BirthDate b, Gender s)  //Constructor with three parameters

   :name(n),bd(b),se(s)    //Member initializer list

    {

      }

      void DisplayOutput(){  //Function to output the name along with the output for other classes 

            cout<<"Full
Name: "
<<name<<endl;   //Output name

            bd.DisplayDate();     //Call the function to Output BirthDay

            se.DisplayGender();    //Call the function to Output Gender

      }

private:

    std::string name;

    BirthDate bd;

    Gender se;

};

int main() {       // main function

       int d,m,y;

                        cout<<"Enter your Date of birth: "<<endl;

                        cin>>d;

                        cout<<"Enter your Month of Birth: "<<endl;

                        cin>>m;

                        cout<<"Enter your Year of Birth: "<<endl;

                        cin>>y;

                  BirthDate birth (d,m,y); //After input it passes three parameters to the class                 

               char s;

                                cout<<"Enter
your Gender: (In M or F) "
<<endl;

                                cin>>s;

                                Gender sex(s);  //After input it passes one parameter to its respective class

                             

      string fn, ln, full;

                              cout<<"Enter your First Name: "<<endl;

                              cin>>fn;

                              cout<<"Enter your Last Name: "<<endl;

                              cin>>ln;

                              full=fn+" "+ln;   //Concatenate the First name and Last name

                              

   person name(full,birth,sex);   //It passes the all three parameters

      name.DisplayOutput();          //It calls the DisplayOutput() function

}

/*

Output:

Enter your Date of birth:

08

Enter your Month of Birth:

11

Enter your Year of Birth:

1993

Enter your Gender: (In M or F)

M

Enter your First Name:

RJ

Enter your Last Name:

Chakraborty

Full Name: RJ Chakraborty

BirthDay: 8/11/1993

Gender: M

 */

·
Here we declare three separate classes. For the sake of simplicity, we declare all three within a single file. But you can declare them using separate class files with header and source implementation.

·
BirthDate class is initialized by a Constructor with three parameters. Then we use a Member initializer list to declare the date, month and year as integer variable along with their values which are initialised within the private class.

Then we use a void function DisplayDate to Output BirthDay in a format.

·
Other two classes are also declared and displayed by the same process.

·
At last in person class we declare the name as string variable and display the name taken from user along with the Birthday and Gender. Here we use the BirthDate and Gender Class as parameters of person class.

·
Individual class can be kept relatively simple and straightforward; thus the programmer can focus on each class separately.