nested classesmany a times it becomes essential


Nested Classes

Many a times, it becomes essential to have a class contain properties of two other classes. One way is to explain a class within another - that is a class with member classes also known as nested classes. This has nothing to do with inheritance. Another way is multiple inheritance , which will be discussed later.

e.g.

                class Aclass

                 {

                  public :

                                                Aclass(int pv)

                                                 {

                                                                private_variable_A = pv;

                                                 }

  private :

                                                int private_variable_A;

                                  };

 

                class Bclass

                 {

                  public :

                                                Bclass(int bpv, int apv): Aobj(apv)

                                                 {

                                                                private_variable_B = bpv;

                                                 }

                  private :

                                                int private_variable_B;

                                                Aclass Aobj;        // Declaring an object here.

                  };

 

As can be seen , the class Bclass have an object Aobj in its private section as one of its members. Also, it have a constructor function with the similar name, to which the two variables passed are bpv and apv. The variable bpv is used to initialize the private variable of Bclass.

Though, the constructor contains something after the colon. The part after the colon in the definition of a constructor is known as initialization section and the actual body of the constructor is known as assignment section . Initialization section initializes the base class members, while assignment section contains statements to initialize the derived class members.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: nested classesmany a times it becomes essential
Reference No:- TGS0309449

Expected delivery within 24 Hours