Inheritace Constructors
Inheritace Constructors
Constructor: A constructor is a special member function that is used to initialize the objects.
Syntax
class classname
{
private :
data members;
public :
constructorname(argument list)
{
statements;
}
};
Example: Program to display two numbers.
#include<iostream.h>
#include<conio.h>
class example
{
private:
int a,b;
public:
example()
{
a=10;
b=20;
}
void display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
};
void main()
{
clrscr();
example e;
e.display();
getch();
}
Output:
Types of constructors
1. Default constructor
2. Parameterized Constructor
3. Copy constructor
Default constructor
A constructor which does not accept any arguments is called a zero-
argument constructor or default constructor.
Example: Program to display two numbers.
#include<iostream.h>
#include<conio.h>
class example
{
private:
int a,b;
public:
example()
{
a=10;
b=20;
}
void display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
};
void main()
{
clrscr();
example e;
e.display();
getch();
}
Output:
If different objects have to be initialized with different values, it cannot be done using
default constructor
PARAMETERISED CONSTRUCTORS
A constructor that takes one or more arguments is called parameterised constructors.
Example: Program to display two numbers.
#include<iostream.h>
#include<conio.h>
class example
{
private:
int a,b;
public:
example(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
};
void main()
{
clrscr();
example e(10,20);
e.display();
getch();
}
Output:
2. Implicit call
An implicit call means the declaration of the object is followed by
argument list enclosed in parenthesis.
Syntax
classname objectname(arg1,arg2,…..);
Example:
num ob1(10,20); //Implicit call
3. initialisation USING ASSIGNMENT OPERATOR
This is used for the constructor with exactly one argument. Declaration is
followed by assignment operator and the value to be initialised.
Syntax
classname objectname = Initial value;
Example:
num ob1=100;
COPY CONSTRUCTOR
Copy constructor is a parameterized constructor using which one object can be copied to
another object .
Example
x a1;
x a2=a1;
Constructor overloading
Constructors with same name but different number or type of arguments.
Destructors
A destructor is a special member function that will be executed automatically when an object
is destroyed.
Like constructor, it has the same name as that of the class but preceded by a tilde (~)
Syntax :
class class name
{
private: data members;
public: constructorname();
~destructorname();
};
Example:
class example
{
private:
int a,b;
public:
example()
{
a=10;
b=20;
}
~ example()
};
FEATURES
1. The destructor’s name is the same as that of class.
2. They take no arguments.
3. It is declared under public.
4. Destructors can never return a value.
5. There can be only one destructor in each class.
Chapter 10 : INHERITANCE
Inheritance is the mechanism by which one class acquires the properties of another class.
Base class is the class whose properties are inherited by another class. It is also called as
super class.
Derived class is the class that inherits the properties from base class. It is also called as sub
class.
Advantages
Reusing existing code
Faster development time
Easy to maintain
Easy to extend
Memory utilization
Syntax
class derivedclassname : visibilitymode baseclassname
{
Members of the derived class
};
Access specifiers or visibility mode
The visibility mode defines whether the features of the base class are privately
derived or publicly derived or derived on a protected mode. The visibility mode basically
controls the access specifier of the properties inherited from base class in the derived class.
Derived class
Base class
Public mode Private mode Protected mode
Public Public Private Protected mode
Private Not inheritable Not inheritable Not inheritable
Protected Protected Private Protected mode
Public Inheritance
The public members of the base class become the public members of the derived
class.
The protected members of the base class remain protected members of the derived
class.
The private members of the base class are not inheritable.
Private Inheritance
• The public members of the base class become the private members of the derived
class.
• The protected members of a base class become the private members of the derived
class.
• The private members are not inherited by the derived class.
Public Inheritance
• The public members of the base class become the protected members of the derived
class.
• The protected members of the base class remain protected members of the derived
class.
• The private members are not inherited by the derived class.
TYPES OF INHERITANCE
1. Single Inheritance
If a class is derived from a single base class, it is called as single inheritance.
Base Father
2. Multilevel Inheritance
The classes can also be derived from the classes that are already derived. This type of
inheritance is called multilevel inheritance.
Base Grand Father
Derived class-n
3. Multiple Inheritance
If a class is derived from more than one base class, it is known as multiple inheritance.
Prince
Derived class
4.Hierarchical Inheritance
If a number of classes are derived from a single base class, it is called as hierarchical
inheritance.
Staff
Base
Derived class Derived class Derived class Lecturers Office Staff Group-D
5. Hybrid Inheritance
Derivation of a class involving more than one form of inheritance is known as hybrid
inheritance.
Example for Single level Inheritance.
Create a base class containing the data members roll number and name. Also
create a member function to read and display the data using the concept of
single level inheritance. Create a derived class that contains marks of two subjects
and total marks as the data members.
#include<iostream.h>
#include<conio.h>
class student
{
private:
char name[20];
int regno;
public:
void getdata()
{
cout<<"Enter your name"<<endl;
cin>>name;
cout<<"Enter your Register Number"<<endl;
cin>>regno;
}
void putdata()
{
cout<<"Name="<<name<<endl;
cout<<"Register Number="<<regno<<endl;
}
};
class marks:public student
{
private:
double m1,m2,total;
public:
void getdata2()
{
cout<<"Enter the marks of subject1 and subject2"<<endl;
cin>>m1>>m2;
total=m1+m2;
}
void putdata2()
{
cout<<"Subject1 mark="<<m1<<endl;
cout<<"Subject2 mark="<<m2<<endl;
cout<<"Total Marks="<<total<<endl;
}
};
void main()
{
clrscr();
marks m;
m.getdata();
m.getdata2();
m.putdata();
m.putdata2();
getch();
}
Output
When two or more objects are derived from a common base class, we can prevent
multiple copies of the base class being present in an object derived from those objects by
declaring the base class as virtual when it is being inherited. Such a base class is known as
virtual base class.
Example
class A
{
————
————
};
class B: virtual public A
{
————
————
};
class C: virtual public A
{
————
————
};
class D: public B, public C
{
————
————
};
Abstract classes
An abstract class is one that is not used to create objects. An abstract class is designed only
to act as a base class.