Loading....
Coupon Accepted Successfully!

 

Object As a Class Member

Properties of one class can be used in another class using inheritance or using the object of a class as a member in another class. Declaring the object as a class data member in another class is also known as delegation. When a class has an object of another class as its member, such a class is known as a container class.
 
In inheritance, the derived class can use members of the base class. Here, the derived class is a kind of base class. The programmer can also add new members to the derived class.
In delegation, the class consists of objects from other classes. The composed class uses the properties of other classes through their objects. This kind of a relationship is known as has-a-relationship or containership.
 

 
11.25 Write a program use object of one class in another class as a member.
 
OUTPUT
Constructor of class I
Constructor of class II
x= 20 k = 30
 
Explanation: In the above program, the class II contains integer k and object y of class I. In function main(), object ii is an object of class II. The constructor of class I is executed first, because when the compiler reaches the class II, it finds an object of class I. We know that the object declaration always executes the constructor of that class. Thus, the object of class I is declared, and the constructor of class I is executed. The constructor of class II is executed, and the variable of class II is initialized. The member function show()displays the contents of x and k. The contents of class I member is obtained from object y. The dot (.) operator is used to access the elements of class I.

11.26 Write a program to access member variable of base class using object, scope access operator and direct.
 

OUTPUT
Name : Ajay
Age : 21
Access Using object of the class
Name : Amit
Age : 20
Access Using direct member variables
Name : Arun
Age : 19
Height : 5.5
Weight: 31
Display using Scope Access operator
Name : Arun
Age : 19
Display Using object of the class
Name : Amit
Age : 20
Access Using direct member variables
Name : Arun
Age : 19
Height: 5.5
Weight: 31

Explanation: In the above program, A1 and A2 are two classes. The class A2 is derived from the class A1. The class A1 has two public member variables. The class A2 has three private members; one of the members is an object of class A1, that is, object a. Theobject a holds its separate set of member variables of class A1. There are three ways of accessing the member variables of base class A1. Table describes the status of access of the member variables of class A1 and the derived class A2 when we use access specifiers such as private, public, and protected.

Table: Access Specifiers

 

Access (using)

Public

Private

Protected

Scope Access :: operator

Possible

Not Possible

Possible

Object

Possible

Not Possible

Not Possible

Direct

Possible

Not Possible

Possible

  1. The first way is to use scope access operators as per the following statements:
     
     
    In the above statements, A1 is a class name, and name and age are member variables of class A1. When an access specifier is public or protected, the above statements are valid.
  2. The second method is to use an object of the base class, as per the following statements:
     
     
    In the above statement, the member variables of class A1 are accessed using an object of the same class, who is the member of the derived class. This is possible only when the member variables of class A1 are public and is not possible if the member variables of class A1 are protected or private.
  3. The third method directly uses the member variables. Direct access is possible when the access specifier is public or protected, as per the following statements:
     
    cin>>name;
     
    cin>>age;
11.27 Write a program to derive a class from two base classes. Use objects of both the classes as member variables for derived class. Initialize and display the contents of classes using constructor and destructor.
 

OUTPUT
a1=65 b1=66

Explanation: In the above program, classes A and B are declared with one integer each. The class AB is declared, which contains objects of classes A and B as member variables. The constructor of class AB initializes the member variables of classes A and B. The constructor also displays contents on the screen. Finally, the destructor destroys the object.





Test Your Skills Now!
Take a Quiz now
Reviewer Name