0% found this document useful (0 votes)
9 views

Object Oriented Programming

The document provides an overview of Object-Oriented Programming (OOP), highlighting its key concepts such as classes, objects, abstraction, encapsulation, inheritance, and polymorphism. It contrasts OOP with structured programming, emphasizing the importance of data and processes in OOP. Additionally, it includes examples and explanations of various OOP features and principles.

Uploaded by

Abdulrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Object Oriented Programming

The document provides an overview of Object-Oriented Programming (OOP), highlighting its key concepts such as classes, objects, abstraction, encapsulation, inheritance, and polymorphism. It contrasts OOP with structured programming, emphasizing the importance of data and processes in OOP. Additionally, it includes examples and explanations of various OOP features and principles.

Uploaded by

Abdulrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Object Oriented

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 ?

• Object-oriented programming is an approach to


design modular, reusable software systems.

• Programming methodology based on objects,


instead of just functions and procedures.

5
What is an Object ?!

• Ward is a division of a hospital or a suite of rooms


shared by patients who need a similar kind of care. In a
hospital, there are a number of wards, each of which may
be empty or have on it one or more patients.

• The doctors in the hospital are organized into teams.


Each team has a unique name or code and is headed by a
consultant doctor or attending physician.

6
Structured vs OOP

7
Programming Paradigms

• Structured Programming

• Object Oriented Programming

8
Structured Programming

• Its main focus is on the algorithms required to


perform the desired computations

• Importance is given to procedure (logic) and not to


the data on which these procedures operate

• Structured programming is often associated with


top-down design approach

9
Object Oriented Programming

• Used the bottom-up design approach

• Data and processes have equal importance

• Reduce complexity

• Promotes re-usability

10
Structured vs OOP

Top-down vs. bottom-up approaches

11
Structured vs OOP (Cont’d)

12
Object Oriented Features

13
Object Oriented Features

• Classes & Objects


• Abstraction
• Encapsulation
• Inheritance
• Polymorphism

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

● Constructor and Destructor

● Static Members

19
Access Modifiers

● A public member is accessible from anywhere outside the class but


within a program.

● A private member variable or function cannot be accessed, or even


viewed from outside the class. Only the class and friend? functions
can access private members.

● A protected member variable or function is very similar to a private


member but it provided one additional benefit that they can be
accessed in child classes which are called derived classes.

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;

class SmallBox:Box // set box width using member function


{ box.setSmallWidth(5.0);
public:
void setSmallWidth( double wid ){ return 0;
width = wid; }
}

double getSmallWidth( void ){


return width ;
}
};

24
Constructor and Destructor

● A class constructor is a special member function of a class


that is executed whenever we create new objects of that
class.

● A constructor will have exact same name as the class and


it does not have any return type at all, not even void.

● Constructors can be very useful for setting initial values


for certain member variables.

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

● When we declare a member of a class as static it means no


matter how many objects of the class are created, there is
only one copy of the static member.

● A static member is shared by all objects of the class

● A constructor will have exact same name as the class and


it does not have any return type at all, not even void.

● Constructors can be very useful for setting initial values


for certain member variables.
27
Static Members (Cont’d)

class Box
{
public:
static int objectCount;
// Constructor definition
Box()
{
objectCount++;
}
};

// Initialize static member of class Box


int Box::objectCount = 0;

int main(void)
{
Box Box1; // Declare box1
Box Box2; // Declare box2

return 0;
}

28
Abstraction 29
Abstraction

Data abstraction refers to, providing only essential information to


the outside world and hiding their background details.

30
Abstraction (Cont’d)

31
Abstraction (Cont’d)

• We can say a television clearly separates its internal


implementation from its external interface.

• You can interact with its interfaces like the power button,
channel changer, and volume control without having zero
knowledge of its internals.

• You do not know how it receives signals over the air or


through a cable, how it translates them, and finally
displays them on the screen.
32
Encapsulation

33
Encapsulation

• Encapsulation is an Object Oriented Programming concept that


binds together the data and functions that manipulate the data, and
that keeps both safe from outside interference and misuse.

• With procedural languages, it is not always clear which functions


work on which variables.

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

• Data encapsulation is a mechanism of bundling the data, and the


functions that use them and data abstraction is a mechanism of
exposing only the interfaces and hiding the implementation details
from the user.

38
Inheritance

39
Inheritance
• One of the most important concepts in object-oriented programming
is that of inheritance.

• Inheritance allows us to define a class in terms of another class,


which makes it easier to reuse the code functionality and fast
implementation time.

• The idea of inheritance implements the is a relationship. For


example, dog IS-A animal.

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

The ability to use an operator or function in different ways.


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);

// Call print to print character


pd.print("Hello C++");

return 0;
}

46
Overriding

Call to a member function will cause a different function to


be executed depending on the type of object that invokes
the function.

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

class Triangle: public Shape{


public:
Triangle( int a=0, int b=0):Shape(a, b){ }
int area (){ return (width * height / 2); }
};

48
Overriding (Cont’d)

// Main function for the program


int main( )
{
Shape *shape;
Rectangle rec(10,7);

Triangle tri(10,5);
shape = &rec;
shape->area();

Rectangle rec(10,7);
shape = &tri;
shape->area();

return 0;
}

49
Thank
You
50

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy