CONSTRUCTOR
·
Special
member function for automatic initialization of an object
·
It
executed automatically whenever an object is created
·
Its
name is the same as the class name
Syntax
class <class_name>
{
private:
public:
<class_name(<parameter>)
{
<statements>
}
protected:
};
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class exam
{
private:
int sno,mark1,mark2;
public:
exam()
{
sno=mark1,mark2=0;
}
void showdata()
{
cout<<"S.no="<<sno<<"\t
Mark 1="<<mark1<<"\t Mark
2="<<mark2<<endl;
}
void getdata()
{
cout<<"\n Enter No=";
cin>>sno;
cout<<"\n Enter two Marks=";
cin>>mark1>>mark2;
}
};
void main()
{
clrscr();
exam e;
e.showdata();
e.getdata();
e.showdata();
getch();
}
DESTRUCTORS
·
It is
used to destroy the objects that have been created by a constructor
·
It is
a member function whose name is the same as the class name but is preceded by a
tilde
·
It
never takes any arguments nor does it return any value
·
It
will be invoked implicitly at the time of program closing
·
It is
used to release memory space that occupied by objects
Syntax
<~class_name()
{
<Statements of destructor>
}
Example
#include<iostream.h>
#include<conio.h>
class des
{
int m,n;
public:
void calculate();
des()
{
m=0;
n=0;
}
~des()
{
cout<<"Destructor function calling";
}
};
void des :: calculate()
{
m=m+1;
n=n+1;
cout<<m<<"\t"<<n;
}
void main()
{
clrscr();
des s;
s.calculate();
getch();
}
INHERITANCE
·
Creating
a new class from an old class
·
Old
class – Base class
·
Derived
class – Subclass
·
Private,
Public, Protected are very important. We only protected or public members of
one class to other class, we can’t inherit private members.
o
Public
– It is visible to all the classes
o
Protected - It
is visible to it own class and it derived class
Types of inheritance
·
Single
Inheritance
·
Multiple
Inheritance
·
Hierarchical
Inheritance
·
Multilevel
Inheritance
·
Hybrid
Inheritance
Single Inheritance
Syntax
class derived-class-name-B :
<access-specifier> base-class-name-A
{
members of the class
}
Multiple Inheritance
Syntax
class derived-class-name-C : <access-specifier>
base-class-name-A,
<access-specifier> base-class-name-B
{
members of the class
}
Hierarchical Inheritance
Syntax
class derived-class-name-B : <access-specifier> base-class-name-A
{
members of the class
}
class derived-class-name-C : <access-specifier>
base-class-name-A
{
members of the class
}
class derived-class-name-D : <access-specifier>
base-class-name-A
{
members of the class
}
Multilevel Inheritance
Syntax
class derived-class-name-B : <access-specifier>
base-class-name-A
{
members of the class
}
class derived-class-name-C : <access-specifier>
base-class-name-B
{
members of the class
}
Hybrid Inheritance
Syntax
class derived-class-name-B : <access-specifier>
base-class-name-A
{
members of the class
}
class derived-class-name-C : <access-specifier>
base-class-name-A
{
members of the class
}
class derived-class-name-D <access-specifier>
base-class-name-B, access-
specifier> base-class-name-C
{
members of the class
}

No comments:
Post a Comment