Constructors, Destructors & Inheritance
Constructors, Destructors & Inheritance
DESTRUCTORS
&
INHERITANCE
by: Naresh Kumar PGT(CS)
K.V.No1, AFS , Bhuj
06:17 PM 06:17 PM
Objective
• Introduction.
• Constructors
– Types of Constructors
– Characteristics of Constructors
• Destructors
– Need for destructors
– Characteristics of destructors
• Inheritance
– Types of inheritance
06:17 PM 06:17 PM
CONSTRUCTORS
Default Copy
Constructor Constructor
Parameterized
Constructor
06:17 PM 06:17 PM
RULES FOR CREATING CONSTRUCTORS
06:17 PM 06:17 PM
CONSTR1.CPP : AN EXAMPLE : Default constructor
public:
circle(void)
{ Call to functions
centerX=80; calculate_area() &
centerY=40; calculate_circumference()
radius=50; functions to initialize the
calculate_area(); values of area and
calculate_circumference(); circumference data
} members as according to
given centerX, centerY &
This function should be added in radius data members.
circle class created in previous
examples
06:17 PM 06:17 PM
CONSTR1.CPP : AN EXAMPLE : Parameterized constructor
This function should be added in circle class created in previous examples
public:
circle(int x, int y, int r) Call to functions
{ calculate_area() &
centerX=x; calculate_circumference()
centerY=y; functions to initialize the
radius=r; values of area and
calculate_area(); circumference data
calculate_circumference(); members as according to
} given centerX, centerY &
radius data members.
06:17 PM 06:17 PM
SYNTAX FOR COPY CONSTRUCTOR
06:17 PM 06:17 PM
PASSING OBJECTS AS ARGUMENTS
{
& is used when we want to
function definition; pass the argument by
reference instead of
[return <value|object>;] passing by value.
circle c1(100,80,90),c2(200,160,180),c3;
c3=c1.addcircles(c2);
c3.putdata();
06:17 PM 06:17 PM
DESTRUCTOR : SYNTAX
~<class name>()
{
destructor definition;
06:17 PM 06:17 PM
TYPES OF INHERITANCE
SINGLE INHERITANCE
BASE CLASS
DERIVED
CLASS
06:17 PM 06:17 PM
TYPES OF INHERITANCE
MULTIPLE INHERITANCE
DERIVED
CLASS
06:17 PM 06:17 PM
TYPES OF INHERITANCE
HIERARCHICAL INHERITANCE
BASE CLASS
DERIVED DERIVED
CLASS 1 CLASS 1
06:17 PM 06:17 PM
TYPES OF INHERITANCE
MULTILEVEL INHERITANCE
BASE CLASS
1
BASE/DERIVED
CLASS 2
DERIVED
CLASS
06:17 PM 06:17 PM
TYPES OF INHERITANCE
HYBRID INHERITANCE
BASE CLASS
1
BASE/DERIVED DERIVED
DERIVED CLASS 2 CLASS 1
CLASS 1
DERIVED
CLASS
06:17 PM 06:17 PM
AN EXAMPLE : MULTILEVEL INHERITANCE : PART 1/4
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
public:
void readperson(void)
{
cout<<"Enter Name of the Person:";
gets(name);
cout<<"Enter Age:";
cin>>age;
}
06:17 PM 06:17 PM
AN EXAMPLE : MULTILEVEL INHERITANCE : PART 2/4
void displayperson(void)
{
cout<< "Name:";
cout<<name;
cout<<"\nAge:"<<age<<"\n";
}
};
void displaystudent(void)
{
displayperson();
cout<<"RollNo: "<<rollno<<"\n";
cout<<"Average Marks:"<< average<<"\n";
}
};
class GradStudent: public Student
{
char subject[LEN];
char working;
public:
void readIt(void)
{
readstudent();
cout<<"Enter Main Subject: ";
gets(subject);
cout<<" Working? (Y/N): ";
cin>> working;
06:17 PM 06:17 PM
}
AN EXAMPLE : MULTILEVEL INHERITANCE : PART 4 /4
void displayIt(void)
{
displaystudent();
cout<< "Subject: "<< subject<<"\n";
cout<< "Working: "<< working<<"\n";
}
};
void main()
{
clrscr();
GradStudent grad;
grad.readIt();
grad.displayIt();
}
06:17 PM 06:17 PM
AN EXAMPLE : MULTIPLE INHERITANCE : PART 1/5
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class student
{
char name[20];
char sex;
int age;
public:
void readstudent(void)
{
cout<<"\nEnter the Name :";
gets(name);
cout<<"\nSex :";
cin>>sex;
cout<<"\nAge :";
cin>>age;
}
06:17 PM 06:17 PM
AN EXAMPLE : MULTIPLE INHERITANCE : PART 2/5
void displaystudent(void)
{
cout<<"\nName :"<< name;
cout<<"\nSex :"<<sex;
cout<<"\nAge :"<< age;
}
};
class course
{
char title[20];
int duration;
public:
void readcourse(void)
{
cout<<"\nEnter Course Title :";
gets(title);
cout<<"\nDuration :";
cin>>duration;
} 06:17 PM 06:17 PM
AN EXAMPLE : MULTIPLE INHERITANCE : PART 3/5
void displaycourse(void)
{
cout<<"\nCourse Title :"<< title;
cout<<"\nDuration :"<<duration;
}
};
struct date
{
int dd;
int mm;
int yy;
};
06:17 PM 06:17 PM
AN EXAMPLE : MULTIPLE INHERITANCE : PART 4 /5
void readadmission(void)
{
readstudent();
readcourse();
cout<< "\nEnter the date of admission :";
cin>> adm_date.dd;
cin>>adm_date.mm;
cin>>adm_date.yy;
}
void displayadmission(void)
{
displaystudent();
displaycourse();
cout<<"\nAdmission Date :”
cout<<adm_date.dd<<"/"<<adm_date.mm<<"/"<<adm_date.yy;
} 06:17 PM 06:17 PM
};
AN EXAMPLE : MULTIPLE INHERITANCE : PART 5 /5
void main()
{
admission adm;
clrscr();
cout<<"\n********************* ADMISSION DETAILS ENTRY ********************";
adm.readadmission();
cout<<"\n********************* ADMISSION DETAILS ********************";
adm.displayadmission();
}
06:17 PM 06:17 PM