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;
• }
Object oriented programming in C++
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.
Object oriented programming in C++
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;
Object oriented programming in C++
Object oriented programming in C++
Object oriented programming in C++
• #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.
Object oriented programming in C++
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

PDF
A COMPLETE FILE FOR C++
PDF
Object Oriented Programming With C++
PPT
Object oriented programming using c++
PPT
Object-Oriented Programming Using C++
PPTX
Learn Concept of Class and Object in C# Part 3
PPTX
C++ Object Oriented Programming
PPTX
classes and objects in C++
PDF
Classes and objects
A COMPLETE FILE FOR C++
Object Oriented Programming With C++
Object oriented programming using c++
Object-Oriented Programming Using C++
Learn Concept of Class and Object in C# Part 3
C++ Object Oriented Programming
classes and objects in C++
Classes and objects

What's hot (20)

PPT
C++ classes
PDF
Object oriented concepts
PPT
C++ oop
PDF
Inheritance
PPTX
PDF
Object Oriented Programming using C++ Part III
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PDF
Class and object
PDF
C++ Object oriented concepts & programming
PPTX
Oop c++class(final).ppt
PDF
Constructors and destructors
PPTX
class and objects
PDF
Programming in c++
PPTX
Chapter 05 classes and objects
PPTX
Lecture 4. mte 407
PDF
Function overloading ppt
PPTX
Classes and objects till 16 aug
PPTX
Chapter 06 constructors and destructors
PPTX
Classes and objects
PPTX
C++ And Object in lecture3
C++ classes
Object oriented concepts
C++ oop
Inheritance
Object Oriented Programming using C++ Part III
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Class and object
C++ Object oriented concepts & programming
Oop c++class(final).ppt
Constructors and destructors
class and objects
Programming in c++
Chapter 05 classes and objects
Lecture 4. mte 407
Function overloading ppt
Classes and objects till 16 aug
Chapter 06 constructors and destructors
Classes and objects
C++ And Object in lecture3
Ad

Viewers also liked (12)

PPT
Oops lecture 1
PDF
TICS
PDF
React, London JS Meetup, 11 Aug 2015
PPTX
開発経済学ゼミ論
DOCX
PDF
Commercial offer_TravelWIFI
PPTX
Holy prophet (S.A.W)
PPTX
Blog ppt
PPTX
UPDATED 1st Table -Aug.15,2015
PDF
Resume
PDF
MingfaTech's Five Series of Breaking - Through LED Coolers
PPTX
Mi collage personal
Oops lecture 1
TICS
React, London JS Meetup, 11 Aug 2015
開発経済学ゼミ論
Commercial offer_TravelWIFI
Holy prophet (S.A.W)
Blog ppt
UPDATED 1st Table -Aug.15,2015
Resume
MingfaTech's Five Series of Breaking - Through LED Coolers
Mi collage personal
Ad

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

PDF
Introduction to C++ Class & Objects. Book Notes
PDF
oopm 2.pdf
PPTX
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
PPTX
Class and object
PPTX
Oop objects_classes
PPTX
OOPs & C++ UNIT 3
PPTX
Classes and objects
PPTX
oopusingc.pptx
PPTX
Introduction to Fundamental of Class.pptx
PPTX
Object Oriented Programming Using C++
PPTX
Concept of Object-Oriented in C++
PPTX
Classes and objects
PPT
PPTX
class c++
PPSX
Arre tari mano bhosko ka pikina tari ma no piko
PPTX
05 Object Oriented Concept Presentation.pptx
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPTX
Class and object
PDF
Lab 4 (1).pdf
PPT
Classes and objects
Introduction to C++ Class & Objects. Book Notes
oopm 2.pdf
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
Class and object
Oop objects_classes
OOPs & C++ UNIT 3
Classes and objects
oopusingc.pptx
Introduction to Fundamental of Class.pptx
Object Oriented Programming Using C++
Concept of Object-Oriented in C++
Classes and objects
class c++
Arre tari mano bhosko ka pikina tari ma no piko
05 Object Oriented Concept Presentation.pptx
classandobjectunit2-150824133722-lva1-app6891.ppt
Class and object
Lab 4 (1).pdf
Classes and objects

More from jehan1987 (7)

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

Recently uploaded (20)

PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
From loneliness to social connection charting
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Renaissance Architecture: A Journey from Faith to Humanism
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
From loneliness to social connection charting
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
How to Manage Starshipit in Odoo 18 - Odoo Slides
Week 4 Term 3 Study Techniques revisited.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
STATICS OF THE RIGID BODIES Hibbelers.pdf
Module 3: Health Systems Tutorial Slides S2 2025
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Open Quiz Monsoon Mind Game Prelims.pptx

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; • }
  • 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.
  • 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;
  • 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.
  • 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.