SlideShare a Scribd company logo
OBJECT
ORIENTED
PROGRAMMING
FPL II Unit II
SPPU-2017/18
Prof. Ansari Aadil
Prof. Ansari Aadil S
Object
 OBJECT is an identifiable entity with some
characteristics and behavior.
 In OOP programming object represents a entity that
can store data and has its interface through functions.
 CLASS is a template/blue-print representing a group of
objects that share common properties and
relationships.
Prof. Ansari Aadil S
Structure of an
Object Oriented
Program
Prof. Ansari Aadil S
Features of OOP
• More emphasis on data rather than procedure.
• Programs are divided into object.
• Function and data are tied together in a data
structure.
• Objects communicate with each other through
Functions.
• Data is hidden or cannot be access by external
function.
Prof. Ansari Aadil S
Principle of OOPS
• Class:
It is type or a category of things.
It is similar to a structure with the difference that it
can also have function.
Ex. Class of Cars. There may be many cars with different
names and brand but all of them will share some
common properties like all of them will have 4
wheels, Speed Limit, Mileage range etc.
Prof. Ansari Aadil S
OBJECT : It is an instance of class. Ex. Class fruit can have apple as its object.
ABSTRACTION refers to the act of representing essential features without
including the background details or explanations.
ENCAPSULATION :The wrapping up of data and operations / functions (that
operate on the data ) into a single unit (called class) is known as
ENCAPSULATION.
INHERITANCE: Inheritance is the capability of one class of things to inherit
capabilities or properties from other class
POLYMORPHISM: Multiple form of the same thing. It is the ability for a
message or data to be processed in more than one form.
Prof. Ansari Aadil S
Structure of Object Oriented Program in
c++
#include<iostream.h>
class class-name
{
access-specifiers…
int i,j;
float f;
char ch;
double b;
access-specifiers…
void function-name()
{
statement 1;
statement 2;
}
}
main()
{
class-name.obj-name;
}
Header File
class
Data Variables or fields
Function or Method
Object of class
Class members
Prof. Ansari Aadil S
Define a Class Type
class class_name
{
permission_label:
member;
permission_label:
member;
...
};
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Body
Header
Prof. Ansari Aadil S
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Classes & Objects
Rectangle
r1;
Rectangle
r2;
Rectangle
r3;
……
int a;
Objects: Instance of a class
Prof. Ansari Aadil S
• Information hiding
• To prevent the internal representation from direct access
from outside the class
• Access Specifiers
• public
• may be accessible from anywhere within a program
• private
• may be accessed only by the member functions, and friends of
this class
• protected
• acts as public for derived classes
• behaves as private for the rest of the program
Class Definition - Access Control
Prof. Ansari Aadil S
Read and print details of a student using class
program in C++
/*C++ program to create class for a student.*/
#include <iostream>
using namespace std;
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get
student's details
void getDetails(void);
//member function to print
student's details
void putDetails(void);
};
//member function definition, outside of the class
void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
perc=(float)total/500*100;
}
//member function definition, outside of the class
void student::putDetails(void){
cout << "Student details:n";
cout << "Name:"<< name << ",Roll
Number:" << rollNo << ",Total:" << total <<
",Percentage:" << perc;
}
int main()
{
student std; //object
creation
std.getDetails();
std.putDetails();
return 0;
}
Output: Enter name: mike
Enter roll number: 112
Enter total marks outof 500: 456
Student details:
Name:mike,Roll
Number:112,Total:456,Percentage:91.2
Prof. Ansari Aadil S
// Program to illustrate the working of objects and
class in C++ Programming
#include <iostream.h>
#include<conio.h>
class Test
{
private:
int data1;
float data2;
public:
void insertIntegerData(int d)
{
data1 = d;
cout << "Number: " << data1;
}
float insertFloatData()
{
cout << "nEnter data: ";
cin >> data2;
return data2;
}
};
int main()
{
Test o1, o2;
float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
cout << "You entered " << secondDataOfObject2;
getch();
}
Output
Number: 12
Enter data: 23.3
You entered 23.3
Prof. Ansari Aadil S
Data Structure
Data : It is piece of Information
ex . String or any record
Types of Data:
1. Atomic– Single , Non decomposable
ex. 1234
2. Composite or structured – can be broken into sub fields
ex PRN has college code, branch code, student id.
Prof. Ansari Aadil S
A data structure is a particular way of organizing data in a
computer so that it can be used effectively.
Data structure is linear or non Linear in nature
1.Linear- every item is attach to previous and next item
ex. Array , Linked list, Stack ,Queue
2. Non linear – every item is attached to many other items.
Multidimensional array, Tree , Graph.
Prof. Ansari Aadil S
Stack
Queue
Linked List Graph Tree
Prof. Ansari Aadil S
Defining member function
1. Internally defined function- when member function of a
class are defined inside the class itself
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a;
public:
void read()
{
cout<<“Enter radius:”;
cin>>r;
}
void compute()
{
a= 3.14 *r * r;
}
void display()
{
cout<<“Area”=<<a; }
};
void main()
{
clrscr();
circle c;
c.read();
c.compute();
c.display();
getch();
}
Output: Enter radius: 5
Area= 78.5
Prof. Ansari Aadil S
2. Externally defined function
When the member function of a class are defined outside the class.
:: () scope resolution operator is used.
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a;
public:
void read();
void compute();
void display();
};
void circle :: read()
{
cout<<“Enter radius:”;
cin>>r;
}
void circle :: compute()
{
a= 3.14 *r * r;
}
void circle :: display()
{
cout<<“Area”=<<a; }
};
void main()
{
clrscr();
circle c;
c.read();
c.compute();
c.display();
getch();
}
Output: Enter radius: 5
Area= 78.5
Prof. Ansari Aadil S
3. Inline Member function
If a function is defined outside the class but still to be treated as internally
defined function then such function has to be made “inline” function
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a;
public:
void read();
void compute();
void display();
};
inline void circle :: read()
{
cout<<“Enter radius:”;
cin>>r;
}
inline void circle :: compute()
{
a= 3.14 *r * r;
}
inline void circle :: display()
{
cout<<“Area”=<<a; }
};
void main()
{
clrscr();
circle c;
c.read();
c.compute();
c.display();
getch();
}
Output: Enter radius: 5
Area= 78.5
Prof. Ansari Aadil S
Constructor
A constructor is a member function of a class which initializes
objects of a class.
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is created.
• If we do not specify a constructor, C++ compiler generates a
default constructor for us (expects no parameters and has an
empty body).
Prof. Ansari Aadil S
Types of Constructors
1. Default Constructors
2. Parameterized Constructors:
3. Copy Constructor
Prof. Ansari Aadil S
Default Constructors:
Default constructor is the constructor which doesn’t take any
argument. It has no parameters.
#include <iostream.h>
#include<conio.h>
using namespace std;
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
void main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: "<< c.a << endl << "b: "<< c.b;
getch();
}
Output:
a: 10
b: 20
Prof. Ansari Aadil S
Parameterized Constructors
To create a parameterized constructor, simply add parameters to it the way you would to any other
function. When you define the constructor’s body, use the parameters to initialize the object.
#include<iostream.h>
#include<conio.h>
class Point
{
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y
= " << p1.getY();
return 0;
}
Output:
p1.x = 10, p1.y = 15
Prof. Ansari Aadil S
Copy Constructor
A copy constructor is a member function which initializes
an object using another object of the same class.
ClassName (const ClassName &old_obj);
Prof. Ansari Aadil S
Destructors
Destructor is a member function which destructs or deletes an object.
Destructors have same name as the class preceded by a tilde (~)
class test
{
int *p;
public:
test()
{
p=new int;
}
void read()
{
cout<< “ Enter a number: ”;
cin>> *p;
}
void display()
{
cout<< “ Value= “<<*p<< endl;
}
~ test()
{
delete p;
cout<<“Destroyed”;
}
};
void main()
{
clrscr();
test t;
t.read();
t.display();
getch();
}
Output: Enter a number: 5
Value=5;
Destroyed
Prof. Ansari Aadil S

More Related Content

PPTX
Inheritance in java
PDF
C++ OOPS Concept
PPT
Need of OOPs and Programming,pop vs oop
PPT
Object Oriented Programming Concepts using Java
PDF
Python programming : Files
PPTX
Java Data Types
PPTX
Introduction to java
PPTX
Inheritance in c++
Inheritance in java
C++ OOPS Concept
Need of OOPs and Programming,pop vs oop
Object Oriented Programming Concepts using Java
Python programming : Files
Java Data Types
Introduction to java
Inheritance in c++

What's hot (20)

PPT
Chapt 02 ia-32 processer architecture
PPT
Applet life cycle
PPTX
JAVA ENVIRONMENT
PPTX
Sdi & mdi
PPTX
Introduction to vb.net
DOCX
Vb.net class notes
PPT
Introduction to visual basic programming
PPTX
Assembly language
PPT
PPTX
Boxing & unboxing
PPTX
Introduction to Object Oriented Programming
PPTX
Data types in php
PPTX
Method overloading and constructor overloading in java
DOCX
college website project report
PPTX
sutherland- Hodgeman Polygon clipping
PPTX
Raster scan system & random scan system
PPT
Java Programming for Designers
PPTX
Multiple inheritance in c++
PDF
Android ui layout
PPTX
Stream classes in C++
Chapt 02 ia-32 processer architecture
Applet life cycle
JAVA ENVIRONMENT
Sdi & mdi
Introduction to vb.net
Vb.net class notes
Introduction to visual basic programming
Assembly language
Boxing & unboxing
Introduction to Object Oriented Programming
Data types in php
Method overloading and constructor overloading in java
college website project report
sutherland- Hodgeman Polygon clipping
Raster scan system & random scan system
Java Programming for Designers
Multiple inheritance in c++
Android ui layout
Stream classes in C++
Ad

Similar to Object oriented programming 2 (20)

PPTX
Oop concept in c++ by MUhammed Thanveer Melayi
PPTX
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
PPTX
Introduction to Fundamental of Class.pptx
PPTX
05 Object Oriented Concept Presentation.pptx
PPT
Classes, objects and methods
PPTX
OOC MODULE1.pptx
PPTX
C++ppt. Classs and object, class and object
PPTX
PDF
Object Oriented Programming using C++ - Part 2
PPT
Oops lecture 1
PPTX
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
PPTX
OOPs & C++ UNIT 3
PPT
Bca 2nd sem u-2 classes & objects
PPT
Mca 2nd sem u-2 classes & objects
PPT
classes & objects.ppt
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPTX
Lecture 2 (1)
PPT
Object Oriented Programming Concept.Hello
Oop concept in c++ by MUhammed Thanveer Melayi
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Introduction to Fundamental of Class.pptx
05 Object Oriented Concept Presentation.pptx
Classes, objects and methods
OOC MODULE1.pptx
C++ppt. Classs and object, class and object
Object Oriented Programming using C++ - Part 2
Oops lecture 1
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
OOPs & C++ UNIT 3
Bca 2nd sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
classes & objects.ppt
Chapter 2 OOP using C++ (Introduction).pptx
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Lecture 2 (1)
Object Oriented Programming Concept.Hello
Ad

Recently uploaded (20)

PPTX
Sustainable Sites - Green Building Construction
PPTX
web development for engineering and engineering
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
PPT on Performance Review to get promotions
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
DOCX
573137875-Attendance-Management-System-original
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
Construction Project Organization Group 2.pptx
Sustainable Sites - Green Building Construction
web development for engineering and engineering
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Lecture Notes Electrical Wiring System Components
PPT on Performance Review to get promotions
R24 SURVEYING LAB MANUAL for civil enggi
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
573137875-Attendance-Management-System-original
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Foundation to blockchain - A guide to Blockchain Tech
UNIT 4 Total Quality Management .pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Geodesy 1.pptx...............................................
Construction Project Organization Group 2.pptx

Object oriented programming 2

  • 1. OBJECT ORIENTED PROGRAMMING FPL II Unit II SPPU-2017/18 Prof. Ansari Aadil Prof. Ansari Aadil S
  • 2. Object  OBJECT is an identifiable entity with some characteristics and behavior.  In OOP programming object represents a entity that can store data and has its interface through functions.  CLASS is a template/blue-print representing a group of objects that share common properties and relationships. Prof. Ansari Aadil S
  • 3. Structure of an Object Oriented Program Prof. Ansari Aadil S
  • 4. Features of OOP • More emphasis on data rather than procedure. • Programs are divided into object. • Function and data are tied together in a data structure. • Objects communicate with each other through Functions. • Data is hidden or cannot be access by external function. Prof. Ansari Aadil S
  • 5. Principle of OOPS • Class: It is type or a category of things. It is similar to a structure with the difference that it can also have function. Ex. Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. Prof. Ansari Aadil S
  • 6. OBJECT : It is an instance of class. Ex. Class fruit can have apple as its object. ABSTRACTION refers to the act of representing essential features without including the background details or explanations. ENCAPSULATION :The wrapping up of data and operations / functions (that operate on the data ) into a single unit (called class) is known as ENCAPSULATION. INHERITANCE: Inheritance is the capability of one class of things to inherit capabilities or properties from other class POLYMORPHISM: Multiple form of the same thing. It is the ability for a message or data to be processed in more than one form. Prof. Ansari Aadil S
  • 7. Structure of Object Oriented Program in c++ #include<iostream.h> class class-name { access-specifiers… int i,j; float f; char ch; double b; access-specifiers… void function-name() { statement 1; statement 2; } } main() { class-name.obj-name; } Header File class Data Variables or fields Function or Method Object of class Class members Prof. Ansari Aadil S
  • 8. Define a Class Type class class_name { permission_label: member; permission_label: member; ... }; class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Body Header Prof. Ansari Aadil S
  • 9. class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Classes & Objects Rectangle r1; Rectangle r2; Rectangle r3; …… int a; Objects: Instance of a class Prof. Ansari Aadil S
  • 10. • Information hiding • To prevent the internal representation from direct access from outside the class • Access Specifiers • public • may be accessible from anywhere within a program • private • may be accessed only by the member functions, and friends of this class • protected • acts as public for derived classes • behaves as private for the rest of the program Class Definition - Access Control Prof. Ansari Aadil S
  • 11. Read and print details of a student using class program in C++ /*C++ program to create class for a student.*/ #include <iostream> using namespace std; class student { private: char name[30]; int rollNo; int total; float perc; public: //member function to get student's details void getDetails(void); //member function to print student's details void putDetails(void); }; //member function definition, outside of the class void student::getDetails(void){ cout << "Enter name: " ; cin >> name; cout << "Enter roll number: "; cin >> rollNo; cout << "Enter total marks outof 500: "; cin >> total; perc=(float)total/500*100; } //member function definition, outside of the class void student::putDetails(void){ cout << "Student details:n"; cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc; } int main() { student std; //object creation std.getDetails(); std.putDetails(); return 0; } Output: Enter name: mike Enter roll number: 112 Enter total marks outof 500: 456 Student details: Name:mike,Roll Number:112,Total:456,Percentage:91.2 Prof. Ansari Aadil S
  • 12. // Program to illustrate the working of objects and class in C++ Programming #include <iostream.h> #include<conio.h> class Test { private: int data1; float data2; public: void insertIntegerData(int d) { data1 = d; cout << "Number: " << data1; } float insertFloatData() { cout << "nEnter data: "; cin >> data2; return data2; } }; int main() { Test o1, o2; float secondDataOfObject2; o1.insertIntegerData(12); secondDataOfObject2 = o2.insertFloatData(); cout << "You entered " << secondDataOfObject2; getch(); } Output Number: 12 Enter data: 23.3 You entered 23.3 Prof. Ansari Aadil S
  • 13. Data Structure Data : It is piece of Information ex . String or any record Types of Data: 1. Atomic– Single , Non decomposable ex. 1234 2. Composite or structured – can be broken into sub fields ex PRN has college code, branch code, student id. Prof. Ansari Aadil S
  • 14. A data structure is a particular way of organizing data in a computer so that it can be used effectively. Data structure is linear or non Linear in nature 1.Linear- every item is attach to previous and next item ex. Array , Linked list, Stack ,Queue 2. Non linear – every item is attached to many other items. Multidimensional array, Tree , Graph. Prof. Ansari Aadil S
  • 15. Stack Queue Linked List Graph Tree Prof. Ansari Aadil S
  • 16. Defining member function 1. Internally defined function- when member function of a class are defined inside the class itself #include<iostream.h> #include<conio.h> class circle { float r,a; public: void read() { cout<<“Enter radius:”; cin>>r; } void compute() { a= 3.14 *r * r; } void display() { cout<<“Area”=<<a; } }; void main() { clrscr(); circle c; c.read(); c.compute(); c.display(); getch(); } Output: Enter radius: 5 Area= 78.5 Prof. Ansari Aadil S
  • 17. 2. Externally defined function When the member function of a class are defined outside the class. :: () scope resolution operator is used. #include<iostream.h> #include<conio.h> class circle { float r,a; public: void read(); void compute(); void display(); }; void circle :: read() { cout<<“Enter radius:”; cin>>r; } void circle :: compute() { a= 3.14 *r * r; } void circle :: display() { cout<<“Area”=<<a; } }; void main() { clrscr(); circle c; c.read(); c.compute(); c.display(); getch(); } Output: Enter radius: 5 Area= 78.5 Prof. Ansari Aadil S
  • 18. 3. Inline Member function If a function is defined outside the class but still to be treated as internally defined function then such function has to be made “inline” function #include<iostream.h> #include<conio.h> class circle { float r,a; public: void read(); void compute(); void display(); }; inline void circle :: read() { cout<<“Enter radius:”; cin>>r; } inline void circle :: compute() { a= 3.14 *r * r; } inline void circle :: display() { cout<<“Area”=<<a; } }; void main() { clrscr(); circle c; c.read(); c.compute(); c.display(); getch(); } Output: Enter radius: 5 Area= 78.5 Prof. Ansari Aadil S
  • 19. Constructor A constructor is a member function of a class which initializes objects of a class. • Constructor has same name as the class itself • Constructors don’t have return type • A constructor is automatically called when an object is created. • If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body). Prof. Ansari Aadil S
  • 20. Types of Constructors 1. Default Constructors 2. Parameterized Constructors: 3. Copy Constructor Prof. Ansari Aadil S
  • 21. Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters. #include <iostream.h> #include<conio.h> using namespace std; class construct { public: int a, b; // Default Constructor construct() { a = 10; b = 20; } }; void main() { // Default constructor called automatically // when the object is created construct c; cout << "a: "<< c.a << endl << "b: "<< c.b; getch(); } Output: a: 10 b: 20 Prof. Ansari Aadil S
  • 22. Parameterized Constructors To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object. #include<iostream.h> #include<conio.h> class Point { private: int x, y; public: // Parameterized Constructor Point(int x1, int y1) { x = x1; y = y1; } int getX() { return x; } int getY() { return y; } }; int main() { // Constructor called Point p1(10, 15); // Access values assigned by constructor cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); return 0; } Output: p1.x = 10, p1.y = 15 Prof. Ansari Aadil S
  • 23. Copy Constructor A copy constructor is a member function which initializes an object using another object of the same class. ClassName (const ClassName &old_obj); Prof. Ansari Aadil S
  • 24. Destructors Destructor is a member function which destructs or deletes an object. Destructors have same name as the class preceded by a tilde (~) class test { int *p; public: test() { p=new int; } void read() { cout<< “ Enter a number: ”; cin>> *p; } void display() { cout<< “ Value= “<<*p<< endl; } ~ test() { delete p; cout<<“Destroyed”; } }; void main() { clrscr(); test t; t.read(); t.display(); getch(); } Output: Enter a number: 5 Value=5; Destroyed Prof. Ansari Aadil S