Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

CONSTRUCTORS,

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

Constructor is a special function defined in a


class & it is named same as the class.
Constructor function does not return any
value.

Constructor function is automatically called


when an object of that class is created.
Types of
Constructor

Default Copy
Constructor Constructor
Parameterized
Constructor

06:17 PM 06:17 PM
RULES FOR CREATING CONSTRUCTORS

•Constructor has the same name as the class.


•Constructor function does not contain any return
type specifier.
•Constructor function may receive the values
from calling function.
•Constructor must be declared as public.
•A class can have multiple constructors with
different prototypes.

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

Default Constructors are those constructors that does not receive


any argument from the calling function. In other words they assign
a predefined fix value to data members.

06:17 PM 06:17 PM
CONSTR1.CPP : AN EXAMPLE : Parameterized constructor
This function should be added in circle class created in previous examples

x, y, and r are specified when an object


is created e.g. circle c2(50,30,45);

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.

Remember whenever a parameterized constructor is


created; class must be having a default constructor for
06:17 PM 06:17 PM
the object being created without any initial value defined.
COPY CONSTRUCTOR

Copy constructor is required whenever we need to


create a new object using an existing object i.e.
when we want that new object that is being created
should be initialized on the values currently being
hold by another object.
Copy constructor’s creation involves object
passing mechanism. Only pass by reference
method is permitted while passing objects to a
constructor of the same class.

06:17 PM 06:17 PM
SYNTAX FOR COPY CONSTRUCTOR

<constructor name>(<class name> &<objectname>)


{
member= <passed object>. <member>;
:
:
:
}

An addition to the previous circle’s class: A copy constructor

circle( circle &a) : Calling from main() :


{
centerX=a.centerX; circle c2(200,160,180);
centerY=a.centerY; circle c3=c2;
radius=a.radius; c3.putdata();
calculate_area();
calculate_circumference();
}

06:17 PM 06:17 PM
PASSING OBJECTS AS ARGUMENTS

Objects can be treated as general variables while we need to


pass them to functions. These functions may be member
function within a class or they may be the stand alone
functions.
return datatype <function_name>(<class_name>
[&]<object_name>)

{
& is used when we want to
function definition; pass the argument by
reference instead of
[return <value|object>;] passing by value.

}A function can also return the object. To do so


function receiving the return value must have
object of the same class as return object at the left
hand side06:17 PM 06:17call.
of function PM
AN EXAMPLE : PASSING OBJECT TO FUNCTIONS

circle addcircles( circle c2) Function to add the


{ members of two
circle c3; objects in a third
c3.centerX=centerX+ c2.centerX; object and then to
c3.centerY=centerY+ c2.centerY ; calculate the area and
c3.radius=radius+c2.radius; circumference. After
c3.calculate_area(); performing operations
c3.calculate_circumference(); returns the third object
return c3; to calling function.
}

: Calling from main() function :

circle c1(100,80,90),c2(200,160,180),c3;
c3=c1.addcircles(c2);
c3.putdata();

Function is returning the


object of06:17
06:17 PM classPMcircle
DESTRUCTOR

Destructor is a special member function that is just


opposite of constructor. A destructor is called
when an object is destroyed. If we need any work
be performed when an object is being destroyed
we can write all the program statement within a
destructor function.
The name of destructor function is the same as the
constructor but it follows a ~ (tilde) character.
Destructor does not accept or return any value.

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

BASE CLASS BASE CLASS


1 2

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>

const int LEN=25;


class Person
{
char name[LEN];
int age;

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";
}
};

class Student: public Person


{
int rollno;
float average;
public:
void readstudent(void)
{
readperson();
cout<<"Enter RollNo";
cin>>rollno;
cout<<"Enter Average Marks:";
cin>>average;
} 06:17 PM 06:17 PM
AN EXAMPLE : MULTILEVEL INHERITANCE : PART 3/4

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

class admission: student,course


{
date adm_date;
public:

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

You might also like