The Object-oriented Programming Paradigm
1 / 32

The Object-oriented Programming Paradigm - PowerPoint PPT Presentation

  • Updated On :

The Object-oriented Programming Paradigm. As implemented in C++. Features. Encapsulation of data and operations on that data. Inheritance. Genericity. Polymorphism. Encapsulation of Data and Operations. More robust architecture. Localizes effects of changes in a program.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'The Object-oriented Programming Paradigm' - Gideon

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Slide1 l.jpg

The Object-oriented Programming Paradigm

As implemented in C++


  • Encapsulation of data and operations on that data

  • Inheritance

  • Genericity

  • Polymorphism

Slide2 l.jpg

Encapsulation of Data and Operations

More robust architecture

  • Localizes effects of changes in a program

  • Easier to maintain – program composed of objects is more stable under change than a program composed of functions

  • “Off the shelf” components can be built and used

Slide3 l.jpg

Encapsulation of Data and Operations

Programmer can define access to the attributes and methods of the class

  • private – accessible only to member functions of the class

  • protected – accessible to member functions of the class and to operations in any descendant class

  • public – accessible to operations from all classes in the program

Slide4 l.jpg

Encapsulation of Data and Operations

The programmer of a class can loosen the restrictions on access to private or protected features by declaring friend functions and friend classes

class Node {

friend class LinkedList;


itemtype key;

Node * next;


Slide5 l.jpg

Friends have greater access to the features of a class than any “children” of the conferring class.

Friends can access private attributes and methods, children can’t.

In the previous example, descendants of LinkedList do not share friendship priviledges in Link.

friend classes and functions

Friends have access to private and protected features of a class

The status of friend can only be given by a class. It cannot be claimed or inherited.

Slide6 l.jpg

Reasons for conferring “friend” status any “children” of the conferring class

Known symbiotic dependencies between classes.

Examples: Link and LinkedList

Link, LinkedList, and ListIterator

Function whose source parameter is an object of the class but whose target is a different object of the same class

Example shown on next slide

Slide7 l.jpg

deposit (amt) is a message whose recipient is the target object.

In transfer(Targ, Src, amt ) both the target object and the source object need to access private data. There is no one recipient of a message.

Reasons for conferring “friend” status (cont.)

class Account {


double balance, credit_limit;


//other operations

void deposit(double amt);

friendvoid transfer (Account & targ, Account & source, double amt};


Slide8 l.jpg

Genericity object.

Container objects such as listsstacks and queues

Have the same behaviors regardless of what kind of data object is being contained.

To support reuse, we need to be able to create container classes that are capable of holding objects of a variety of types.

This capability is provided in C++ by templates.

Slide9 l.jpg

Notation for templates object.


T * buffer;

int first;

int capacity;

User supplies capacity of a stack implemented as an array

Include implementation file



template <class T>

class Stack {


Stack(int cap);

~Stack( );

void push(T item);

void pop( );

T top ( );

boolean empty ( );


#include Stack.cpp

Slide10 l.jpg

Dynamically allocated array object.

Implementation using Templates

template <class T> Stack <T> :: Stack (int cap) {

buffer = new T[cap];

first = 0;

capacity = cap;


Qualifier Stack <T>:: specifies both class and template parameter

buffer is an array of objects of type (class) T where both the type of the contents T and the capacity are specified by the client at the time of compilation.

Slide11 l.jpg

template <class T> object.


class_name <T> ::

Implementation using templates (cont.)

template <class T> void Stack<T>:::push (T item) {

if (first ==capacity) //raise exception -- not shown in abbreviated .h file

error (OVERFLOW);

buffer[first] = item;



template <class T> T Stack<T>:: top( ) {

if (first ==0) //raise UNDERFLOW error

error (UNDERFLOW);

return buffer[first];



Slide12 l.jpg

Limitation of Templates in Providing Genericity object.

If items in a container are ordered, they must be able to respond to relational operators such as < or ==

(Not all classes have objects that are comparable –because the relational operators have not been overloaded in those classes or the objects are inherently incomparable and overloading the relational operators makes little sense.)

Slide13 l.jpg

Polymorphism object.

Polymorphism is provided in C++ by the following:

Overloading of functions and operators

  • Same function may be found in two or more classes

  • Function with the same name appears in one class with different sets of arguments

Overriding methods in a descendant class to augment or refine behavior defined in the parent class

  • May change the implementation, but should never change the fundamental meaning of the method as defined in the parent.

Substitution of one variable (object) type (class) for another at runtime

  • Dynamic bonding can be achieved in certain circumstances

Slide14 l.jpg

Operator Overloading object.

Restrictions on overloading operators

You can overload any operator except: . :: ?: sizeof

You cannot define new operators by “overloading” symbols that are not already operators.

At least one operand of an overloaded operator must be an instance of a class.

You cannot change the precedence of a C++ operator, or the number of its operands.

Slide15 l.jpg

List object.

size = 2



Operator Overloading

Reasons for operator overloading:

Overloading the = operator allows for “deep copy” of objects with dynamically allocated memory

Example – Linked List

Slide16 l.jpg

Operator Overloading object.



size = 2

size = 2





Shallow copy using the default = operator

B = A;

B.size = A.size;

B.first = A.first;

B.last = A.last;



Slide17 l.jpg

Format for declaring an overloaded operator function will be examined in the next couple of examples

Operator Overloading

Deep copy:

List & List::operator = (const List & rhs) {

//if the target is not empty, make it empty

while (first != NULL) {

link * temp = first;

first = first -> next;

delete temp;


link * ptr = rhs.first;

while (ptr) {

insert ( ptr -> key); //assumes insert at back of list

ptr = ptr -> next;


return *this;


Deep copy produces an independent chain of links attached to the target List

Slide18 l.jpg

target of message is lhs examined in the next couple of examples

target is not a member of class Complex

Operator Overloading

Consider the following example – class Complex (abridged)

class Complex {


double: re, im;


Complex ( ); //default constructor

Complex (double x, double y);

//other member functions

Complex & operator += (const Complex & rhs);

friend Complex operator + (const Complex & scr1, const Complex & scr2);

friend ostream & operator << (ostream & out, Complex & rhs);


Slide19 l.jpg

Returns the object that examined in the next couple of examplesthis points to – the target

Construct 2 complex number objects

z2 is the target for the += operation

Operator Overloading (cont.)

Complex & Complex:: operator += (const Complex & rhs) {

re +=;

im +=;

return * this;


This is a member function of class Complex

  • The left-hand side of the operator is the target object that receives the message

Example: Complex z1(2, 3);

Complex z2 (21, -24) ;

z2 += z1;

Slide20 l.jpg

Target may be different from two sources or same as one of them

Operator Overloading (cont.)

Complex operator + (const Complex & src1, const Complex & src2) {

Complex z;

z,re = +;

z,im = +;

return z;


Sources scr1 and scr2 are passed by reference, but the const declarations prevents the programmer from changing their state.

  • This operator is not a member function of class Complex -- just a friend

  • no qualifier is used in its header.

Return type is an object, not a reference -- each of the following assignments are permitted

Complex z1, z2(1, 2), z3(-2, 4);

z1 = z2 + z3;

z2 = z2 + z3;

cout << z2 + z3;

Slide21 l.jpg

Operator Overloading (cont.) them

ostream & operator << (ostream & out, const Complex & rhs) {

out << ‘(‘ << << “, “ << << ‘)’;

return out;


Returns a reference to the ostream object to provide for the sequencing of stream insertion operators in a single statement

Complex z1(1.2, -2.1), z2(24, 44);

cout << z1 << “+ “ << z2 “ = “ << z1 + z2 << endl;

Slide22 l.jpg

Substitution of types at Runtime them

The substitute must be a subclass of the declared type

The original class (type) and the substitute must be referenced by a pointer

Methods overridden in the subclass must be declared virtual in the parent.

Slide23 l.jpg

Dog them


string name, says;

int x_pos, y_pos;

string name, says;

int x_pos, y_pos;

Dog ( ); //default

Dog(constchar aname[]);

//inherited methods

void speak( ); //override

Animal( ); //default

Animal(constchar aname[];

//other non virtual methods

virtualvoid speak( );


void chase_cats(Cat & acat);

Parent class

Descendant class

Dogs bark “ruff, ruff”

Animals are mute

Slide24 l.jpg

Default constructor – no name given them


Consider an array of (pointers to) Animals

Animal * theBarn[4];

Now create some animals and put them in theBarn.

Animal MickeyMouse(“Mickey”);

Cat Sly (“Sylvester”);

Dog Fred;

Dog Spot (“Spot”);

theBarn[0] = &MickeyMouse;

theBarn[1] = &Sly;

theBarn[2] = &Fred;

theBarn[3] = &Spot;

Slide25 l.jpg

Substitutions them

Now tell each animal to speak

for (int i = 0; i < 4; i++)

theBarn[i] -> speak( );

The dogs will bark, the cat meow, and the mouse say that he is mute.

But the dogs cannot chase cats!

Slide26 l.jpg

theBarn them













Only methods and attributes in animal are seen


The Farm

Farm objects have a data member called theBarn that is an array of animal pointers

Slide27 l.jpg

Substitution them

An example that illustrates polymorphism








Copy these files and see what happens when you do the following:

  • Remove virtual from in front of speak( ) in animal

  • Try having a dog chase one of the cats

Slide28 l.jpg

Shape them



Inheritance and Abstract Classes


Shape is abstract

It has no instances

Slide29 l.jpg

methods have no implementation them

Abstract Classes

class Shape {

protected: //data members needed if used for substitution

int X, Y;

double R1, R2;


Shape (int x, y, double u, v);

virtual double area ( ) = 0;

virtual double perimeter ( ) = 0;


If at least one method is undefined, the class is abstract

The abstract class Shape provides the specification for methods in the subclass, but no actual implementation.

Two dimensional shapes have an area and a perimeter, but only specific shapes have formulae for computing them.

Slide30 l.jpg

Implementations of themarea and perimeter are specific to the subclass

Inheriting from Abstract Classes

class Circle : public Shape {


Circle (int x, int y, double r) : X(x), Y(y), R1(r ), R2(0) {}

double area ( ) {return M_PI * R1 *R1; }

double perimeter ( ) {return 2 *M_PI * R1; }

//other methods for Circle


Slide31 l.jpg

Using Abstract Classes them

Abstract classes provide a Framework for adding application specific behavior

User must supply the implementation details for all undefined methods.

Abstract classes are useful as generalizations – where common behavior of separate but similar classes is abstracted out and placed in a common parent

  • Provides for design simplification

  • Provides for run-time polymorphism

Slide32 l.jpg

Target for reference param in retrieve them

Uses shape specific method

Application using Template Class

List <Shape *> L;

Circle * p = new Circle(10, 12, 4.0);

L.insert(p, 1);

Circle * p2 = new Circle (15, 24, 3.6);

L.insert(p2, 2);

Rectangle * p3 = new Rectangle(21,36, 2.5, 4.1);

L.insert(p3, 3);

for (int i = 1; i <=L.length(); i++) {

Shape * q;

L.retrieve(i, q);

q -> area ( );