15
POLYMORPISM BY IMTIAZ HUSSAIN 1

polymorphism

Embed Size (px)

DESCRIPTION

A topic in c++ polymorphism

Citation preview

Page 1: polymorphism

1

POLYMORPISMBY

IMTIAZ HUSSAIN

Page 2: polymorphism

2What is Meant By Polymorphism?

It is a Greek word.

Poly mean many.

Morphism mean forms.

In programing Polymorphism is the ability for objects of different classes related by inheritance to response differently to the same function call.

Page 3: polymorphism

3What is Polymorphism in OOP?

After the inheritance it is another most important feature of OOP.

In polymorphism, the member functions with same name are define in base class and also in each derived class.

Polymorphism is use to keep the interface of base class to its derived classes.

Polymorphism can be achieved by mean of virtual functions.

In polymorphism one pointer to a base class object may also point any object of its derived class.

Page 4: polymorphism

4How can we access the member of class?

The member of a class can be access through pointer to the class.

General syntax;

P -> member class;

P is pointer to object.

-> is member access operator.

Member class. It is the member of the object;

Page 5: polymorphism

5Simple program to access member of class; #include<iostream.h>

#include<conio.h>

class data

{

private:

char name[12];

int age;

public:

void input(){

cout<<"Entr your name“; cin>>name;

cout<<"Enter your age"; cin>>age;}

void show()

{

cout<<"Your name is "<<name;

cout<<"Your age is "<<age;

}

};

main()

{

data obj,*j;

j=&obj;

j->input();

j->show();

getch();

}

Page 6: polymorphism

6

Polymorphism

Early Binding ORStatic Binding ORStatic Polymorphism

LATE Binding ORDYNAMIC Binding ORdynamic Polymorphism

Page 7: polymorphism

7Early Binding

The Events That Takes Place At Compile Time Is Called Early Binding.

Also Called Static Binding.

Information Required To Call A Function Is Known At Compile Time.

Page 8: polymorphism

8Early Binding Example

#include<iostream.h>

#include<conio.h>

class A

{

public:

void show() {

cout<<"This Is Base Class "<<endl; }

};

class B: public A

{

public:

void show() {

cout<<"This Is First Derived Class "<<endl; }

};

class C: public A{

public:

void show() {

cout<<"This Is Second Derived Class "<<endl; } };

void main() {

A *ptr;

B C1;

C C2;

ptr=&C1;

ptr->show();

ptr=&C2;

ptr->show();

getch(); }

Page 9: polymorphism

9Virtual Function

Special Type Function.

Can Be Define In Both Derived And Base Class.

Its Name Will Be Same In Every Class.

Definition May Be Different.

In Derived Classes It Will Be Executed Through Pointer Of Its Base Class.

It is Declared By Keyword Virtual.

A Redefined Function Is Said To Override The Base Class Function.

Page 10: polymorphism

10Late Binding

The Events That Takes Place In Execution Time Is Called Late Binding.

In Late Binding At The Compile Time Compiler Does Not Know Which Message Should Compiler Will Respond Because The Type Of Pointer Is Unknown At Compile Time.

It Depending Upon The Contents In Pointer During Execution Time Of Program.

Page 11: polymorphism

11Late Binding Example

#include<iostream.h>

#include<conio.h>

class A

{

public:

virtual void show() {

cout<<"This Is Base Class "<<endl; }

};

class B: public A

{

public:

void show() {

cout<<"This Is First Derived Class "<<endl; }

};

class C: public A{

public:

void show() {

cout<<"This Is Second Derived Class "<<endl; } };

void main() {

A *ptr;

B C1;

C C2;

ptr=&C1;

ptr->show();

ptr=&C2;

ptr->show();

getch(); }

Page 12: polymorphism

12Pure Virtual Function

The virtual function that is only declare But Not Define In The In The Base Class.

General Syntax:

virtual function_name()=0;

The class that contains the pure virtual function exists only to act as base or parent classes.

Page 13: polymorphism

13Abstract Base Class

The base class that has one or more pure virtual function is called the abstract base class.

These classes designed as a framework or layout for derived classes

An abstract base class cannot be instantiated

i.e.

An object of its type cannot be defined but a pointer to an abstract base class can be defined.

Page 14: polymorphism

14Concrete Derived Class

The derived class that does not have any pure virtual function is called concrete derived class.

The pure virtual function of the abstract base class is implemented in the concrete derived class.

The derived classes usually have their own versions of the virtual functions

Page 15: polymorphism

15THANKs

Good Luck