Vous êtes sur la page 1sur 16

C++ Notes Class, Constructor and Destructor , Inheritance Class XII

Features of Object Oriented Programming:


 Data Encapsulation: Wrapping up of data and functions in a single unit is known as data
encapsulation. In a class, we encapsulate the data and function together in a single unit.
 Data Hiding: Keeping the data in private area of the class to prevent it from
accidental modification (change) is known as data hiding.
 Inheritance: Inheritance is a process of creating new classes (derived classes) from
existing classes (base classes).
 Polymorphism: The process of using an operator or a function in different ways for
different set of given inputs is known as polymorphism.
 Function Overloading: Function overloading is an example of polymorphism, where
the functions having same name with different set of parameters perform different
operations.
Example:
#include<iostream.h>
void Display () Function 1 (without parameter)
{
cout<< “Hello”<<endl;
}
void Display (int N) Function 2 (One parameter)
{
for(int I=1; I<=N; I++)
cout<<I<<endl;
}
void Display (int N, int M) Function 3 (Two parameters)
{
for(int I=N; I<= M; I++)
cout<<N<<”x”<<I<<”=”<<N*I<<endl;
}
void main ( ) Call for Function 2 – Prints Number from 1 to
{ 5
int X=5, Y=10;
Display (X);
Display (X, Y); Call for Function 3 – Prints table of 5 up to 10 multiples
Display ();
}
Call for Function 1 – Prints Hello

Difference between Object Oriented Programming and Procedural Programming:


Object Oriented Programming Procedural Programming
 Emphasis on Data  Emphasis on doing things (functions)
 Follows Bottom-Up approach in program  Follows Top-Down approach in program
design design
 Data hiding feature prevents accidental  Presence of Global variables increase
change in data changes of accidental change in data
 Features like data encapsulation,  Such features are not available
polymorphism, inheritance are present

By Sunil Kumar Mobile No.: 9868583636 Page 1 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

Class & Objects


A Class is used to encapsulate data and functions in a single unit. Contents of a class are
known as members of the class, data (variable) inside the class is known as data member
and function included inside the class is known as member function. Member of the class
are private by default. (i.e. they are not accessible outside the scope of the class). An object
has the similar relationship to a class that a variable has to a data type. An object is said to
be an instance of a class, in the same way ‘YOU’ and ‘ME’ are the instances of a class
‘HUMAN’. Class may contain data as well as functions.

Class Student

Data Members int RollNo;


char Name[40]
+ Data
Encapsulation
Member void Input();
Function void Output ( );

OBJECT
OBJECT S2
S1
23 Richa Gupta
11 Amit Kumar

Abstract Class: A class with no instances (no objects) is known as an abstract class.
Concrete Class: A class having objects is known as a concrete class.
Every object has its identity, state and behavior. Identity is the property of the object, which
distinguishes the object from other object. The operations/functions associated with the
object exhibit the behavior of the object.
A class has three visibility modes for keeping its members:

private
 Object Private members – not accessible to objects

protected  Object Protected members – not accessible to objects

public  Object Public members – accessible to the objects

 Private – Member in private visibility mode are not accessible to the objects of class.
This visibility mode only accessible inside the class to the member functions of the class.
This visibility mode member is not inheritable. By default private member declare.
 Protected - Same as Private. This visibility mode member is inheritable.

By Sunil Kumar Mobile No.: 9868583636 Page 2 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Public – Member in public visibility mode of the class are accessible to the objects of the
class. This visibility mode member is inheritable.
In C++, a class can be defined with the following syntax:
class Class_Name class ANIMAL
{ Data Member {
Data-member; int LEG, EYE, EAR;
: char TYPE; // H: Herbivore
Member-function; // O: Omnivore
protected:
Data-member; public:
: void ENT_DATA();
Member-function; void SHOW_DATA();
public: };
Member Function
Data-member; :
: ANIMAL cat, cow;
Member-function;
}; Object Declaration
Class_Name List_of_the_objects;
Following C++ program illustrates the use of class and objects:
#include<iostream.h>
#include<stdio.h>
Const int Max = 20;
class Hospital
{
int Pno, Wardno; // Private data members
char Name[20];
public:
void Register(); // Publicmember function prototype
void ShowStatus() // Public member functions definition
{
cout<<Pno<<” “<<Wardno<<” “<<Name<<endl;
}
}; // Semicolon is must to end the class
void Hospital :: Register() // Member function defined outside the class
{
cout<<”Patient No? “; cin>>Pno;
cout<<”Name ? “; gets(name);
cout<<”Ward No ? ”; cin>>Wardno;
} // Semicolon not required
void main()
{ Hospital P1, P2; // Declaration of the objects P1, P2 of Hospital
P1.Register(); // Member function call for the object p1
P2.Register(); // Member function call for the object p2
P1.ShowStatus();
P2.ShowStatus();
}

By Sunil Kumar Mobile No.: 9868583636 Page 3 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

In the above example, one can see that a class Hospital is defined with three private data
members Pno, Wardno and Name; and two public member functions Register() and
Showstatus(). Member function can defined inside the class as well as outside the class; as
shown in the example, member function Showstatus() is defined inside the class whereas
member function Register() is defined outside the class by using scope resolution operator
(::) with the name of the class belongs to. Both of these functions can be called from the
objects of the class and multiple line functions are defined outside the class to have clarity in
understanding the behavior of the class. Functions with control structures should be defined
outside the class.
The size of object (in bytes) depends upon the data members present in the class it belongs
to. In the above example outside object P1 occupies 24 bytes.
 Inline Function: Class inside defined function is treated as an inline function. We can
define a member function outside the class definition and still make it inline by just using
the qualifier inline in the header (Starting) line of function definition. Normally, only
small functions are defined inside the class definition. Inline function execution speed is
faster than normal function. Every calling time copy in memory then memory wastage.
Normal functions of class loading and unloading from memory.
 Example
#include<iostream.h>
#include<stdio.h>
class STUDENT
{
int Rollno; Automatically as inline function
char Name[40];
public:
void Enter()
{
cin>>Rollno; gets(Name);
} Define the inline function through
void Show( ); “inline” keywords
};
inline void STUDENT :: Show()
{
cout<<Rollno<<” “<<Name<<endl;
}
void main()
{
STUDENT S1;
S1.Enter();
S1.Show();
}

By Sunil Kumar Mobile No.: 9868583636 Page 4 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Static member:
 Static data member is a share variable of all objects in same class type.
 Static data member scope is life time of program.
 Static data member must be defined outside the class definition.
 Static member function access only static data member of same class.
 Static member function access through class name (not object).

#include<iostream.h>
class STUDENT
{ Static Data Member
int Rollno;
float Marks;
static int count;
public: Static Member Function
void Input ();
void Output ();
static void Display ()
{
cout<<”\nCount = “<<count;
}
};
Static Data Member define outside class
int STUDENT :: count = 0;
void STUDENT :: Input ()
{
cout<<”\n Enter Roll No. : “; cin>>Rollno;
cout<<”\n Enter Marks : “; cin>>Marks;
count++;
}
void STUDENT :: Output ()
{
cout<<”\n Roll No. : “<< Rollno<<”\t Marks : “<< Marks;
}
void main ()
{
STUDENT S1, S2; Static Member function call (Invoke)
S1.Input(); S2.Input(); through class name (STUDENT)
STUDENT :: Display ();
STUDENT S3;
S3.Input();
STUDENT :: Display ();
S1.Output();
S2.Output();
S3.Output();
}

By Sunil Kumar Mobile No.: 9868583636 Page 5 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Constructor: It is a special member function of class with the following unique


features:
1. It has same name as the name of the class they belongs to.
2. It has no return type (even void).
3. It is defined in public visibility mode.
4. it is automatically called and execute (invoked) when an object is declared/created
5. Constructor can be overloaded.
6. It can not be inherited, through derived class.
7. Constructor can not virtual.
8. We can not refer to their addresses.

// Example of Constructor
#include<iostream.h>
class Trial
{
int a,b;
public:
Trial() Constructor / Default Constructor
{
a=0; b=0;
}
void Display()
{
cout<<a<<b<<endl;
}
void Raise() { a += 10; b += 10; }
void Down() { a -= 5; b -= 5; }
};

void main()
{ Automatically call for Constructor
Trial T;
T.Display();
T.Raise();
T.Display();
T.Down();
T.Display();
}

By Sunil Kumar Mobile No.: 9868583636 Page 6 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Default Constructor: A default constructor is one, which accepts no parameter. If no


constructor is defined for a class, the compiler creates a default constructor. A default
constructor supplied by the compiler does not perform any initialization.

 Parameterized constructor: A parameterized constructor can take values as argument


into the constructor function when objects are created.

 Constructor Overloading: A class can have more than one constructor with different
parameter. It is called constructor overloading.

// Example of Constructor Overloading


#include<iostream.h>
class Work
{
int X, Y;
public:
Work () // Constructor 1
{ X=15; Y=30; }
Work (int C) // Constructor 2 (Parameterized Constructor)
{
X=C; Y=2*C;
}
Work (int a, int b) // Constructor 3 (Parameterized Constructor)
{
X = a; Y = b;
}
void Show ()
{
cout<<X<<” “<<Y<<endl;
}
};
void main ()
{
Work W1; // Call of Constructor 1
Work W2 (25); // Call of Constructor 2
Work W3 (25, 55); // Call of Constructor 3
W1.Show();
W2.Show();
W3.Show();
}

By Sunil Kumar Mobile No.: 9868583636 Page 7 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Copy Constructor: A copy constructor is an overloaded constructor function in which an


object of the same class is passed as a reference parameter. It is used when an object’s
data value is related to or is initialized using another object’s data value of the same
class. In the example below the values of data members of object Q are dependent on
the values of data members of object P and Data members of object R dependent on Q.
//Example of Copy Constructor
#include<iostream.h>
class Play
{
int Count, Number;
public: Default Constructor Prototype
Play ();
Play (Play &);
void Display(); Copy Constructor Prototype
void Change(int, int);
};
Play :: Play () Default Constructor Defined
{
Count = 0;
Number=0;
}
Play :: Play (Play &P) Copy Constructor Defined
{
Count = P.Count;
Number=P.Number;
}
void Play :: Display()
{
cout<<Count<<” “<<Number<<endl;
}
void Play :: Change(int C, int N)
{
Count = C;
Number = N;
}

void main()
{ Default Constructor Call
Play P;
P.Display();
P.Change (80, 95);
Play Q (P); Copy Constructor Call
Q.Display();
Play R=Q;
R.Display();
Copy Constructor Call
}

By Sunil Kumar Mobile No.: 9868583636 Page 8 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Destructor: It is a special member function of class with the following unique features:
1. It has same name as the name of the class they belongs to and prefix tilde (~) sign.
2. It has no return type (even void).
3. It is defined in public visibility mode.
4. it is automatically called and execute (invoked) when an object scope is over.
5. It can not be inherited, through derived class.
6. Destructor can not virtual.
7. We can not refer to their addresses.

// Example of Destructor
#include<iostream.h>
int Count = 0;
class Work
{
public: Constructor Define
Work ()
{ Count++;
cout<< ”\n No. of object created “<<Count;
}
Destructor Define
~Work ()
{
cout<< ”\nNo. Ob object destroyed “<<Count;
Count--;
}
};
void main ()
{
Constructor Call of w1 and w2
cout<< “\n Enter Main “;
Work w1, w2;
{
Constructor Call of w3
cout<< “\nEnter Block “;
Work w3;
} Destructor Call of w3
(Scope is over of W3)
cout<< “\n Re-Enter Main”;
}

Destructor Call of w1 and w2


(Scope is over of w1 and w2)

By Sunil Kumar Mobile No.: 9868583636 Page 9 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Inheritance: Inheritance is most powerful feature of Object Oriented Programming, after


classes themselves. Inheritance is a process of creating new classes (derived classes)
from existing classes (base classes). The derived classes not only inherit capabilities of
the base class but also can add new features of their own. The process of inheritance
does not affect the base class. The most important aspect of inheritance is that it allows
reusability of code, and also a debugged class can be adapted to work in different to
work in different situations. Reusability of code saves money as well as time and increase
program reliability. Inheritance is very useful in original conceptualization and design of a
programming problem.

 Visibility under various modes

Private Member
 Object


Derived Class

Protected Member 
Object


Derived Class


Public Member Object


Derived Class

 Access Specifiers: Public


Class Base Class Derived : Public Base

Not Inheritable
Private Private

Inheritable
Protected Protected

Inheritable
Public Public

By Sunil Kumar Mobile No.: 9868583636 Page 10 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

 Access Specifiers: Protected

Class Base Class Derived : Protected Base

Not Inheritable
Private Private

Inheritable
Protected Protected
Inheritable

Public Public

 Access Specifiers: Private

Class Base
Class Derived : Private Base
Not Inheritable
Private
Inheritable Private

Protected
Protected
Inheritable

Public
Public

By Sunil Kumar Mobile No.: 9868583636 Page 11 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

Types of Inheritance
Single Level Multi Level Hierarchical Multiple Hybrid
Inheritance Inheritance Inheritance Inheritance Inheritance

AA
A A B
A

B B C
B
B C C

CD

class <Base Class Name>


{ Syntax
<Class Member> ; (Single Level
protected: Inheritance)
<Class Member> ;
public:
<Class Member> ;
};
class <Derived Class Name> : <Access Specifier> <Base Class Name> Country
{
<Class Member> ;
protected:
<Class Member> ;
public: State
<Class Member> ;
};

class Country
Example
{
(Single Level
int ArmyFunds;
Inheritance) Base Class
protected:
int Infrastructure;
public:
void Provide (); void Collect ();
};
class State: public Country
{
int No_of_Farms; Derived Class
public:
void Get (); void Put ();

By Sunil Kumar Mobile No.: 9868583636 Page 12 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

};

class <Base Class Name>


{ Syntax
<Class Member> ; (Multi Level
protected: Inheritance)
<Class Member> ;
public:
<Class Member> ;
};
class <Derived/Base Class Name> : <Access Specifier> <Base Class Name>
{
<Class Member> ; Country
protected:
<Class Member> ;
public:
<Class Member> ;
}; State
class <Derived Class Name> : <Access Specifier> <Base Class Name>
{
<Class Member> ;
protected:
<Class Member> ;
District
public:
<Class Member> ;
};

class Country
Example
{
(Multi Level
int ArmyFunds;
Inheritance) Base Class
protected:
int Infrastructure;
public:
void Provide (); void Collect ();
};
class State: public Country
{
int No_of_Farms; Sub Derived Class
public:
void Get (); void Put ();
};
class District : public State
{
int No_of_Offices; Derived Class
public:
void Input (); void Putput ();

By Sunil Kumar Mobile No.: 9868583636 Page 13 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

};

class <Base Class Name 1>


{ Syntax
<Class Member> ; (Multiple Inheritance)
protected:
<Class Member> ;
public:
<Class Member> ;
}; Country Government
class <Base Class Name 2>
{
<Class Member> ;
protected:
<Class Member> ;
public: Rules
<Class Member> ;
};
class <Derived Class Name> : <Access Specifier> <Base Class Name 1> ,
<Access Specifier> <Base Class Name 2>
{
<Class Member> ;
protected:
<Class Member> ;
public:
<Class Member> ;
};

class Country
Example
{
(Multiple
int ArmyFunds;
Inheritance) Base Class 1
protected:
int Infrastructure;
public:
void Provide (); void Collect ();
};
class Government
{
int ArticleNo; Base Class 2
public:
void Get (); void Put ();
};
class Rules : public Country, public Government
{
char Description[80]; Derived Class
public:
void Input (); void Putput ();

By Sunil Kumar Mobile No.: 9868583636 Page 14 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

};

class <Base Class Name 1>


{ Syntax
<Class Member> ; (Hierarchical Inheritance)
protected:
<Class Member> ;
public:
<Class Member> ;
};
class <Derived Class Name 1> : <Access Specifier> <Base Class Name>
{
<Class Member> ;
protected:
<Class Member> ;
public:
<Class Member> ;
};
class <Derived Class Name 2> : <Access Specifier> <Base Class Name >
{
<Class Member> ; Country
protected:
<Class Member> ;
public: State Union Territory
<Class Member> ;
};

class Country Example


{ (Hierarchical Inheritance)
int ArmyFunds;
protected: Base Class
int Infrastructure;
public:
void Provide (); void Collect ();
};
class State : public Country
{
int No_of_Farms; Derived Class 1
public:
void Get (); void Put ();
};
class UnionTerritory : public Country
{
int No_of_Offices; Derived Class 2
public:
void Input (); void Putput ();
};

By Sunil Kumar Mobile No.: 9868583636 Page 15 of 16


C++ Notes Class, Constructor and Destructor , Inheritance Class XII

Practice Work Sheet


Answer the questions with the help of the portion of program shown below:
class U
{
int X;
protected:
int Y;
public:
void Haveit ();
void Giveit ();
};
class T : public U
{ Mr. Sunil Kumar
int X1; Faculty of Computer Science
void Calc (); Teaches by : C, C++, Java, Data
protected: Structure, Oracle, MySQL,
int Y1; VBScript, ASP, JavaScript, HTML,
void Doit (); VB etc.
public: Contact No. :
int Z1; 9868583636
void Showit ();
void Addit ();
};
class P : private T
{
int X2;
protected:
int Y2;
public:
int Z2;
void Display ();
void Change ();
};
a) Name the member function(s), which can be accessed from the objects of class P.
b) Name the Data member(s), which can be accessed from the member function of class T.
c) Name the data member(s), which can be accessed by the object of class U.
d) Is the data member Y declare in class U accessible to the member function Change() of
class P?
e) How many bytes will be required by an object belonging to class P?
f) Write the definition of member function Display () of class P to display all data members
which are accessible from it.
g) Write the name of Base class and Derived class and Derived class of class T.

By Sunil Kumar Mobile No.: 9868583636 Page 16 of 16

Vous aimerez peut-être aussi