You are on page 1of 31

OOPS CONCEPTS

C++
Object Oriented Programming
Object-oriented programming aims to
implement real-world entities like
inheritance, hiding, polymorphism, etc. in
programming. The main aim of OOP is to
bind together the data and the functions that
operate on them so that no other part of the
code can access this data except that
function.
C++ Programming Features
C++ Programming Features
OOPS CONCEPTS
Class and Objects
Objects:
An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is allocated.
Classes
 A Class in C++ is a blueprint representing a group of objects which shares some
common properties and behaviours.
 A Class is a user-defined data type that has data members and member functions.
 Data members are the data variables and member functions are the functions used to
manipulate these variables together these data members and member functions
define the properties and behavior of the objects in a Class.
Syntax for Class
class classname
{
accesss specifier:
variable declaration;
method declaration;
accesss specifier:
variable declaration;
method declaration;
};
Syntax for object creation:
classname objectname;
Structure of Class (example)
#include <iostream>
using namespace std;
class person
{
char name[20];
int id;
public:
void getdetails() {
cout<<"enter the id number"<<endl;
cin>>id;
cout<<"enter the name"<<endl;
cin>>name;
cout<<"The id and name of student:"<<id<<endl<<name<<endl;
}
};
int main()
{
person p1;
p1.getdetails();// p1 is a object
return 0;
}
Data Abstraction
Abstraction:

 DATA HIDING
 Abstraction refers to the act of representing
essential features without including the
background details or explanations.
example
#include<iostream> void main()
{ sum s;
class sum s.add();
{
// hidden data from outside world getch(); }
private:
int a,b,c;
public:
void add()
{
clrscr();
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
Encapsulation
Definition:
The Wrapping up of data and functions
into a single unit(called class) is known as
Data Encapsulation. Data encapsulation is
the most striking feature of a class.
Polymorphism
Definition:
 Polymorphism is another important oop concept.
 Polymorphism means the ability to take more than
one form.
 For example, an operation may exhibit different
behavior in different instances. Behavior depends
upon the types of data used in the operation.
Method overloading
Method overriding
Function Overloading
Definition:
 Using the same function name and performing

different process.
 When a function is redefined with different set of
arguments, then it is known as overloaded
function. This process is known as function
overloading.
 Function name is same, so functions invoked on
the basis of,
* based on number of arguments
* based on data-type of the arguments
Function overloading
Operator Overloading
Definition:
 The operator overloading feature of C++ is one of

the methods of realizing polymorphism.


 C++ has the ability to provide the operators with a

special meaning to an operator is known as

operator overloading.
 Different behavior at different instances.
• Early binding is also called static binding.
Early binding occurs when we make the
explicit or direct function call in our program.
• When the compiler encounters a function call
in the program, it replaces that function call
with a machine language instruction to go to
that function.
• Therefore, when the point of execution comes
in that function call line, it reads that
instruction and then jumps to the function
given by memory address.
• Late binding is also called dynamic binding.
Late binding occurs when we make implicit or
indirect function calls in our program. An
example of this is using function pointers or
virtual functions when using classes. Here, the
function call’s memory reference is not
determined at compile-time, but rather at run-
time.
Operator Overloading
The following Operators cannot be overloaded

Size of operator(sizeof)
Scope resolution operator(::)
Conditional operator(?:)
Dynamic Memory Allocation
Definition:
 Ifthe memory space for the constructor
(memory variable) is allocated during the
constructor calling, then it is called as dynamic
constructor.
This is done with the help of operator “new”.
Depending upon the size of the values in the
variable, the memory occupation of the object
varies.
Inheritance
Definition:
The mechanism of deriving a new class
from an old one is called inheritance.
An object of one class gets the properties
from object of another class.
It supports the concept of reusability
Base class or super class: class which gives
the property
Derived class or sub class : class which gets
the property
Types of inheritance
• In C++, we have 5 different types of Inheritance. Namely,
• Single Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Multilevel Inheritance
• Hybrid Inheritance (also known as Virtual Inheritance)
Inheritance
Syntax:
class derived-classname : access specifier
base-classname
{
//member of derived class
}
Virtual Functions
Definition:
 A virtual function is a member function that you expect to be
redefined in derived classes.
When you refer to a derived class object using a pointer, you
can call a virtual function for that object and execute the
derived class version of the function.
Example

You might also like