Classes and Objects: Definition of OOPS
Classes and Objects: Definition of OOPS
Characteristics of OOPS • Programs are divided into known objects • Builds the data and functions around these objects or
entities
Definition of OOPS:
OOP is a method of implementation in which programs are organized as cooperative collections of objects, each of which
represent an instance of some class and whose classes are all members of a hierarchy of classes united through the
property called inheritance.
1. Object 2. Class 3. Data abstraction 4. Inheritance 5. Polymorphism 6. Dynamic Binding 7. Message passing.
1. Object
Object is an entity that can store data and, send and receive messages. They are runtime entities, they may represent a
person, a place a bank account, a table of data or any item that the program must handle. It is an instance of a class.
2. Classes
A class is a collection of objects of similar type. Classes are user defined data types and behave like the built in types of a
programming language. For example mango, apple and orange are members of the class fruit. Then the statement FRUIT
MANGO; will create an object mango belonging to the class fruit. The syntax used to create an object is no different than
the syntax used to create an integer object in C. if fruit has been defined as a class, then the statement
fruit mango;
Object is an entity that can store data and send and receive messages. They are run time entities they may also represent
user-defined data.
4. Inheritance :
Types of Inheritance:
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
5. Hybrid inheritance
1. SINGLE INHERITANCE:-
The new class can be derived from only one base class is called single
inheritance.
2.Multilevel Inheritance:-
The class A serves as a base class for the derived class B which in turn serves
as a base class for the derived class C. The chain ABC is known as inheritance path.
Class A
{
//body of base class A
};
Class B: public A
};
Class C: public B
3. MULTIPLE INHERITANCE:-
A class can inherit the attributes or properties of two or more classes that is a
new class can be derived from more than one base class is called multiple inheritance.
Here the class A , class B and class C serves as a base class for the derived class D
body of class D;
};
Example:
class A public:
protected: {
public: sub2=z;
void getroll(int x) }
{ };
} {
}; int total;
class B public:
{ void display()
protected: {
total=sub1+sub2; {
cout<<”roll C s;
no:”<<rollno<<”sub1:”<<sub1<<”sub2:”<<sub2;
s. getroll(435);
cout<<”total”<<total;
s.getmark(100,90);
}
s.display();
};
}
void main()
4. HIERARCHICAL INHERITANCE:-
The hierarchical inheritance structure is given below .This type is helpful when
We have class hierarchies. In this scheme the base class will include all the features
#include<iostream.h>
#include<string.h>
class A
protected:
int x, y;
public:
void get ( )
cin>> x>>y;
};
Class B: public A
Private:
int m;
Public:
Void add ( )
m= x + y;
};
class C : public A
Private:
int n;
public:
void mul( )
n= x * y;
};
Class D: public A
Private:
float l;
public:
void division( )
l = x / y;
};
void main( )
B obj1; C .get( );
C obj2; C .mul( )
D obj3; D .get( );
B .get( ); D .division( );
B .add( ); }
5. HYBRID INHERITANCE:-
The hybrid inheritance is defined as a combination of multilevel and multipleInheritances. This new class can be derived by
either multilevel or multiple method or both.
Example program
In this program the derived class (result) object will be inheriting the properties of both test class and sports class.
#include <iostream.h>
protected: {
public: }
rollno = x; {
} int total;
}; public:
{ {
public: }
{ void main( )
sub1 = y; sub2 = z; {
} result S;
}; s.getroll(101);
{ s.getscore(2000);
protected: s. display( );
int score; }
VIRTUAL FUNCTION:
When we use the same function name in base and derived classes, the function in the base classes is declared as virtual
using keyword ‘virtual’ preceding its normal declaration. The member function that can be changed at runtime is called
virtual function.
Class classname
public:
.....
{
.....
. . . . .}};
The duplication of inherited members due to the multiple paths can be avoided
by making the common base class as virtual base class while declaring the direct or
class A
------
------
};
-----
-----
};
-----
-----
};
-----
-----
};
*******************************************************************************************************************************
5. Polymorphism: Polymorphism means the ability to take more than one form.
6. Dynamic binding: Dynamic binding(dispatch) means that a block of code executed with reference to a
procedure(method) call is determined at run time.
#include <iostream>
class B
{
public:
// Virtual function
};
class D: public B
public:
void f()
};
int main()
B base;
D derived;
B *basePtr = &base;
basePtr->f();
basePtr = &derived;
basePtr->f();
return 0;
7. message communication : An object oriented program consists of a set of objects that communicate with each other.
Objects communicate with one another by sending and receiving information much the same way as people pass
messages to one another. Objects have a life cycle. They can be created and destroyed. Communication with an object is
feasible as long as it is alive.
Inline function : One of the objective of usin g functions in a program is to save some memory space , which becomes
appreciable when a function is likely to be called many times.
To eliminate the cost of calls to small function C++ proposes a new feature called inline function.
An inline function is a function that is expanded in a line, this function keeps a request to compiler to give preference to
execute it as a request, but it does not makes a commands to give that preference to execute.
Inline function_header
function body
DESTRUCTOR:- It is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor
is a member function whose name is the same as the class name but is preceded by a tilde.
FRIEND FUNCTION A function that has access to the private member of the class but is not itself a member of the class is
called friend functions.The general form is
#include<iostream.h> {
t.getmark( ); }
FRIEND CLASSES
Definition:
All the functions of another class to manipulate or use the private members of the friend class.
Example:
#include<iostream.h>
class two;
class one
private:
int x, y;
{ }
x=a; y=b; };
} void main( )
}; one obj1;
{ Run
} Y is: 65
THIS POINTER:-
C++ uses a unique keyword called this to represent an object that invokes a member function. This is a pointer that points
to the object for which this function was called