0% found this document useful (0 votes)
5 views25 pages

Lecture 19

Uploaded by

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

Lecture 19

Uploaded by

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

CSC241: Object Oriented Programming

Lecture No 19

1
Previous Lecture
• Default copy constructor
• Memory management
– String class using new operator
– Pointer to object
– Array of pointers to objects
• Polymorphism
– Two examples
• Animal : frog, fish, bird
• Shape: circle, triangle, rectangle
2
Today’s Lecture
• Polymorphism
– Example
– Virtual function
• Static and dynamic binding
• Abstract class and pure virtual functions

3
Polymorphism – Example 2
• Suppose you have a objects of different
classes
• All are inherited from a base class
• you want to put them all in an array
• perform a particular operation on them using the
same function call.

Shape

Circle Square Triangle


4
Cont.
0 1 2 3
shape* ptrArray[4]; *ptrArray
ptrArray[0] = new Circle;
ptrArray[1] = new Triangle;
circle square
ptrArray[0] = new Circle;
ptrArray[0] = new Square; triangle circle
for(int j=0; j<4; j++)
ptrarr[j]->draw();

5
Condition for polymorphism
• Following condition must be meet in order to
achieve polymorphic behaviour
– First, all the different classes of shapes, such as
circle and triangles, must be inherited from a
single base class
– Second, the draw() function must be declared to
be virtual in the base class.
Shape
draw()

ptrarr[j]->draw();
Circle Square Triangle
draw() draw() draw()
6
Example programs
• Normal Member Functions Accessed with
Pointers
• Virtual Member Functions Accessed with
Pointers

7
Normal Member Functions Accessed
class Base { main() { *ptr
public: Derv1 dv1;
void show() Derv2 dv2;
{ cout << “Base\n”; } Base* ptr;
}; ptr = &dv1; dv1 dv2
class Derv1 : public Base { ptr->show();
public: ptr = &dv2; Program Output
void show() ptr->show();
{ cout << “Derv1\n”; } } Base
}; Go to program Base
class Derv2 : public Base {
public: Note: Compiler ignores the
void show() contents of the pointer ptr and
{ cout << “Derv2\n”; } chooses the member function that
}; matches the type of the pointer 8
Non-virtual pointer access.

9
Virtual Member Functions Accessed
class Base { main() { *ptr
public: Derv1 dv1;
virtual void show() Derv2 dv2;
{ cout << “Base\n”; } Base* ptr;
}; ptr = &dv1; dv1 dv2
class Derv1 : public Base { ptr->show();
public: ptr = &dv2; Program Output
void show() ptr->show();
{ cout << “Derv1\n”; } } Derv1
}; Go to program Derv2
class Derv2 : public Base {
public: Note: Compiler selects the function
void show() based on the contents of the
{ cout << “Derv2\n”; } pointer ptr, not on the type of the
}; pointer 10
Virtual pointer access

11
Static binding
• When a member function/virtual function is
called with the object of that class
• For example
– dist1.getdist();
– derv1.show();
– base1.show();
• Function invocation is resolved at compile time.
• This is called static or early binding
• This is not a polymorphic behavior
12
Dynamic binding
• Virtual function can be invoked using base
class pointer to a derive class object
– shapePtr—>draw()
• Now the correct derived class draw function
will be selected dynamically (execution time)
• This is called dynamic or late binding
• At run time, when it knows what class is
pointed by shapePtr, the draw function of that
class is called
13
Allowable assignments b/w base and derived class object and pointer

• Four ways to aim base and derive class pointer


at base and derived class objects
– Base class pointer to base class objects
– Derived class pointer to derived class objects
– Base class pointer to derive class objects
• Derive class object is also object of base class
– Derive class pointer to base class object.
• Not allowed
• “is a” relationship applies only from derive class to base
class not vice versa

14
Abstract Classes and Pure Virtual Functions
class Base {
Pure virtual
public: Go to program
virtual void show() = 0; function
}; 0 1
class Derv1 : public Base { main() { arr[2]
public: Base* arr[2];
void show() Derv1 dv1;
{ cout << “Derv1\n”; } Derv2 dv2;
}; arr[0] = &dv1;
dv1 dv2
class Derv2 : public Base { arr[1] = &dv2;
public: arr[0]->show(); Program Output
void show() arr[1]->show(); Derv1
{ cout << “Derv2\n”; } // Base b; Derv2
}; }
Note: objects of abstract class cannot be created 15
Note
• After aiming a base-class pointer at a derived-
class object, attempting to reference derived-
class-only members with the base-class
pointer is a compilation error
class B { class Derv1 : public B { main(){
public: public: B *ptr;
virtual void show() = 0; void show() Derv1 dv1;
}; { cout << “Derv1\n”; } ptr = &dv1;
void display() ptr—>show();
{cout<<“hello derv1”; } ptr—>display();
}; }

16
Example program: Person class
class person { class professor : public person {
protected: private:
char name[40]; int numPubs;
public: public:
void getName() { void getData() {
cout << “ Enter name: “; person::getName();
cin >> name; cout << “ Enter publications: “;
} cin >> numPubs;
void putName() { }
cout << “Name is: “ << name; bool isOutstanding() {
} return (numPubs > 100) ? true :
virtual void getData() = 0; false;
virtual bool isOutstanding() = 0; }
}; };
17
Cont.
class student : public person { person* persPtr[100];
private: int n =0;
float gpa; persPtr[n] = new student;
public: persPtr[n]->getData();
void getData() { n++;
person::getName(); persPtr[n] = new professor;
cout << “ Enter GPA: “; persPtr[n]->getData();
cin >> gpa; n++;
} for(int j=0; j<n; j++){
bool isOutstanding() { persPtr[j]->putName();
return (gpa > 3.5) ? true : false; if( persPtr[j]->isOutstanding() )
} cout << “ This person is
}; outstanding\n”;
}
Go to program 18
Virtual Destructors
• A problem can occur when using
polymorphism to process dynamically
allocated objects of a class hierarchy
• E.g. applying delete operator to a base class
pointer, which point to derived class objects
– B *ptr;
– Derv1 dv1;
– ptr = &dv1;
– delete ptr;
• Solution : virtual destructor
19
Cont.
• A virtual destructor in base class makes all
derived class destructor virtual
– B *ptr; Derv1 dv1;
– ptr = &dv1;
– delete ptr;
• Now destructor for appropriate class is called
• When derived class object is destroyed, base
class part of derive class object is also destroyed
• Base class destructor is executed after derived
class destructor
20
Example – virtual destructor
class Base { Without virtual Destructor
public: *pBase Object of
virtual ~Base()
~Base() Derv
{ cout << “Base Object of
destroyed\n”; } Derv
}; Program Output
class Derv : public Base { Base destroyed
public:
~Derv() With virtual Destructor
{ cout << “Derv Object of
destroyed\n”; } *pBase
Derv
};
main() { Program Output
Base* pBase = new Derv; Derv destroyed
delete pBase; Base destroyed
} Go to program 21
Virtual base classes
• A problem can arise if a member function in
the Grandchild class wants to access data or
functions in the Parent class
When the Child1 and
Child2 classes are
derived from Parent,
each inherits a copy of
Parent; this copy is
called a subobject

22
Cont.
class Parent {
protected: basedata
int basedata;
};
class Child1 : public Parent
{ };
class Child2 : public Parent basedata basedata
{ };
class Grandchild : public Child1,
public Child2 {
public: basedata
int getdata()
{ return basedata; }
}; This situation is ambiguous, and
that’s what the compiler reports
23
Cont.
class Parent { keyword virtual in these
protected: two classes causes them
int basedata; to share a single common
}; subobject of their base
class Child1 : virtual public Parent class Parent
{ };
class Child2 : virtual public Parent
{ };
class Grandchild : public Child1, Go to program
public Child2 {
public:
int getdata()
{ return basedata; }
};
24
25

You might also like