Object Oriented Programming
Object Oriented Programming
Programming
Open Source 34
Agenda
●
What is OOP ?
●
What is an Object ?
●
Structured vs Object Oriented Programming
●
Object Oriented Features
– Classes & Objects
– Abstraction
– Encapsulation
– Inheritance
– Polymorphism
2
References
●
Tutorials Point
http://www.tutorialspoint.com/cplusplus
●
C++ How to Program
http://www.deitel.com/books/cpphtp5/
●
Object-Oriented Programming in C++
Author: Robert Lafore
3
What is OOP ?
4
What is OOP ?
5
What is an Object ?!
6
Structured vs OOP
7
Programming Paradigms
• Structured Programming
8
Structured Programming
9
Object Oriented Programming
• Reduce complexity
• Promotes re-usability
10
Structured vs OOP
11
Structured vs OOP (Cont’d)
12
Object Oriented Features
13
Object Oriented Features
14
Classes & Objects
15
Classes & Objects
• Objects
This is the basic unit of object oriented programming. That is both
data and function that operate on data are bundled as a unit called
as object.
• Classes
When you define a class, you define a blueprint for an object. This
doesn't actually define any data, but it does define what the class
name means, that is, what an object of the class will consist of and
what operations can be performed on such an object.
16
Classes & Objects (Cont’d)
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
17
Classes & Objects (Cont’d)
int main( )
{
Box box1; // Declare Box1 of type Box
Box box2; // Declare Box2 of type Box
// box 1 specification
box1.height = 5.0;
box1.length = 6.0;
box1.breadth = 7.0;
// box 2 specification
box2.height = 10.0;
box2.length = 12.0;
box2.breadth = 13.0;
return 0;
}
18
Classes & Objects (Cont’d)
● Access Modifiers
● Static Members
19
Access Modifiers
20
Access Modifiers (Cont’d)
class Base
{
public:
// public members go here
protected:
// protected members go here
private:
// private members go here
};
21
Access Modifiers - Public
class Line{
public:
double length;
};
int main( ){
Line line;
line.length = 10.0;
return 0;
}
22
Access Modifiers - Private
class Box
{ // Main function for the program
public: int main( )
double length; {
void setWidth( double wid ){ Box box;
width = wid;
} // box.width = 10.0; // Error: because width is private
box.setWidth(10.0); // Use member function to set it.
double getWidth( void ){
return width ; return 0;
} }
private:
double width;
};
23
Access Modifiers - Protected
class Box
{ // Main function for the program
protected:
double width; int main( ){
}; SmallBox box;
24
Constructor and Destructor
25
Constructor and Destructor (Cont’d)
class Line
// Main function for the program
{
int main( )
public:
{
void setLength( double len );
Line line(10.0);
double getLength( void );
return 0;
Line(double len){ // This is the constructor
}
Length = len;
}
private:
double length;
};
26
Static Members
class Box
{
public:
static int objectCount;
// Constructor definition
Box()
{
objectCount++;
}
};
int main(void)
{
Box Box1; // Declare box1
Box Box2; // Declare box2
return 0;
}
28
Abstraction 29
Abstraction
30
Abstraction (Cont’d)
31
Abstraction (Cont’d)
• You can interact with its interfaces like the power button,
channel changer, and volume control without having zero
knowledge of its internals.
33
Encapsulation
34
Encapsulation (Cont’d)
35
Encapsulation (Cont’d)
36
Encapsulation (Cont’d)
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
37
Encapsulation vs Abstraction
38
Inheritance
39
Inheritance
• One of the most important concepts in object-oriented programming
is that of inheritance.
40
Inheritance (Cont’d)
41
Inheritance (Cont’d)
// Derived class
// Base class
class Rectangle: public Shape
class Shape
{
{
public:
public:
int getArea(){
void setWidth(int w){
return (width * height);
width = w;
}
}
};
void setHeight(int h) {
height = h; int main(void)
} {
protected: Rectangle Rect;
int width;
int height; Rect.setWidth(5);
}; Rect.setHeight(7);
int area = Rect.getArea();
return 0;
}
42
Polymorphism
43
Polymorphism
●
Overloading
●
Overriding
44
Overloading
●
C++ allows you to specify more than one definition for a
function name or an operator in the same scope
●
"Overloading" a function means that you have multiple
functions with the same name, but different arguments
and obviously different definition (implementation).
●
You can not overload function declarations that differ only
by return type.
45
Overloading (Cont’d)
int main(void)
class printData {
{ printData pd;
public:
void print(int i); // Call print to print integer
void print(double f); pd.print(5);
void print(char* c);
}; // Call print to print float
pd.print(500.263);
return 0;
}
46
Overriding
47
Overriding (Cont’d)
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0){ width = a; height = b; }
virtual int area(){ return 0; }
};
48
Overriding (Cont’d)
Triangle tri(10,5);
shape = &rec;
shape->area();
Rectangle rec(10,7);
shape = &tri;
shape->area();
return 0;
}
49
Thank
You
50