SlideShare a Scribd company logo
Object Oriented Programming
By Basharat Jehan
Class
• Class is the description of properties and
behavior of a real world entity.
•OR
• Class is blueprint of objects
Object
• Object is instance of class. Object uses
behavior and properties used in classes.
• Functions in class are called member
functions.
• Data in class is called data members.
• Data of class is private, while functions in class
are public.
• Private data can only accessed with in class,
while public data can be accessed from main.
• Public data can be accessed by objects of the
same class.
• // smallobj.cpp
• // demonstrates a small, simple object
• #include <iostream>
• using namespace std;
• ////////////////////////////////////////////////////////////////
• class smallobj //define a class
• {
• private:
• int somedata; //class data
• public:
• void setdata(int d) //member function to set data
• { somedata = d; }
• void showdata() //member function to display data
• { cout << “Data is “ << somedata << endl; }
• };
• ////////////////////////////////////////////////////////////////
• int main()
• {
• smallobj s1, s2; //define two objects of class smallobj
• s1.setdata(1066); //call member function to set data
• s2.setdata(1776);
• s1.showdata(); //call member function to display data
• s2.showdata();
• return 0;
• }
private and public
• The body of the class contains two unfamiliar keywords:
private and public. What is their purpose?
• A key feature of object-oriented programming is data
hiding. This term does not refer to the
• activities of particularly paranoid programmers; rather it
means that data is concealed within a
• class so that it cannot be accessed mistakenly by functions
outside the class. The primary
• mechanism for hiding data is to put it in a class and make it
private. Private data or functions
• can only be accessed from within the class. Public data or
functions, on the other hand, are
• accessible from outside the class.
Hidden from Whom?
• Hidden from Whom?
• Don’t confuse data hiding with the security techniques used to protect
computer databases. To
• provide a security measure you might, for example, require a user to
supply a password before
• granting access to a database. The password is meant to keep
unauthorized or malevolent users
• from altering (or often even reading) the data.
• Data hiding, on the other hand, means hiding data from parts of the
program that don’t need to
• access it. More specifically, one class’s data is hidden from other classes.
Data hiding is designed
• to protect well-intentioned programmers from honest mistakes.
Programmers who really want to
• can figure out a way to access private data, but they will find it hard to do
so by accident.
Class Data
• The smallobj class contains one data item:
somedata, which is of type int. The data items
• within a class are called data members (or
sometimes member data). There can be any
number
• of data members in a class, just as there can be
any number of data items in a structure. The
• data member somedata follows the keyword
private, so it can be accessed from within the
• class, but not from outside.
Member Functions
• Member functions are functions that are included within a class. (In
some object-oriented
• languages, such as Smalltalk, member functions are called methods;
some writers use this term
• in C++ as well.) There are two member functions in smallobj:
setdata() and showdata().
• The function bodies of these functions have been written on the
same line as the braces that
• delimit them. You could also use the more traditional format for
these function definitions:
• void setdata(int d)
• {
• somedata = d;
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class shape_class
• {
• private:
• int radius,lenght,width;
• float cir_area,rect_area;
• public:
• void circle(int r)
• {
• radius=r;
• cout<<3.14*radius*radius;
• }
• void rectangle(int l,int w)
• {
• lenght=l;
• width=w;
• cout<<lenght*width;
• }};
Continue….
• int main()
• {
• shape_class c,r;
• int z,x,y;
• cin>>z>>x>>y;
• c.circle(z);
• r.rectangle(x,y);
• getch();
• }
Constructor
• A constructor is a member function that is
executed automatically whenever an object is
created. (The term constructor is sometimes
abbreviated ctor, especially in comments in
program listings.)
• Constructors are special type of functions that
have same name as class.
• Constructors have no return type.
Shape_class Program using
Constuctors
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class shape_class
• {
• private:
• int radius,lenght,width;
• float cir_area,rect_area;
• public:
• shape_class(int r)
• {
•
• radius=r;
• cout<<3.14*radius*radius;
• }
• shape_class(int l,int w)
• {
• lenght=l;
• width=w;
• cout<<lenght*width;
• }
continue
• int main()
• {
• shape_class c(3),r(4,5);
• getch();
• }
Destructors
• We’ve seen that a special member function—the
constructor—is called automatically when an
object is first created. You might guess that
another function is called automatically when an
object is destroyed. This is indeed the case. Such a
function is called a destructor. A destructor has
the same name as the constructor (which is the
same as the class name) but is preceded by a
tilde.
• class Foo
• {
• private:
• int data;
• public:
• Foo() //constructor (same name as class)
• {int data=0; }
• ~Foo() //destructor (same name with tilde)
• { }
• };
• Like constructors, destructors do not have a
return value. They also take no arguments
(the assumption being that there’s only one
way to destroy an object).
• The most common use of destructors is to
deallocate memory that was allocated for the
object by the constructor.
Constructor overloading
• Constructors that have same name but
different number of arguments is called
constructor overloading.
• Like simple function constructor can be
overloaded.
• If we have a class name shape_class then
overloaded constuctor may be the following
• Shape_class();
• Shape_class(variable_1);
• Shape_class(variable_1,variable_2);
• Shape_class(variable_n1,………,………);
Objects as an function arguments
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class add
• {
• private:
• int number1,number2;
• int sum;
• public:
• add()
• {
• sum=0;
• }
• void addition(add a,add b)
• {
• cin>>a.number1;
• cin>>a.number2;
• sum=a.number1+a.number2;
• cout<<sum;
• }
• };
• main()
• {
• add q,w;
• add t;
• t.addition(q,w);
• }
Friend Functions
• A friend function is a function that is not a
member of a class but has access to the class's
private and protected members.
• Keyword friend is used before friend function.
• Friend function take class objects as
aruguments.
Example
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class add
• {
• private:
• int number1,number2;
• public:
• add(int a,int b)
• {
• number2=b;
• number1=a;
• }
• friend int sum(add obj);
• };
• int sum(add obj)
• {
• int d=obj.number1+obj.number2;
• return d;
• }
• main()
• {
• add r(8,9);
• cout<<sum(r);
• }
Operator Overloading
• Adding extra functionality to a c++ operators is
called operator overloading.
Overloading Unary Operators(++,--)
• Syntax:
• Class_name operator ++or - - ()
++ operator overloading
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class opr_ov
• {
•
• private:
• int count;
• public:
• opr_ov()
• {
•
• count=0;
• }
• opr_ov operator ++ ()
• {
• ++count;
• Cout<<count;
• }
•
• };
• main()
• {
• opr_ov c1,c2;
• ++c1;
• ++c2;}
Binary operator overloading
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class op_ol
• {
• private:
• int num1,num2,value;
• public:
• void set_value()
• {
• cin>>num1;
• }
• op_ol operator - (op_ol ob)
• {
• op_ol t,p;
• t.value=num1-ob.num1;
• cout<<t.value;
• }
• };
• main()
• {
• op_ol obj1,obj2,result;
• obj1.set_value();
• obj2.set_value();
• result=obj1-obj2;
• }
INHERITANCE
• Inheritance is one of the key feature of object-
oriented programming including C++ which
allows user to create a new class(derived
class) from a existing class(base class). The
derived class inherits all feature from a base
class and it can have additional features of its
own.
Type of Inheritance:
• Public Inheritance: When deriving a class from a public
base class, public members of the base class become public
members of the derived class and protected members of
the base class become protected members of the derived
class. A base class's private members are never accessible
directly from a derived class, but can be accessed through
calls to the public and protected members of the base
class.
• Protected Inheritance: When deriving from a protected
base class, public and protected members of the base class
become protected members of the derived class.
• Private Inheritance: When deriving from a private base
class, public and protected members of the base class
become private members of the derived class.
Multiple inheritance
• Multiple inheritance is a feature of some
object-oriented computer programming
languages in which an object or class can
inherit characteristics and features from more
than one parent object or parent class.

More Related Content

What's hot

C++ classes
C++ classesC++ classes
C++ classes
imhammadali
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
Pranali Chaudhari
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Class and object
Class and objectClass and object
Class and object
Prof. Dr. K. Adisesha
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
nirajmandaliya
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
Baljit Saini
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
rumanatasnim415
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
shashank12march
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
rajveer_Pannu
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 

What's hot (20)

C++ classes
C++ classesC++ classes
C++ classes
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
C++ oop
C++ oopC++ oop
C++ oop
 
Inheritance
InheritanceInheritance
Inheritance
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Class and object
Class and objectClass and object
Class and object
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
class and objects
class and objectsclass and objects
class and objects
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 

Viewers also liked

Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
TICS
TICSTICS
TICS
yudysndvl
 
React, London JS Meetup, 11 Aug 2015
React, London JS Meetup, 11 Aug 2015React, London JS Meetup, 11 Aug 2015
React, London JS Meetup, 11 Aug 2015
Stuart Harris
 
開発経済学ゼミ論
開発経済学ゼミ論開発経済学ゼミ論
開発経済学ゼミ論
satoshiel
 
DArshan
DArshanDArshan
Holy prophet (S.A.W)
Holy prophet (S.A.W)Holy prophet (S.A.W)
Holy prophet (S.A.W)
M Huraira Hashim
 
Blog ppt
Blog pptBlog ppt
Blog ppt
madifushischool
 
UPDATED 1st Table -Aug.15,2015
UPDATED 1st Table -Aug.15,2015UPDATED 1st Table -Aug.15,2015
UPDATED 1st Table -Aug.15,2015
Yesh Lazarte
 
Resume
ResumeResume
Resume
Justin Jauk
 
MingfaTech's Five Series of Breaking - Through LED Coolers
MingfaTech's Five Series of Breaking - Through LED CoolersMingfaTech's Five Series of Breaking - Through LED Coolers
MingfaTech's Five Series of Breaking - Through LED Coolers
AlexThomasusa
 
Mi collage personal
Mi collage personalMi collage personal
Mi collage personal
Ricardo Reta
 

Viewers also liked (12)

Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
TICS
TICSTICS
TICS
 
React, London JS Meetup, 11 Aug 2015
React, London JS Meetup, 11 Aug 2015React, London JS Meetup, 11 Aug 2015
React, London JS Meetup, 11 Aug 2015
 
開発経済学ゼミ論
開発経済学ゼミ論開発経済学ゼミ論
開発経済学ゼミ論
 
DArshan
DArshanDArshan
DArshan
 
Commercial offer_TravelWIFI
Commercial offer_TravelWIFICommercial offer_TravelWIFI
Commercial offer_TravelWIFI
 
Holy prophet (S.A.W)
Holy prophet (S.A.W)Holy prophet (S.A.W)
Holy prophet (S.A.W)
 
Blog ppt
Blog pptBlog ppt
Blog ppt
 
UPDATED 1st Table -Aug.15,2015
UPDATED 1st Table -Aug.15,2015UPDATED 1st Table -Aug.15,2015
UPDATED 1st Table -Aug.15,2015
 
Resume
ResumeResume
Resume
 
MingfaTech's Five Series of Breaking - Through LED Coolers
MingfaTech's Five Series of Breaking - Through LED CoolersMingfaTech's Five Series of Breaking - Through LED Coolers
MingfaTech's Five Series of Breaking - Through LED Coolers
 
Mi collage personal
Mi collage personalMi collage personal
Mi collage personal
 

Similar to Object oriented programming in C++

Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
AshrithaRokkam
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Sikder Tahsin Al-Amin
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Oops concept on c#
Oops concept on c#Oops concept on c#
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Shailendra Veeru
 
Topic 3
Topic 3Topic 3
Topic 3
DiyarAldusky
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
anitashinde33
 
Introduction to c ++ part -1
Introduction to c ++   part -1Introduction to c ++   part -1
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
Michael Redlich
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
Michael Redlich
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
sai kumar
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
secondakay
 

Similar to Object oriented programming in C++ (20)

Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Topic 3
Topic 3Topic 3
Topic 3
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
Introduction to c ++ part -1
Introduction to c ++   part -1Introduction to c ++   part -1
Introduction to c ++ part -1
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
 

More from jehan1987

Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan1987
 
Artifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in oneArtifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in one
jehan1987
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
jehan1987
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
jehan1987
 
Assessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industryAssessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industry
jehan1987
 

More from jehan1987 (7)

Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Artifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in oneArtifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in one
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
Assessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industryAssessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industry
 

Recently uploaded

Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 

Recently uploaded (20)

Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 

Object oriented programming in C++

  • 2. Class • Class is the description of properties and behavior of a real world entity. •OR • Class is blueprint of objects
  • 3. Object • Object is instance of class. Object uses behavior and properties used in classes. • Functions in class are called member functions. • Data in class is called data members. • Data of class is private, while functions in class are public.
  • 4. • Private data can only accessed with in class, while public data can be accessed from main. • Public data can be accessed by objects of the same class.
  • 5. • // smallobj.cpp • // demonstrates a small, simple object • #include <iostream> • using namespace std; • //////////////////////////////////////////////////////////////// • class smallobj //define a class • { • private: • int somedata; //class data • public: • void setdata(int d) //member function to set data • { somedata = d; } • void showdata() //member function to display data • { cout << “Data is “ << somedata << endl; } • }; • //////////////////////////////////////////////////////////////// • int main() • { • smallobj s1, s2; //define two objects of class smallobj • s1.setdata(1066); //call member function to set data • s2.setdata(1776); • s1.showdata(); //call member function to display data • s2.showdata(); • return 0; • }
  • 6.
  • 7. private and public • The body of the class contains two unfamiliar keywords: private and public. What is their purpose? • A key feature of object-oriented programming is data hiding. This term does not refer to the • activities of particularly paranoid programmers; rather it means that data is concealed within a • class so that it cannot be accessed mistakenly by functions outside the class. The primary • mechanism for hiding data is to put it in a class and make it private. Private data or functions • can only be accessed from within the class. Public data or functions, on the other hand, are • accessible from outside the class.
  • 8. Hidden from Whom? • Hidden from Whom? • Don’t confuse data hiding with the security techniques used to protect computer databases. To • provide a security measure you might, for example, require a user to supply a password before • granting access to a database. The password is meant to keep unauthorized or malevolent users • from altering (or often even reading) the data. • Data hiding, on the other hand, means hiding data from parts of the program that don’t need to • access it. More specifically, one class’s data is hidden from other classes. Data hiding is designed • to protect well-intentioned programmers from honest mistakes. Programmers who really want to • can figure out a way to access private data, but they will find it hard to do so by accident.
  • 9.
  • 10. Class Data • The smallobj class contains one data item: somedata, which is of type int. The data items • within a class are called data members (or sometimes member data). There can be any number • of data members in a class, just as there can be any number of data items in a structure. The • data member somedata follows the keyword private, so it can be accessed from within the • class, but not from outside.
  • 11. Member Functions • Member functions are functions that are included within a class. (In some object-oriented • languages, such as Smalltalk, member functions are called methods; some writers use this term • in C++ as well.) There are two member functions in smallobj: setdata() and showdata(). • The function bodies of these functions have been written on the same line as the braces that • delimit them. You could also use the more traditional format for these function definitions: • void setdata(int d) • { • somedata = d;
  • 12.
  • 13.
  • 14.
  • 15. • #include<iostream> • #include<conio.h> • using namespace std; • class shape_class • { • private: • int radius,lenght,width; • float cir_area,rect_area; • public: • void circle(int r) • { • radius=r; • cout<<3.14*radius*radius; • } • void rectangle(int l,int w) • { • lenght=l; • width=w; • cout<<lenght*width; • }};
  • 16. Continue…. • int main() • { • shape_class c,r; • int z,x,y; • cin>>z>>x>>y; • c.circle(z); • r.rectangle(x,y); • getch(); • }
  • 17. Constructor • A constructor is a member function that is executed automatically whenever an object is created. (The term constructor is sometimes abbreviated ctor, especially in comments in program listings.)
  • 18. • Constructors are special type of functions that have same name as class. • Constructors have no return type.
  • 19. Shape_class Program using Constuctors • #include<iostream> • #include<conio.h> • using namespace std; • class shape_class • { • private: • int radius,lenght,width; • float cir_area,rect_area; • public: • shape_class(int r) • { • • radius=r; • cout<<3.14*radius*radius; • } • shape_class(int l,int w) • { • lenght=l; • width=w; • cout<<lenght*width; • }
  • 20. continue • int main() • { • shape_class c(3),r(4,5); • getch(); • }
  • 21. Destructors • We’ve seen that a special member function—the constructor—is called automatically when an object is first created. You might guess that another function is called automatically when an object is destroyed. This is indeed the case. Such a function is called a destructor. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde.
  • 22. • class Foo • { • private: • int data; • public: • Foo() //constructor (same name as class) • {int data=0; } • ~Foo() //destructor (same name with tilde) • { } • };
  • 23. • Like constructors, destructors do not have a return value. They also take no arguments (the assumption being that there’s only one way to destroy an object). • The most common use of destructors is to deallocate memory that was allocated for the object by the constructor.
  • 24. Constructor overloading • Constructors that have same name but different number of arguments is called constructor overloading. • Like simple function constructor can be overloaded.
  • 25. • If we have a class name shape_class then overloaded constuctor may be the following • Shape_class(); • Shape_class(variable_1); • Shape_class(variable_1,variable_2); • Shape_class(variable_n1,………,………);
  • 26. Objects as an function arguments • #include<iostream> • #include<conio.h> • using namespace std; • class add • { • private: • int number1,number2; • int sum; • public: • add() • { • sum=0; • } • void addition(add a,add b) • { • cin>>a.number1; • cin>>a.number2; • sum=a.number1+a.number2; • cout<<sum; • } • }; • main() • { • add q,w; • add t; • t.addition(q,w); • }
  • 27. Friend Functions • A friend function is a function that is not a member of a class but has access to the class's private and protected members. • Keyword friend is used before friend function. • Friend function take class objects as aruguments.
  • 28. Example • #include<iostream> • #include<conio.h> • using namespace std; • class add • { • private: • int number1,number2; • public: • add(int a,int b) • { • number2=b; • number1=a; • } • friend int sum(add obj); • }; • int sum(add obj) • { • int d=obj.number1+obj.number2; • return d; • } • main() • { • add r(8,9); • cout<<sum(r); • }
  • 29. Operator Overloading • Adding extra functionality to a c++ operators is called operator overloading.
  • 30. Overloading Unary Operators(++,--) • Syntax: • Class_name operator ++or - - ()
  • 31. ++ operator overloading • #include<iostream> • #include<conio.h> • using namespace std; • class opr_ov • { • • private: • int count; • public: • opr_ov() • { • • count=0; • } • opr_ov operator ++ () • { • ++count; • Cout<<count; • } • • };
  • 32. • main() • { • opr_ov c1,c2; • ++c1; • ++c2;}
  • 33. Binary operator overloading • #include<iostream> • #include<conio.h> • using namespace std; • class op_ol • { • private: • int num1,num2,value; • public: • void set_value() • { • cin>>num1; • } • op_ol operator - (op_ol ob) • { • op_ol t,p; • t.value=num1-ob.num1; • cout<<t.value; • } • }; • main() • { • op_ol obj1,obj2,result; • obj1.set_value(); • obj2.set_value(); • result=obj1-obj2; • }
  • 34. INHERITANCE • Inheritance is one of the key feature of object- oriented programming including C++ which allows user to create a new class(derived class) from a existing class(base class). The derived class inherits all feature from a base class and it can have additional features of its own.
  • 35.
  • 36. Type of Inheritance: • Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. • Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. • Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  • 37. Multiple inheritance • Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.