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

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++

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++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
 
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 onejehan1987
 
Java interfaces
Java interfacesJava interfaces
Java interfacesjehan1987
 
Complete java swing
Complete java swingComplete java swing
Complete java swingjehan1987
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreadingjehan1987
 
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 Onejehan1987
 
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 industryjehan1987
 

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

ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 

Recently uploaded (20)

ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.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; • }
  • 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.