Académique Documents
Professionnel Documents
Culture Documents
Class Student
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
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.
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();
}
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();
}
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();
}
// 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();
}
Constructor Overloading: A class can have more than one constructor with different
parameter. It is called constructor overloading.
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
}
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”;
}
Private Member
Object
Derived Class
Protected Member
Object
Derived Class
Public Member Object
Derived Class
Not Inheritable
Private Private
Inheritable
Protected Protected
Inheritable
Public Public
Not Inheritable
Private Private
Inheritable
Protected Protected
Inheritable
Public Public
Class Base
Class Derived : Private Base
Not Inheritable
Private
Inheritable Private
Protected
Protected
Inheritable
Public
Public
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 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 ();
};
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 ();
};
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 ();
};