0% found this document useful (0 votes)
39 views11 pages

CS304 Part 2

Uploaded by

Abdul Wahab
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)
39 views11 pages

CS304 Part 2

Uploaded by

Abdul Wahab
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/ 11

Object Oriented Programming (CS304) VU

Example – Extention

Window
width
height
open
close
draw

1- Invoke Window’s
DialogBox draw
2- draw the dialog
controls
box
enable
draw

Example – Restriction

IntegerSet


add( elem ) Add element to
… the set

If elem < 1 then


NaturalSet give error
else

Add element
add( elem ) to the set

© Virtual University of Pakistan 35


Object Oriented Programming (CS304) VU

Example – Improve Performance


Class Circle overrides rotate operation of class Shape with a Null operation.

Shape

color
coord

draw
rotate
setColor

Circle
radius
draw
rotate

04.6. Abstract Classes

In our examples we made classes for shape and person. These are abstract concepts
and the classes we make against abstract concepts are called abstract classes. They
are present at or near the top in the class hierarchy to present most generalized
behaviour.

An abstract class implements an abstract concept


Main purpose is to be inherited by other classes
Can’t be instantiated
Promotes reuse

Abstract Classes - Example I

Shape
color
vertices

draw
move
setColor

Circle Line Triangle

Here, Shape is an abstract class

36 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

Abstract Class Shape


Concrete Classes Circle Line Triangle ….

Abstract Classes - Example II

Person
name
age
gender

eat
walk

Student Doctor
Teacher

Here, Person is an abstract class

Abstract Class Person


Concrete Classes Student Teacher Doctor Engineer Director ….

Abstract Classes - Example III

Vehicle

color
model

accelerate
applyBrakes

Car Truck

Bus

Here, Vehicle is an abstract class

© Virtual University of Pakistan 37


Object Oriented Programming (CS304) VU

Abstract Class Vehicle


Concrete Classes Car Bus Truck ….

Abstract Classes can not exist standalone in an object model


While making object model we start by finding out objects in our object model and
then we find out objects having common attributes and make them in the form of
general classes at the top of class hierarchies.

04.7. Concrete Classes

The entities that actually we see in our real world are called concrete objects and
classes made against these objects are called concrete classes.

A concrete class implements a concrete concept


These are used to instantiate objects in our programs
Provides implementation details specific to the domain context

Concrete Classes - Example I

Person

Student Doctor
program Teacher
studyYear
study
heldExam

Here Student, Teacher and Doctor are concrete classes

Concrete Classes - Example II

Vehicle

Car Truck
Bus
capacity
load
unload

Here Car, Bus and Truck are concrete classes

38 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

• A concrete class may exist in an object model independently


• Concrete classes mostly lie below the top of class hierarchy in a good object
model.

If there is an abstract class then hierarchy exists in the object model as there will
definitely be some concrete classes as well derived from this abstract class otherwise
there is no use of abstract class.

Glossary:

a. Natural numbers: numbers from 1 to …….onwards


b. Integers: all positive and negative numbers …..-3,-2,-1,0,1,2,3………
c. Whole numbers: numbers from 0 ,1 ,2, 3 ….onwards (natural no’s including
0)
Some times whole numbers are also called numbers without fractional part.

© Virtual University of Pakistan 39


Object Oriented Programming (CS304) VU

Lecture No.05
Multiple Inheritance

Inheritance:
We saw inheritance purposes in last lecture

• Generalization
• Extention or sub typing
• Specialization or restriction

Abstract and concrete classes, former is used to represent abstract concepts later is
used to represent concrete concepts.
Overriding derived classes override inherited classes (base classes) behaviour.
Overriding is used for Specialization, Extention, Restriction, and Performance.

05.1. Multiple Inheritance

Sometimes we want to reuse characteristics of more than one parent class, in that
case we need to inherit a class from more than one classes.

Example 1– Multiple Inheritance

Consider the example of an imaginary specie Mermaid used in fairy tales that lives in
water having features both of a women as well as of a fish, In Object Oriented
programming perspective Mermaid can be derived from two classes Women and
Fish.

Woman Fish

Mermaid

C++ Code:

/*Program to demonstrate simple multiple inheritance*/

class Fish {

};

class Woman {

40 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

};

class Mermaid : public Woman , public Fish {

};

Our Mermaid class inherits features of both woman and fish suppose our woman
class has method wald() and fish cclass has method swim then our mermaid class
can use both methods i.e can walk as well as can swim.

Woman Fish
void walk() void swim()

Mermaid

C++ code:

#include <iostream>
#include <stdlib.h>

using namespace std;

/*Program to demonstrate simple multiple inheritance*/

class Fish
{
public:
void swim(){
cout<<"\n In method swim";
}

};

class Woman
{
public:
void walk(){
cout<<"\n In method walk"<<endl;
}

};

© Virtual University of Pakistan 41


Object Oriented Programming (CS304) VU

class Mermaid : public Woman,public Fish


{

};

int main(int argc, char *argv[])


{

Mermaid mermaid;
/*This Mermaid object will have two implicit objects one of Fish class and one of
Woman class*/
mermaid.swim();
mermaid.walk();

system("PAUSE");
return 0;

Output:

In method4 swim
In method walk

Example 2– Multiple Inheritance

Take another example of amphibious vehicle (vehicle that can run on land as well as
on water) so it has properties of both land as well as of water vehicle. The general
hierarchy in this case will be,

Vehicle

Land Vehicle Water Vehicle

Car Amphibious Vehicle Boat

Here we have added a general Vehicle class as well to add all common functions of
Land Vehicles and Water Vehicles in that class, and specific functions of Land and

4
class member functions are also called class methods

42 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

Water vehicle in their respective classes then we have derived Amphibious Vehicle
class from Land Vehicle and Water Vehicle classes (we can do the same in first
example as well concerning Woman, Fish and Mermaid).

C++ code:

class Vehicle
{

};

class WaterVehicle : public Vehicle


{

};

class LandVehicle : public Vehicle


{

};

class AmphibiousVehicle : public LandVehicle,public WaterVehicle


{

};

Suppose we have a changeGear method in Vehicle class that is applicable to both


water and land vehicle, we also have Float and Move methods in water and land
vehicles respectively then our amphibious vehicle will have all these methods,

C++ code:

#include <iostream>
#include <stdlib.h>

using namespace std;

/*Multiple Inheritance in case of Amphibious Vehicle*/

class Vehicle
{
public:
void changeGear(){ cout<<"\nI am Vehicle changeGear() function..\n";}
};

class WaterVehicle : public Vehicle


{

© Virtual University of Pakistan 43


Object Oriented Programming (CS304) VU

public:
void Float(){ cout<<"\nI am float function of Water Vehicle";}
};

class LandVehicle : public Vehicle


{
public:
void Move(){ cout<<"\nI am move function of Land Vehicle"<<endl;}

};

class AmphibiousVehicle : public LandVehicle,public WaterVehicle


{

};

int main(int argc, char *argv[])


{

AmphibiousVehicle amphibious;

amphibious.Float();
/*Calling Float function of Water Vehicle class*/

amphibious.Move();
/*Calling Move function of Land Vehicle class*/

system("PAUSE");
return 0;

Output:

I am float function of Water Vehicle


I am move function of Land Vehicle

Advantage of Multiple Inheritance:

As was the case with simple (single) inheritance multiple inheritance also decreases
redundant code as we can inherit a class from many classes and can use their
functions without the need to write them again.

44 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

However, there are more disadvantages of multiple inheritance, than its advantages.

Problems with Multiple Inheritance

Increased complexity

Amphibious vehicle hierarchy is a complicated as this class is derived from two


classes that will make code more complex and less understandable however this is
obvious as amphibious vehicle is a complicated vehicle. It is generic problem.

Reduced understanding

Due to increased complexity of class hierarchy the object model becomes difficult it
understand especially for someone who is looking it first time.

Duplicate features

As we are deriving a single class from more than one class so there is a chance of
duplication of features (same methods in both parents), following problems may
arise due to duplicate features,

Problem 1: Ambiguity

Consider the class hierarchy of Mermaid class below,

Woman Fish
eat eat
… …

Mermaid

As mermaid also needs to eat and its both parents have their own methods of eating
so here question arises,

Which eat operation Mermaid should inherit as both functions are available?

Solution – We can solve this problem by explicitly calling eat method from any of
the parent classes in Mermaid class according to behaviour of Mermaid (i.e. if it eats
like a Woman we can call eat method of Woman class and if eats like Fish we can call
method of Fish class), for this we will Override the Common method in multiply
inherited class and in that class overridden method we will call the appropriate base
class function.

© Virtual University of Pakistan 45

You might also like