SlideShare a Scribd company logo
www.SunilOS.com 1
www.sunilos.com
www.raystec.com
Object Oriented Programming
OOP……. Not OOPS!!
2
Object-Oriented Programming Concepts
What is an Object?
What is a Class?
Encapsulation?
Inheritance?
Polymorphism/Dynamic Binding?
Data Hiding?
Data Abstraction?
www.SunilOS.com
www.SunilOS.com 3
Custom Data Type
class Shape {
private:
int borderWidth = 0;
public:
int getBorderWidth() {
return borderWidth;
}
void setBorderWidth(int bw) {
borderWidth = bw;
}
};
:Shape
-color :String
-borderWidth:int
+getColor():String
+setColor()
+getBorderWidth():int
+setBorderWidth()
Members
Member
variables
Member
methods
www.SunilOS.com 4
Define attribute/variable
 void main(){
 Shape s;
 s.setBorderWidth(3);
 ….
 cout<<s.getBorderWidth();
}
S is an object here
Real World Entities – More Classes
www.SunilOS.com 5
:Automobile
-color :String
-speed:int
-make:String
+$NO_OF_GEARS
+getColor():String
+setColor()
+getMake():String
+setMake()
+break()
+changeGear()
+accelerator()
+getSpeed():int
:Person
-name:String
-dob : Date
-address:String
+$AVG_AGE
+getName():String
+setName()
+getAdress():String
+setAddress()
+getDob (): Date
+setDob ()
+getAge() : int
:Account
-number:String
-accountType : String
-balance:double
+getNumber():String
+setNumber()
+getAccountType():String
+setAccountType()
+deposit ()
+withdrawal ()
+getBalance():double
+fundTransfer()
+payBill()
www.SunilOS.com 6
Constructor
class Shape {
public Shape(){
cout<<“This is default
constructor”;
}
};
Shape s;
 Constructor is just like a method.
 It does not have return type.
 Its name is same as Class name.
 It is called at the time of object
calling.
 Constructors are used to initialize
instance/class variables.
 A class may have multiple
constructors with different number
of parameters.
www.SunilOS.com 7
Multiple Constructors
One class may have more than one constructors.
Multiple constructors are used to initialize different
sets of class attributes.
When a class has more than one constructors, it is
called Constructor Overloading.
Constructors those receive parameters are called
Parameterized Constructors.
www.SunilOS.com 8
Constructors Overloading
class Shape {
public :
Shape(){
cout<<“This is default
constuctor”;
}
Shape(int a){
cout<<a;
}
Shape s ;
Or
Shape s1(5);
Default Constructor
 Default constructor does not receive any parameter.
o public Shape(){ .. }
 If User does not define any constructor then Default
Constructor will be created by Compiler.
 But if user defines single or multiple constructors then
default constructor will not be generated by Compiler.
www.SunilOS.com 9
Destructor
It is inverse of constructor function.
It is used to deallocate the memory or
release the resources like closing files.
It is called when object is destroyed.
It has a same name as class name used
tilde(~) as prefix.
~class_name(){…….}
www.SunilOS.com 10
Copy Constructor
It is a special type of constructor .
It is used to create new object as a copy of
existing object.
It is a standard way to copy an object.
It is used to initialize one object from another
object of the same type.
www.SunilOS.com 11
Copy Constructor…
class CopyCon{
private :
int a1;
public:
CopyCon(){}
CopyCon(int a){
a1 = a;
}
void display(){
cout<<a1;
}
};
www.SunilOS.com 12
void main(){
clrscr();
CopyCon c(2);
CopyCon c1 = c;
c.display();
c1.display();
getch();
}
Operator Overloading
Built-in operators can be redefined &
overloaded.
Overloaded operators are functions with special
names.
It is used to perform operations on user defined
data type.
 + operator overloaded on two integer values to
perform addition.
 + operator overloaded on two string values to
perform concatenation.
www.SunilOS.com 13
return_type operator sign(){}
void operator +() {
count=count+1;
}
www.SunilOS.com 14
 class temp {
private:
int count;
public:
temp():count(5){ }
void operator ++() {
count=count+1;
}
void Display() {
cout<<"Count: "<<count;
}
};
www.SunilOS.com 15
int main()
{
temp t;
++t;
t.Display();
return 0;
}
Operators that cannot be overloaded
scope operator - ::
sizeof
member selector - .
member pointer selector - *
ternary operator - ?:
www.SunilOS.com 16
Scope Resolution Operator ::
It is used to qualify hidden name.
When local variable and global variable are
having same name, local variable gets the
priority.
C++ allows flexibility of accessing both the
variables through a scope resolution
operator.
www.SunilOS.com 17
int a = 10;
void main(){
o int a = 15;
o cout<<a<<::a;
o ::a=20;
o Cout<<a<<::a;
}
Output :15 10
 15 20
www.SunilOS.com 18
OOP Key Concepts
Encapsulation:
o Creates Expert Classes.
Inheritance:
o Creates Specialized Classes.
Polymorphism:
o Provides Dynamic behaviour at Runtime.
www.SunilOS.com 19
www.SunilOS.com 20
Encapsulation
Gathering all related methods and attributes in a
Class is called encapsulation.
Often, for practical reasons, an object may wish
to expose some of its variables or hide some of
its methods.
Access Levels:
Modifier Class Subclass Packag
e
World
private X
protected X X X
public X X X X
Friend Function
There is a mechanism in C++ to access
private & protected members of a class.
This mechanism is known as friend function
or friend class.
We have to use friend keyword in prefix of
function name.
Declaration should be inside the class body
starting with keyword friend.
www.SunilOS.com 21
 class Distance {
o private:
o int meter;
o public:
o Distance(): meter(0){ }
o friend int func(Distance); //friend function
 };
 int func(Distance d) {//function definition
o d.meter=5; //accessing private data from non-member function
o return d.meter;
 } int main() {
o Distance D;
o cout<<"Distace: "<<func(D);
o return 0;
 }
www.SunilOS.com 22
Inheritance
www.SunilOS.com 23
www.SunilOS.com 24
Inheritance
:Shape
color :String
borderWidth:int
getColor():String
setColor()
getBorderWidth():int
setBorderWidth()
:Rectangle
length :int
width:int
area()
getLength()
setLength()
:Circle
radius : int
area()
getRadius()
setRadius()
:Triangle
base:int
hight:int
area()
getBase()
setBase()
Circle c ;
c.getColor();
c.getBorderWidth();
c.area();
UML Notation
Polymorphism
www.SunilOS.com 25
Polymorphism
One thing with multiple forms.
Two types of polymorphism
Static Polymorphism(Overloading)
Dynamic Polymorphism(Overriding)
Ways to Provide polymorphism:
o Through Interfaces.
o Method Overriding.
o Method Overloading.
www.SunilOS.com 26
www.SunilOS.com 27
Method Overriding – area()
:Shape
Color :String
borderWidth:int
getColor():String
setColor()
getBorderWidth():int
setBorderWidth()
area()
:Rectangle
length :int
width:int
area()
getLength()
setLength()
:Circle
radius
area()
getRadius()
setRadius()
:Triangle
base:int
hight:int
area()
getBase()
setBase()
Shape s;
s.getColor();
s.getBorderWidth();
s.area();
www.SunilOS.com 28
Abstract Class
 What code can be written in Shape.area() method?
o Nothing, area() method is defined by child classes. It should have
only declaration.
 Is Shape a concrete class?
o NO, Rectangle, Circle and Triangle are concrete classes.
 If it has only area declaration then
o Class will be abstract.
 Benefit?
o Parent will enforce child to implement area() method.
o Child has to mandatorily define (implement) area method.
o This will achieve polymorphism.
www.SunilOS.com 29
Shape
class Shape {
private:
int borderWidth = 0;
public :
double area();
int getBorderWidth() {
return borderWidth;
}
};
www.SunilOS.com 30
Interface / Virtual function
A class is made abstract by declaring at least one
of its functions as pure virtual function
A pure virtual function is specified by placing "= 0"
in its declaration
public:
virtual double area() = 0;
www.SunilOS.com 31
Interfaces
Richman
earnMony()
donation()
party()
Businessman
name
address
earnMony()
donation()
party()
SocialWorker
helpToOthers()
Businessman
name
address
earnMony()
donation()
party()
helpToOthers()
Businessman
name
address
helpToOthers()
Data Abstraction
www.SunilOS.com 32
Data Abstraction ( Cont. )
Data abstraction is the way to create complex data
types and exposing only meaningful operations to
interact with data type, whereas hiding all the
implementation details from outside world.
Data Abstraction is a process of hiding the
implementation details and showing only the
functionality.
Data Abstraction in java is achieved by interfaces
and abstract classes.
www.SunilOS.com 33
Data Hiding
www.SunilOS.com 34
Data Hiding ( Cont. )
Data Hiding is an aspect of Object Oriented
Programming (OOP) that allows developers
to protect private data and hide
implementation details.
Developers can hide class members from
other classes. Access of class members
can be restricted or hide with the help of
access modifiers.
www.SunilOS.com 35
Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 36
Thank You!
www.SunilOS.com 37
www.SunilOS.com

More Related Content

PPT
Class and object in C++
PPT
PDF
Function overloading ppt
PPTX
Pointers in c++
PDF
Classes and objects
PDF
Object-oriented Programming-with C#
PPTX
Friend function & friend class
PPTX
Java script errors &amp; exceptions handling
Class and object in C++
Function overloading ppt
Pointers in c++
Classes and objects
Object-oriented Programming-with C#
Friend function & friend class
Java script errors &amp; exceptions handling

What's hot (20)

PPTX
Intro to c++
PPTX
C# classes objects
PPSX
Files in c++
PPSX
Complete C++ programming Language Course
PPT
PHP - Introduction to Object Oriented Programming with PHP
PPTX
Decision Making Statement in C ppt
PPT
Introduction to C++
PPTX
classes and objects in C++
PPT
Functions in C++
PPT
friend function(c++)
PPT
Basics of c++
PDF
C programming notes
PPTX
PDF
Java variable types
PPT
7.data types in c#
PPT
standard template library(STL) in C++
PPTX
OOP C++
PPTX
Templates in c++
PPT
Constructor and destructor in C++
PPTX
User defined functions in C
Intro to c++
C# classes objects
Files in c++
Complete C++ programming Language Course
PHP - Introduction to Object Oriented Programming with PHP
Decision Making Statement in C ppt
Introduction to C++
classes and objects in C++
Functions in C++
friend function(c++)
Basics of c++
C programming notes
Java variable types
7.data types in c#
standard template library(STL) in C++
OOP C++
Templates in c++
Constructor and destructor in C++
User defined functions in C
Ad

Similar to C++ oop (20)

PPTX
concept of oops
PDF
L1-Introduction to OOPs concepts.pdf
PPTX
Introduction to Fundamental of Class.pptx
PPTX
oopusingc.pptx
PPTX
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
PPTX
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
PPTX
Concept of Object-Oriented in C++
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PDF
4 pillars of OOPS CONCEPT
PDF
Lab 4 (1).pdf
PPTX
Oop objects_classes
PPTX
Four Pillers Of OOPS
PPT
OOP Core Concept
PPTX
Introduction to Object Oriented Programming
PPTX
OBJECT ORIENTED PROGRAMING IN C++
PPTX
Chapter - 1.pptx
PPTX
Unit - I Fundamentals of Object Oriented Programming .pptx
PPTX
C++ & Data Structure - Unit - first.pptx
concept of oops
L1-Introduction to OOPs concepts.pdf
Introduction to Fundamental of Class.pptx
oopusingc.pptx
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
Concept of Object-Oriented in C++
Chapter 2 OOP using C++ (Introduction).pptx
4 pillars of OOPS CONCEPT
Lab 4 (1).pdf
Oop objects_classes
Four Pillers Of OOPS
OOP Core Concept
Introduction to Object Oriented Programming
OBJECT ORIENTED PROGRAMING IN C++
Chapter - 1.pptx
Unit - I Fundamentals of Object Oriented Programming .pptx
C++ & Data Structure - Unit - first.pptx
Ad

More from Sunil OS (20)

PPT
Threads V4
PPT
Java IO Streams V4
PPT
OOP V3.1
PPT
Java Basics V3
PPT
DJango
PPT
PDBC
PPT
OOP v3
PPT
Threads v3
PPT
Exception Handling v3
PPT
Collection v3
PPT
Java 8 - CJ
PPTX
Machine learning ( Part 3 )
PPTX
Machine learning ( Part 2 )
PPTX
Machine learning ( Part 1 )
PPT
Python Pandas
PPT
Python part2 v1
PPT
Angular 8
PPT
Python Part 1
PPT
C# Variables and Operators
PPT
C# Basics
Threads V4
Java IO Streams V4
OOP V3.1
Java Basics V3
DJango
PDBC
OOP v3
Threads v3
Exception Handling v3
Collection v3
Java 8 - CJ
Machine learning ( Part 3 )
Machine learning ( Part 2 )
Machine learning ( Part 1 )
Python Pandas
Python part2 v1
Angular 8
Python Part 1
C# Variables and Operators
C# Basics

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
master seminar digital applications in india
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
LDMMIA Reiki Yoga Finals Review Spring Summer
Supply Chain Operations Speaking Notes -ICLT Program
Orientation - ARALprogram of Deped to the Parents.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Microbial diseases, their pathogenesis and prophylaxis
Updated Idioms and Phrasal Verbs in English subject
Weekly quiz Compilation Jan -July 25.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial disease of the cardiovascular and lymphatic systems
Module 4: Burden of Disease Tutorial Slides S2 2025
History, Philosophy and sociology of education (1).pptx
Final Presentation General Medicine 03-08-2024.pptx
Yogi Goddess Pres Conference Studio Updates
master seminar digital applications in india

C++ oop

  • 2. 2 Object-Oriented Programming Concepts What is an Object? What is a Class? Encapsulation? Inheritance? Polymorphism/Dynamic Binding? Data Hiding? Data Abstraction? www.SunilOS.com
  • 3. www.SunilOS.com 3 Custom Data Type class Shape { private: int borderWidth = 0; public: int getBorderWidth() { return borderWidth; } void setBorderWidth(int bw) { borderWidth = bw; } }; :Shape -color :String -borderWidth:int +getColor():String +setColor() +getBorderWidth():int +setBorderWidth() Members Member variables Member methods
  • 4. www.SunilOS.com 4 Define attribute/variable  void main(){  Shape s;  s.setBorderWidth(3);  ….  cout<<s.getBorderWidth(); } S is an object here
  • 5. Real World Entities – More Classes www.SunilOS.com 5 :Automobile -color :String -speed:int -make:String +$NO_OF_GEARS +getColor():String +setColor() +getMake():String +setMake() +break() +changeGear() +accelerator() +getSpeed():int :Person -name:String -dob : Date -address:String +$AVG_AGE +getName():String +setName() +getAdress():String +setAddress() +getDob (): Date +setDob () +getAge() : int :Account -number:String -accountType : String -balance:double +getNumber():String +setNumber() +getAccountType():String +setAccountType() +deposit () +withdrawal () +getBalance():double +fundTransfer() +payBill()
  • 6. www.SunilOS.com 6 Constructor class Shape { public Shape(){ cout<<“This is default constructor”; } }; Shape s;  Constructor is just like a method.  It does not have return type.  Its name is same as Class name.  It is called at the time of object calling.  Constructors are used to initialize instance/class variables.  A class may have multiple constructors with different number of parameters.
  • 7. www.SunilOS.com 7 Multiple Constructors One class may have more than one constructors. Multiple constructors are used to initialize different sets of class attributes. When a class has more than one constructors, it is called Constructor Overloading. Constructors those receive parameters are called Parameterized Constructors.
  • 8. www.SunilOS.com 8 Constructors Overloading class Shape { public : Shape(){ cout<<“This is default constuctor”; } Shape(int a){ cout<<a; } Shape s ; Or Shape s1(5);
  • 9. Default Constructor  Default constructor does not receive any parameter. o public Shape(){ .. }  If User does not define any constructor then Default Constructor will be created by Compiler.  But if user defines single or multiple constructors then default constructor will not be generated by Compiler. www.SunilOS.com 9
  • 10. Destructor It is inverse of constructor function. It is used to deallocate the memory or release the resources like closing files. It is called when object is destroyed. It has a same name as class name used tilde(~) as prefix. ~class_name(){…….} www.SunilOS.com 10
  • 11. Copy Constructor It is a special type of constructor . It is used to create new object as a copy of existing object. It is a standard way to copy an object. It is used to initialize one object from another object of the same type. www.SunilOS.com 11
  • 12. Copy Constructor… class CopyCon{ private : int a1; public: CopyCon(){} CopyCon(int a){ a1 = a; } void display(){ cout<<a1; } }; www.SunilOS.com 12 void main(){ clrscr(); CopyCon c(2); CopyCon c1 = c; c.display(); c1.display(); getch(); }
  • 13. Operator Overloading Built-in operators can be redefined & overloaded. Overloaded operators are functions with special names. It is used to perform operations on user defined data type.  + operator overloaded on two integer values to perform addition.  + operator overloaded on two string values to perform concatenation. www.SunilOS.com 13
  • 14. return_type operator sign(){} void operator +() { count=count+1; } www.SunilOS.com 14
  • 15.  class temp { private: int count; public: temp():count(5){ } void operator ++() { count=count+1; } void Display() { cout<<"Count: "<<count; } }; www.SunilOS.com 15 int main() { temp t; ++t; t.Display(); return 0; }
  • 16. Operators that cannot be overloaded scope operator - :: sizeof member selector - . member pointer selector - * ternary operator - ?: www.SunilOS.com 16
  • 17. Scope Resolution Operator :: It is used to qualify hidden name. When local variable and global variable are having same name, local variable gets the priority. C++ allows flexibility of accessing both the variables through a scope resolution operator. www.SunilOS.com 17
  • 18. int a = 10; void main(){ o int a = 15; o cout<<a<<::a; o ::a=20; o Cout<<a<<::a; } Output :15 10  15 20 www.SunilOS.com 18
  • 19. OOP Key Concepts Encapsulation: o Creates Expert Classes. Inheritance: o Creates Specialized Classes. Polymorphism: o Provides Dynamic behaviour at Runtime. www.SunilOS.com 19
  • 20. www.SunilOS.com 20 Encapsulation Gathering all related methods and attributes in a Class is called encapsulation. Often, for practical reasons, an object may wish to expose some of its variables or hide some of its methods. Access Levels: Modifier Class Subclass Packag e World private X protected X X X public X X X X
  • 21. Friend Function There is a mechanism in C++ to access private & protected members of a class. This mechanism is known as friend function or friend class. We have to use friend keyword in prefix of function name. Declaration should be inside the class body starting with keyword friend. www.SunilOS.com 21
  • 22.  class Distance { o private: o int meter; o public: o Distance(): meter(0){ } o friend int func(Distance); //friend function  };  int func(Distance d) {//function definition o d.meter=5; //accessing private data from non-member function o return d.meter;  } int main() { o Distance D; o cout<<"Distace: "<<func(D); o return 0;  } www.SunilOS.com 22
  • 24. www.SunilOS.com 24 Inheritance :Shape color :String borderWidth:int getColor():String setColor() getBorderWidth():int setBorderWidth() :Rectangle length :int width:int area() getLength() setLength() :Circle radius : int area() getRadius() setRadius() :Triangle base:int hight:int area() getBase() setBase() Circle c ; c.getColor(); c.getBorderWidth(); c.area(); UML Notation
  • 26. Polymorphism One thing with multiple forms. Two types of polymorphism Static Polymorphism(Overloading) Dynamic Polymorphism(Overriding) Ways to Provide polymorphism: o Through Interfaces. o Method Overriding. o Method Overloading. www.SunilOS.com 26
  • 27. www.SunilOS.com 27 Method Overriding – area() :Shape Color :String borderWidth:int getColor():String setColor() getBorderWidth():int setBorderWidth() area() :Rectangle length :int width:int area() getLength() setLength() :Circle radius area() getRadius() setRadius() :Triangle base:int hight:int area() getBase() setBase() Shape s; s.getColor(); s.getBorderWidth(); s.area();
  • 28. www.SunilOS.com 28 Abstract Class  What code can be written in Shape.area() method? o Nothing, area() method is defined by child classes. It should have only declaration.  Is Shape a concrete class? o NO, Rectangle, Circle and Triangle are concrete classes.  If it has only area declaration then o Class will be abstract.  Benefit? o Parent will enforce child to implement area() method. o Child has to mandatorily define (implement) area method. o This will achieve polymorphism.
  • 29. www.SunilOS.com 29 Shape class Shape { private: int borderWidth = 0; public : double area(); int getBorderWidth() { return borderWidth; } };
  • 30. www.SunilOS.com 30 Interface / Virtual function A class is made abstract by declaring at least one of its functions as pure virtual function A pure virtual function is specified by placing "= 0" in its declaration public: virtual double area() = 0;
  • 33. Data Abstraction ( Cont. ) Data abstraction is the way to create complex data types and exposing only meaningful operations to interact with data type, whereas hiding all the implementation details from outside world. Data Abstraction is a process of hiding the implementation details and showing only the functionality. Data Abstraction in java is achieved by interfaces and abstract classes. www.SunilOS.com 33
  • 35. Data Hiding ( Cont. ) Data Hiding is an aspect of Object Oriented Programming (OOP) that allows developers to protect private data and hide implementation details. Developers can hide class members from other classes. Access of class members can be restricted or hide with the help of access modifiers. www.SunilOS.com 35
  • 36. Disclaimer This is an educational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. www.SunilOS.com 36