SlideShare a Scribd company logo
1 of 37
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

What's hot

Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++Vishesh Jha
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Object Oriented Programming ppt presentation
Object Oriented Programming ppt presentationObject Oriented Programming ppt presentation
Object Oriented Programming ppt presentationAyanaRukasar
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop pptdaxesh chauhan
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Languagedheva B
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 

What's hot (20)

Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Object Oriented Programming ppt presentation
Object Oriented Programming ppt presentationObject Oriented Programming ppt presentation
Object Oriented Programming ppt presentation
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Polymorphism in c++
Polymorphism in c++Polymorphism in c++
Polymorphism in c++
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 

Similar to C++ oop

Similar to C++ oop (20)

Oops concept
Oops conceptOops concept
Oops concept
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
02.adt
02.adt02.adt
02.adt
 
C++ classes
C++ classesC++ classes
C++ classes
 
Object
ObjectObject
Object
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
OOP V3.1
OOP V3.1OOP V3.1
OOP V3.1
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

More from Sunil OS

Threads V4
Threads  V4Threads  V4
Threads V4Sunil OS
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4Sunil OS
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3Sunil OS
 
Threads v3
Threads v3Threads v3
Threads v3Sunil OS
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3Sunil OS
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJSunil OS
 
Machine learning ( Part 3 )
Machine learning ( Part 3 )Machine learning ( Part 3 )
Machine learning ( Part 3 )Sunil OS
 
Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )Sunil OS
 
Machine learning ( Part 1 )
Machine learning ( Part 1 )Machine learning ( Part 1 )
Machine learning ( Part 1 )Sunil OS
 
Python Pandas
Python PandasPython Pandas
Python PandasSunil OS
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1Sunil OS
 
Angular 8
Angular 8 Angular 8
Angular 8 Sunil OS
 
Python Part 1
Python Part 1Python Part 1
Python Part 1Sunil OS
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and OperatorsSunil OS
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays TechnologiesSunil OS
 

More from Sunil OS (20)

Threads V4
Threads  V4Threads  V4
Threads V4
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
 
DJango
DJangoDJango
DJango
 
PDBC
PDBCPDBC
PDBC
 
OOP v3
OOP v3OOP v3
OOP v3
 
Threads v3
Threads v3Threads v3
Threads v3
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3
 
Collection v3
Collection v3Collection v3
Collection v3
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
 
Machine learning ( Part 3 )
Machine learning ( Part 3 )Machine learning ( Part 3 )
Machine learning ( Part 3 )
 
Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )
 
Machine learning ( Part 1 )
Machine learning ( Part 1 )Machine learning ( Part 1 )
Machine learning ( Part 1 )
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays Technologies
 

Recently uploaded

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 

Recently uploaded (20)

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

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