C Note Part3
C Note Part3
For the multiple-inheritance, the constructors of the base class are executed in
the order of inheritance in which they are coded. That means, for the
inheritance as coded below, the constructor of the class B is executed at first,
then the constructor of class C, and finally of the derived class A.
Order of Inheritance:
Class A: public B, public C
{
//body of class A
};
Destructors in inheritance:
Destructors are executed in the opposite order of that of constructors. That
means, the constructor of the derived class is executed at first and then the
constructors of its base classes in order.
1
By: Manoj Sapkota, United Technical College
Example program- 38: Create a class called Person with suitable data
members to represent their name and age. Use member functions to initialize
and display these information. Derive Student and Employee from the Person
class with their unique features. Initialize objects of these classes using
constructor and display the information.
Solution:
2
By: Manoj Sapkota, United Technical College
3
By: Manoj Sapkota, United Technical College
Output:
Displaying student info..
Name: Neha
Age: 20
RollNos: 15
Displaying employee info..
Name: Aashish
Age: 25
ID: 101
Example program- 39: Create a class Person with data members Name, Age
and Address. Create another class Teacher with data members Qualification
and Department. Also create another class Student with data members
Program and Semester. Both classes are inherited from the class Person. Every
class has at least one constructor which uses base class constructor. Create a
member function showData( ) in each to display the information of the class
member. Create objects of the derived classes and display the information.
Solution:
4
By: Manoj Sapkota, United Technical College
5
By: Manoj Sapkota, United Technical College
Output:
Displaying Teacher info..
Name: Manoz
Age: 30
Address: Bharatpur
Qualification: Masters
Example program- 40: The following figure shows the minimum information
required for each class. Write a program by realizing the necessary member
functions to read and display information of individual object. Every class
should contain at least one constructor and should be inherited to other
classes as well.
6
By: Manoj Sapkota, United Technical College
Solution:
7
By: Manoj Sapkota, United Technical College
8
By: Manoj Sapkota, United Technical College
Output:
College Name: Utech
Location: Bharatpur
Student Name: Kiran
Roll no: 12
Teacher Name: Manoz
Code: 105
Book Name: C++
Write Name: Robert
Code: 1010
The object pointer can be used to access the members of a class. The
arrow operator (->) is used for this purpose instead of dot(.) operator.
For example:
ptr -> display( );
It calls the ‘display’ member function of class ‘Student’.
To use the dot operator (.), we have to dereference the object pointer.
For example: (*ptr).display( );
An example program that demonstrates the use of object pointer is given below.
[Example program- 41]
9
By: Manoj Sapkota, United Technical College
this pointer: A special pointer that points to the current object of the class
with the help of ‘this’ keyword is known as ‘this pointer’. The ‘this’ pointer is
passed as a hidden argument to all the non-static member function calls. It is
available as a local variable within their body and may be used to refer to the
invoking object. It is not available in static member functions and friend
functions.
10
By: Manoj Sapkota, United Technical College
11