SlideShare a Scribd company logo
26-07-2018 08:24:48 PM
vishnuvishnushaji5@gmail.com [1/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
OBJECT-ORIENTED
PROGRAMMING WITH C++
VISHNU P S
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [2/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Procedure Oriented Programming Object Oriented Programming
Divided Into In POP, program is divided into small parts
called functions.
In OOP, program is divided into parts called
objects.
Importance In POP, Importance is not given to data but
to functions as well as sequence of actions
to be done.
In OOP, Importance is given to the data
rather than procedures or functions because
it works as a real world.
Approach POP follows Top Down approach. OOP follows Bottom Up approach.
Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public,
Private, Protected, etc.
Data Moving In POP, Data can move freely from
function to function in the system.
In OOP, objects can move and communicate
with each other through member functions.
Expansion To add new data and function in POP is
not so easy.
OOP provides an easy way to add new data
and function.
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [3/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Contd…
Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the form of
Function Overloading and Operator
Overloading.
Examples Example of POP are : C, VB, FORTRAN,
Pascal.
Example of OOP are : C++, JAVA, VB.NET,
C#.NET
Data Access In POP, Most function uses Global data for
sharing that can be accessed freely from
function to function in the system.
In OOP, data can not move easily from
function to function,it can be kept public or
private so we can control the access of data.
Data Hiding POP does not have any proper way for
hiding data so it is less secure.
OOP provides Data Hiding so provides more
security.
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [4/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
BASIC CONCEPTS OF OBJECT-ORIENTED PROGTAMMING
OBJECTS CLASSES
DATA
ABSTRACTION
DATA
ENCAPSULATION
INHERITANCE POLIYMORPHISM
DYNAMIC
BINDING
MESSAGE
PASSING
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [5/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
#include<iostream.h>
main()
{
cout<<“Hello World!”;
return 0;
}
#include<stdio.h>
int main()
{
printf(“Hello World!”);
return 0;
}
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [6/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
MEMMORY ALLOCATION
STATIC DYNAMIC
 is allocated at the start of your
program
 the global variables or global
available arrays
 is allocated at runtime
 malloc(), calloc(), new
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [7/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
C
•malloc()
•calloc()
C++
•new
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [8/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
new
 C++ uses new operator to allocate memory dynamically at run time.
Syntax
pointer_variable = new data_type;
Example
int *p;
p = new int;
int *q = new int;
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [9/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
delete
 Destroyed by using delete.
Syntax
delete pointer_variable;
Example
delete p;
delete q;
26-07-2018 08:24:49 PM
vishnuvishnushaji5@gmail.com [11/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
CLASSES AND OBJECTS
CLASS OBJECT
 It is a user defined data type
 which holds its own data members
and member functions
 which can be accessed and used by
creating an instance of that class
 A class is like a blueprint for an
object.
is an instance of a Class.
When a class is defined, no memory is
allocated but when it is instantiated
(i.e. an object is created) memory is
allocated.
26-07-2018 08:24:50 PM
vishnuvishnushaji5@gmail.com [12/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
DEFINING CLASS
26-07-2018 08:24:50 PM
vishnuvishnushaji5@gmail.com [13/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
DECLARING OBJECTS
When a class is defined, only the specification for the object is
defined; no memory or storage is allocated. To use the data and
access functions defined in the class, you need to create objects.
ClassName ObjectName;
26-07-2018 08:24:50 PM
vishnuvishnushaji5@gmail.com [14/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
ACCESSING DATA MEMBERS AND MEMBER FUNCTIONS
ACCESSING DATA MEMBERS
The data members and member functions of class can be accessed using the dot(‘.’)
operator with the object. For example if the name of object is obj and you want to access
the member function with the name printName() then you will have to write
obj.printName() .
The public data members are also accessed in the same way given however the private data members
are not allowed to be accessed directly by the object. Accessing a data member depends solely on the
access control of that data member.
26-07-2018 08:24:50 PM
vishnuvishnushaji5@gmail.com [15/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
// C++ program to demonstrate
// accessing of data members
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
// Access specifier
public:
// Data Members
string name;
// Member Functions()
void printname()
{
cout << “Name is: " << name;
}
};
int main() {
// Declare an object of class
geeks
Geeks obj1;
// accessing data member
obj1.name = "Abhi";
// accessing member function
obj1.printname();
return 0;
}
26-07-2018 08:24:50 PM
vishnuvishnushaji5@gmail.com [17/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
The capability of a class to derive properties
and characteristics from another class is
called Inheritance. Inheritance is one of the
most important feature of Object Oriented
Programming.
INHERITANCE IN C++
Sub Class :
The class that inherits properties from
another class is called Sub class or
Derived Class.
Super Class :
The class whose properties are inherited
by sub class is called Base Class or Super
class.
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [18/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Why and when to use inheritance?
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [19/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [20/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Implementing inheritance in C++
For creating a sub-class which is inherited from the base class we have to follow the below
syntax.
class subclass_name : access_mode base_class_name
{
//body of subclass
};
subclass_name is the name of the sub class, access_mode is the mode in which you
want to inherit this sub class for example: public, private etc. and base_class_name is
the name of the base class from which you want to inherit the sub class.
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [21/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
// C++ program to demonstrate
implementation
// of Inheritance
#include <iostream.h>
using namespace std;
//Base class
class Parent
{
public:
int id_p;
};
// Sub class inheriting from Base
Class(Parent)
class Child : public Parent
{
public:
int id_c;
};
//main function
int main()
{
Child obj1;
// An object of class child has
all data members
// and member functions of class
parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is "
<< obj1.id_c << endl;
cout << "Parent id is "
<< obj1.id_p << endl;
return 0;
}
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [22/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Modes of Inheritance
Public mode:
If we derive a sub class from a public
base class. Then the public member
of the base class will become public
in the derived class and protected
members of the base class will
become protected in derived class.
Private members of the base class
will never get inherited in sub class.
Protected mode:
If we derive a sub class from a
Protected base class. Then both
public member and protected
members of the base class will
become protected in derived class.
Private members of the base class
will never get inherited in sub class.
Private mode:
If we derive a sub class from a
Private base class. Then both public
member and protected members of
the base class will become Private in
derived class. Private members of
the base class will never get
inherited in sub class.
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [23/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
The below table summarizes the above three modes and shows the access specifier of the members of base
class in the sub class when derived in public, protected and private modes:
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [24/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
TYPES OF INHERITANCE IN C++
Single Inheritance Multiple Inheritance
Multilevel Inheritance Hierarchical Inheritance
Hybrid (Virtual) Inheritance
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [25/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e.
one sub class is inherited by one base class only.
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [26/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more
than one classes. i.e one sub class is inherited from more than one base
classes.
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [27/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Multilevel Inheritance
In this type of inheritance, a derived class is created from another derived
class.
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [28/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Hierarchical Inheritance
In this type of inheritance, more than one sub class is inherited from a
single base class. i.e. more than one derived class is created from a single
base class.
26-07-2018 08:24:52 PM
vishnuvishnushaji5@gmail.com [29/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
Hybrid (Virtual) Inheritance
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance.
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [31/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
DYNAMIC BINDING
• THE COMPILER MATCHES THE FUNCTION CALL WITH THE CORRECT
FUNCTION DEFINITION AT RUNTIME. IT IS ALSO KNOWN AS LATE BINDING
OR RUNTIME BINDING.
• IN LATE BINDING, THE COMPILER IDENTIFIES THE TYPE OF OBJECT AT
RUNTIME AND THEN MATCHES THE FUNCTION CALL WITH THE CORRECT
FUNCTION DEFINITION.
• THIS CAN BE ACHIEVED BY DECLARING A VIRTUAL FUNCTION.
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [32/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
#include <iostream>
using namespace std;
class Animals
{
public:
void sound()
{
cout << "This is parent class" <<
endl;
}
};
class Dogs : public Animals
{
public:
void sound()
{
cout << "Dogs bark" << endl;
}
};
int main()
{
Animals *a;
Dogs d;
a= &d;
a -> sound(); // early binding
return 0;
}
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [33/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
FRIEND CLASS
A friend class can access private and protected members of other class in which it is
declared as friend. It is sometimes useful to allow a particular class to access private
members of other class. For example a LinkedList class may be allowed to access private
members of Node.
FRIEND FUNCTION
Like friend class, a friend function can be given special grant to access private and
protected members. A friend function can be:
a) A method of another class
b) A global function
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [34/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
class Node
{
private:
int key;
Node *next;
/* Other members of Node Class */
friend class LinkedList; // Now class
LinkedList can
// access
private members of Node
}
class Node
{
private:
int key;
Node *next;
/* Other members of Node Class */
friend int LinkedList::search(); // Only
search() of linkedList
// can
access internal members
};
FRIEND CLASS FRIEND FUNCTION
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [36/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
ENCAPSULATION IN C++
Encapsulation is defined as wrapping up of data and information under a single unit
(Encapsulation is defined as binding together the data and the functions that manipulates
them.)
Consider a real life example of encapsulation, in a company there
are different sections like the accounts section, finance section,
sales section etc. The finance section handles all the financial
transactions and keep records of all the data related to finance.
Similarly the sales section handles all the sales related activities
and keep records of all the sales. Now there may arise a situation
when for some reason an official from finance section needs all the
data about sales in a particular month. In this case, he is not
allowed to directly access the data of sales section. He will first
have to contact some other officer in the sales section and then
request him to give the particular data. This is what encapsulation
is. Here the data of sales section and the employees that can
manipulate them are wrapped under a single name “sales section”.
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [37/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
TEMPLATES IN C++
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [38/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
TEMPLATES IN C++
 Template is simple and yet very powerful tool in C++.
 The simple idea is to pass data type as a parameter so that we don’t need to write same
code for different data types.
 For example a software company may need sort() for different data types. Rather than
writing and maintaining the multiple codes, we can write one sort() and pass data type as a
parameter.
C++ adds two new keywords to support templates: ‘template’
CLASS TEMPLATES
FUNCTION TEMPLATES
26-07-2018 08:24:53 PM
vishnuvishnushaji5@gmail.com [39/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
How templates work?
Templates are expended at compiler time.
This is like macros. The difference is, compiler
does type checking before template
expansion. The idea is simple, source code
contains only function/class, but compiled
code may contain multiple copies of same
function/class.
26-07-2018 08:24:54 PM
vishnuvishnushaji5@gmail.com [41/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
C++ Exception Handling
 An exception is a problem that arises during the execution of a program.
 A C++ exception is a response to an exceptional circumstance that arises while a
program is running, such as an attempt to divide by zero.
 Exceptions provide a way to transfer control from one part of a program to another.
 C++ exception handling is built upon three keywords: try, catch, and throw.
26-07-2018 08:24:54 PM
vishnuvishnushaji5@gmail.com [42/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
 throw − A program throws an exception when a problem shows up. This is done using
a throw keyword.
 catch − A program catches an exception with an exception handler at the place in a
program where you want to handle the problem. The catch keyword indicates the
catching of an exception.
 try − A try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more catch blocks.
26-07-2018 08:24:54 PM
vishnuvishnushaji5@gmail.com [43/38]
OBJECT-ORIENTED PROGRAMMING WITH C++
REFERENCE
https://www.tutorialspoint.com/
https://www.geeksforgeeks.org/
https://en.wikipedia.org/wiki/Main_Page

More Related Content

PPTX
Object oriented programming in C++
PPTX
Learn Concept of Class and Object in C# Part 3
PDF
Object Oriented Programming using C++ Part I
PDF
Inheritance
PPTX
C++ Object Oriented Programming
PDF
A COMPLETE FILE FOR C++
PPTX
class and objects
PPT
Object-Oriented Programming Using C++
Object oriented programming in C++
Learn Concept of Class and Object in C# Part 3
Object Oriented Programming using C++ Part I
Inheritance
C++ Object Oriented Programming
A COMPLETE FILE FOR C++
class and objects
Object-Oriented Programming Using C++

What's hot (20)

PPT
C++ classes
PDF
Object oriented concepts
PPTX
Object Oriented Programming Concepts
PPT
Inheritance : Extending Classes
PDF
C++ Object oriented concepts & programming
PPSX
Support for Object-Oriented Programming (OOP) in C++
PPTX
Oop c++class(final).ppt
PDF
Classes and objects
PPT
Classes and data abstraction
PPTX
classes and objects in C++
PPTX
Static Data Members and Member Functions
PPTX
Chapter 06 constructors and destructors
PPT
Data abstraction and object orientation
PPT
C++ oop
PPT
Object oriented programming using c++
PDF
Programming in c++
PPTX
OOPS Basics With Example
PPT
C++ classes tutorials
PDF
Implementation of oop concept in c++
PPTX
OOPS IN C++
C++ classes
Object oriented concepts
Object Oriented Programming Concepts
Inheritance : Extending Classes
C++ Object oriented concepts & programming
Support for Object-Oriented Programming (OOP) in C++
Oop c++class(final).ppt
Classes and objects
Classes and data abstraction
classes and objects in C++
Static Data Members and Member Functions
Chapter 06 constructors and destructors
Data abstraction and object orientation
C++ oop
Object oriented programming using c++
Programming in c++
OOPS Basics With Example
C++ classes tutorials
Implementation of oop concept in c++
OOPS IN C++
Ad

Similar to Object Oriented Programming With C++ (20)

PPTX
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
PPT
The smartpath information systems c plus plus
PPTX
Opp concept in c++
PPTX
Object oriented programming. (1).pptx
PPTX
Procedure Oriented programming Object Oriented programming Basic Concept of ...
PPTX
OOP CHAPTER object oreinted programming using c++
PPT
PPTX
Rajib Ali Presentation on object oreitation oop.pptx
PPTX
Object Oriented Programming using C++ - OOPS concepts using C++ programming l...
PPTX
Oo ps concepts in c++
PPTX
An introduction to object-oriented programming.pptx
PPTX
CAP444-Unit-3-Polymorphism.pptx
PPTX
Four Pillers Of OOPS
PPTX
Object oriented programing
PPT
Oops and c fundamentals
PDF
C++
PPT
2. oop with c++ get 410 day 2
PPT
the education purpose of software C++.ppt
PPT
C++ basic intro on c++ programming language ppt
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
The smartpath information systems c plus plus
Opp concept in c++
Object oriented programming. (1).pptx
Procedure Oriented programming Object Oriented programming Basic Concept of ...
OOP CHAPTER object oreinted programming using c++
Rajib Ali Presentation on object oreitation oop.pptx
Object Oriented Programming using C++ - OOPS concepts using C++ programming l...
Oo ps concepts in c++
An introduction to object-oriented programming.pptx
CAP444-Unit-3-Polymorphism.pptx
Four Pillers Of OOPS
Object oriented programing
Oops and c fundamentals
C++
2. oop with c++ get 410 day 2
the education purpose of software C++.ppt
C++ basic intro on c++ programming language ppt
Ad

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Basic Mud Logging Guide for educational purpose
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
RMMM.pdf make it easy to upload and study
Basic Mud Logging Guide for educational purpose
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Structure & Organelles in detailed.
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPH.pptx obstetrics and gynecology in nursing
Renaissance Architecture: A Journey from Faith to Humanism
Supply Chain Operations Speaking Notes -ICLT Program

Object Oriented Programming With C++

  • 1. 26-07-2018 08:24:48 PM vishnuvishnushaji5@gmail.com [1/38] OBJECT-ORIENTED PROGRAMMING WITH C++ OBJECT-ORIENTED PROGRAMMING WITH C++ VISHNU P S
  • 2. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [2/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Procedure Oriented Programming Object Oriented Programming Divided Into In POP, program is divided into small parts called functions. In OOP, program is divided into parts called objects. Importance In POP, Importance is not given to data but to functions as well as sequence of actions to be done. In OOP, Importance is given to the data rather than procedures or functions because it works as a real world. Approach POP follows Top Down approach. OOP follows Bottom Up approach. Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public, Private, Protected, etc. Data Moving In POP, Data can move freely from function to function in the system. In OOP, objects can move and communicate with each other through member functions. Expansion To add new data and function in POP is not so easy. OOP provides an easy way to add new data and function.
  • 3. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [3/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Contd… Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the form of Function Overloading and Operator Overloading. Examples Example of POP are : C, VB, FORTRAN, Pascal. Example of OOP are : C++, JAVA, VB.NET, C#.NET Data Access In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system. In OOP, data can not move easily from function to function,it can be kept public or private so we can control the access of data. Data Hiding POP does not have any proper way for hiding data so it is less secure. OOP provides Data Hiding so provides more security.
  • 4. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [4/38] OBJECT-ORIENTED PROGRAMMING WITH C++ BASIC CONCEPTS OF OBJECT-ORIENTED PROGTAMMING OBJECTS CLASSES DATA ABSTRACTION DATA ENCAPSULATION INHERITANCE POLIYMORPHISM DYNAMIC BINDING MESSAGE PASSING
  • 5. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [5/38] OBJECT-ORIENTED PROGRAMMING WITH C++ #include<iostream.h> main() { cout<<“Hello World!”; return 0; } #include<stdio.h> int main() { printf(“Hello World!”); return 0; }
  • 6. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [6/38] OBJECT-ORIENTED PROGRAMMING WITH C++ MEMMORY ALLOCATION STATIC DYNAMIC  is allocated at the start of your program  the global variables or global available arrays  is allocated at runtime  malloc(), calloc(), new
  • 7. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [7/38] OBJECT-ORIENTED PROGRAMMING WITH C++ C •malloc() •calloc() C++ •new
  • 8. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [8/38] OBJECT-ORIENTED PROGRAMMING WITH C++ new  C++ uses new operator to allocate memory dynamically at run time. Syntax pointer_variable = new data_type; Example int *p; p = new int; int *q = new int;
  • 9. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [9/38] OBJECT-ORIENTED PROGRAMMING WITH C++ delete  Destroyed by using delete. Syntax delete pointer_variable; Example delete p; delete q;
  • 10. 26-07-2018 08:24:49 PM vishnuvishnushaji5@gmail.com [11/38] OBJECT-ORIENTED PROGRAMMING WITH C++ CLASSES AND OBJECTS CLASS OBJECT  It is a user defined data type  which holds its own data members and member functions  which can be accessed and used by creating an instance of that class  A class is like a blueprint for an object. is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
  • 11. 26-07-2018 08:24:50 PM vishnuvishnushaji5@gmail.com [12/38] OBJECT-ORIENTED PROGRAMMING WITH C++ DEFINING CLASS
  • 12. 26-07-2018 08:24:50 PM vishnuvishnushaji5@gmail.com [13/38] OBJECT-ORIENTED PROGRAMMING WITH C++ DECLARING OBJECTS When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects. ClassName ObjectName;
  • 13. 26-07-2018 08:24:50 PM vishnuvishnushaji5@gmail.com [14/38] OBJECT-ORIENTED PROGRAMMING WITH C++ ACCESSING DATA MEMBERS AND MEMBER FUNCTIONS ACCESSING DATA MEMBERS The data members and member functions of class can be accessed using the dot(‘.’) operator with the object. For example if the name of object is obj and you want to access the member function with the name printName() then you will have to write obj.printName() . The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object. Accessing a data member depends solely on the access control of that data member.
  • 14. 26-07-2018 08:24:50 PM vishnuvishnushaji5@gmail.com [15/38] OBJECT-ORIENTED PROGRAMMING WITH C++ // C++ program to demonstrate // accessing of data members #include <bits/stdc++.h> using namespace std; class Geeks { // Access specifier public: // Data Members string name; // Member Functions() void printname() { cout << “Name is: " << name; } }; int main() { // Declare an object of class geeks Geeks obj1; // accessing data member obj1.name = "Abhi"; // accessing member function obj1.printname(); return 0; }
  • 15. 26-07-2018 08:24:50 PM vishnuvishnushaji5@gmail.com [17/38] OBJECT-ORIENTED PROGRAMMING WITH C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important feature of Object Oriented Programming. INHERITANCE IN C++ Sub Class : The class that inherits properties from another class is called Sub class or Derived Class. Super Class : The class whose properties are inherited by sub class is called Base Class or Super class.
  • 16. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [18/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Why and when to use inheritance?
  • 17. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [19/38] OBJECT-ORIENTED PROGRAMMING WITH C++
  • 18. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [20/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Implementing inheritance in C++ For creating a sub-class which is inherited from the base class we have to follow the below syntax. class subclass_name : access_mode base_class_name { //body of subclass }; subclass_name is the name of the sub class, access_mode is the mode in which you want to inherit this sub class for example: public, private etc. and base_class_name is the name of the base class from which you want to inherit the sub class.
  • 19. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [21/38] OBJECT-ORIENTED PROGRAMMING WITH C++ // C++ program to demonstrate implementation // of Inheritance #include <iostream.h> using namespace std; //Base class class Parent { public: int id_p; }; // Sub class inheriting from Base Class(Parent) class Child : public Parent { public: int id_c; }; //main function int main() { Child obj1; // An object of class child has all data members // and member functions of class parent obj1.id_c = 7; obj1.id_p = 91; cout << "Child id is " << obj1.id_c << endl; cout << "Parent id is " << obj1.id_p << endl; return 0; }
  • 20. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [22/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Modes of Inheritance Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class. Private members of the base class will never get inherited in sub class. Protected mode: If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class. Private members of the base class will never get inherited in sub class. Private mode: If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class. Private members of the base class will never get inherited in sub class.
  • 21. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [23/38] OBJECT-ORIENTED PROGRAMMING WITH C++ The below table summarizes the above three modes and shows the access specifier of the members of base class in the sub class when derived in public, protected and private modes:
  • 22. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [24/38] OBJECT-ORIENTED PROGRAMMING WITH C++ TYPES OF INHERITANCE IN C++ Single Inheritance Multiple Inheritance Multilevel Inheritance Hierarchical Inheritance Hybrid (Virtual) Inheritance
  • 23. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [25/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Single Inheritance In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.
  • 24. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [26/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Multiple Inheritance Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.
  • 25. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [27/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Multilevel Inheritance In this type of inheritance, a derived class is created from another derived class.
  • 26. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [28/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Hierarchical Inheritance In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.
  • 27. 26-07-2018 08:24:52 PM vishnuvishnushaji5@gmail.com [29/38] OBJECT-ORIENTED PROGRAMMING WITH C++ Hybrid (Virtual) Inheritance Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
  • 28. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [31/38] OBJECT-ORIENTED PROGRAMMING WITH C++ DYNAMIC BINDING • THE COMPILER MATCHES THE FUNCTION CALL WITH THE CORRECT FUNCTION DEFINITION AT RUNTIME. IT IS ALSO KNOWN AS LATE BINDING OR RUNTIME BINDING. • IN LATE BINDING, THE COMPILER IDENTIFIES THE TYPE OF OBJECT AT RUNTIME AND THEN MATCHES THE FUNCTION CALL WITH THE CORRECT FUNCTION DEFINITION. • THIS CAN BE ACHIEVED BY DECLARING A VIRTUAL FUNCTION.
  • 29. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [32/38] OBJECT-ORIENTED PROGRAMMING WITH C++ #include <iostream> using namespace std; class Animals { public: void sound() { cout << "This is parent class" << endl; } }; class Dogs : public Animals { public: void sound() { cout << "Dogs bark" << endl; } }; int main() { Animals *a; Dogs d; a= &d; a -> sound(); // early binding return 0; }
  • 30. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [33/38] OBJECT-ORIENTED PROGRAMMING WITH C++ FRIEND CLASS A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example a LinkedList class may be allowed to access private members of Node. FRIEND FUNCTION Like friend class, a friend function can be given special grant to access private and protected members. A friend function can be: a) A method of another class b) A global function
  • 31. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [34/38] OBJECT-ORIENTED PROGRAMMING WITH C++ class Node { private: int key; Node *next; /* Other members of Node Class */ friend class LinkedList; // Now class LinkedList can // access private members of Node } class Node { private: int key; Node *next; /* Other members of Node Class */ friend int LinkedList::search(); // Only search() of linkedList // can access internal members }; FRIEND CLASS FRIEND FUNCTION
  • 32. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [36/38] OBJECT-ORIENTED PROGRAMMING WITH C++ ENCAPSULATION IN C++ Encapsulation is defined as wrapping up of data and information under a single unit (Encapsulation is defined as binding together the data and the functions that manipulates them.) Consider a real life example of encapsulation, in a company there are different sections like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keep records of all the data related to finance. Similarly the sales section handles all the sales related activities and keep records of all the sales. Now there may arise a situation when for some reason an official from finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of sales section and the employees that can manipulate them are wrapped under a single name “sales section”.
  • 33. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [37/38] OBJECT-ORIENTED PROGRAMMING WITH C++ TEMPLATES IN C++
  • 34. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [38/38] OBJECT-ORIENTED PROGRAMMING WITH C++ TEMPLATES IN C++  Template is simple and yet very powerful tool in C++.  The simple idea is to pass data type as a parameter so that we don’t need to write same code for different data types.  For example a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter. C++ adds two new keywords to support templates: ‘template’ CLASS TEMPLATES FUNCTION TEMPLATES
  • 35. 26-07-2018 08:24:53 PM vishnuvishnushaji5@gmail.com [39/38] OBJECT-ORIENTED PROGRAMMING WITH C++ How templates work? Templates are expended at compiler time. This is like macros. The difference is, compiler does type checking before template expansion. The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of same function/class.
  • 36. 26-07-2018 08:24:54 PM vishnuvishnushaji5@gmail.com [41/38] OBJECT-ORIENTED PROGRAMMING WITH C++ C++ Exception Handling  An exception is a problem that arises during the execution of a program.  A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.  Exceptions provide a way to transfer control from one part of a program to another.  C++ exception handling is built upon three keywords: try, catch, and throw.
  • 37. 26-07-2018 08:24:54 PM vishnuvishnushaji5@gmail.com [42/38] OBJECT-ORIENTED PROGRAMMING WITH C++  throw − A program throws an exception when a problem shows up. This is done using a throw keyword.  catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.  try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
  • 38. 26-07-2018 08:24:54 PM vishnuvishnushaji5@gmail.com [43/38] OBJECT-ORIENTED PROGRAMMING WITH C++ REFERENCE https://www.tutorialspoint.com/ https://www.geeksforgeeks.org/ https://en.wikipedia.org/wiki/Main_Page