C++ nested classes or member classes

C++ Nested classes or member classes

Inheritance is the mechanism of derived certain properties of one class into another. The c++ language supports yet another way of inheriting properties of one class into another. That is a class can contain objects of other classes as its members as shown below example

class alpha
{
……
……
};
class beta
{
………
………
};
class gamma
{
alpha a; // a is an object of class alpha
beta b; // b is an object of class beta
…….
…….
};

all objects of gamma class will contain the object a and b. This kind of relationship is called nesting. creation of an object that contains the another object is very different than the create of an independent object. An independent object is created by its constructor whenever it is declared with arguments. on the other hand, a nested object is created in two stages, First the member object are created using their respective constructors and then the other ordinary members are created. This means constructors of all the member object should be called before its own constructor body is executed. This is accomplished using an initialization list in the constructor of the nested class.

Example
class gamma
{
………
alpha a; // a is object of alpha class
beta b; // b is object of beta class <
public:
gamma(arglist):a(arglist1),b(arglist2)
{
//constructor body
}
};

arglist is the list of arguments that is to be supplied when a gamma object is defined. These parameters are used for initialization the members of gamma. arglist1 variable is the argument list for the constructor of a and variable arglist2 is the argument list for the constructor of b. arglist1 and arglist2 may or may not use the arguments from arglist. Remember a(arglist1) and b(arglist2) are function calls and therefore the arguments do not contain the data types. They are simply variables or constants.

Example gamma(int x,int y, float z):a(x), b(x,z)
{
assignment section(for ordinary other members)
}

we can use as many member objects as are required in a class. for each member object we add a constructor call in the initializer list. The constructor of the member object are called in the order in which they are declared in the nested class.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise