2. Polymorphism
• Polymorphism is the technique in which
various forms of a single function can be
defined and shared by various objects to
perform the operation.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
4. Pointer
• A pointer is a memory variable that stores a
memory address. Pointers can have any
name that is legal for other variables and it
is declared in the same fashion like other
variables but it is always denoted by ‘*’
operator.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
5. Pointer declaration
• Syntax:
data-type * pointer-mane;
• For example:
• int *x; //integer pointer, holds
//address of any integer variable.
• float *f; //float pointer, stores
//address of any float variable.
• char *y; //character pointer, stores
//address of any character variable.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
6. Pointer declaration & initialization
int *ptr; //declaration.
int a;
ptr = &a; //initialization.
• ptr contains the address of a.
• We can also declare pointer variable to point
to another pointer,
int a, *ptr1, *ptr2;
ptr1 = &a;
ptr2 = &ptr1;
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
7. • We can manipulate the pointer with
indirection operator i.e. ‘*’ is also known as
deference operator.
• Dereferencing a pointer allows us to get the
content of the memory location.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Manipulation of Pointer
8. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Manipulation of Pointer
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int *ptr;
ptr = &a;
cout <<"nDereferencing :: "<< *ptr <<endl;
*ptr = *ptr + a;
cout << a;
return 0;
}
9. • A pointer can be incremented(++) or
decremented(--).
• Any integer can be added to or subtracted
from a pointer.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer Expressions and Arithmetic:
11. • Pointer objects are useful in creating objects
at run-time.
• We can also use an object pointer to access
the public members of an object.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to objects
12. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to objects
#include <iostream>
using namespace std;
class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code = a;
price = b;
}
void show (void)
{
cout << code <<endl;
cout << price <<endl;
}
};
13. Pointer to objects
• We can also write ,
int main()
{
item x;
x.getdata(100, 75.6);
x.show();
item *ptr = &x;
ptr -> getdata(100, 75.6);
ptr ->show();
return 0;
}
(*ptr).show();//*ptr is an alias of x
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
14. • We can also create objects using pointers
and new operator as follows:
• Statements allocates enough memory for
data members in the objects structure.
• We can create array of objects using
pointers,
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to objects
Item *ptr = new item;
Item *ptr = new item[10];
15. • Pointers can be declared to point base or
derived classes.
• Pointers to object of base class are type-
compatible with pointers to object of
derived class.
• Base class pointer can point to objects of
base and derived class.
• Pointer to base class object can point to
objects of derived classes whereas a pointer
to derived class object cannot point to
objects of base class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to derived classes
16. Fig: Type-compatibility of base and derived class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to derived classes
17. • If B is a base class and D is a derived class from B,
then a pointer declared as a pointer to B can also
be pointer to D.
B *ptr;
B b;
D d;
ptr = &b;
• We can also write,
ptr = &d;
• Here, we can access only those members which are
inherited from B and not the member that
originally belonging to D.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to derived classes
18. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to derived classes
#include <iostream>
using namespace std;
class B
{
public:
int b;
void show()
{
cout << "b = " << b <<endl;
}
};
class D : public B
{
public:
int d;
void show()
{
cout << "d = " << d <<endl;
}
};
19. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to derived classes
int main()
{
B *bptr;
B bobj;
bptr = &bobj;
bptr -> b = 100;
bptr ->show ();
D dobj;
bptr = &dobj;
bptr -> b = 200;
bptr ->show ();
D *dptr;
dptr = &dobj;
dptr -> d = 300;
dptr ->show ();
return 0;
}
Output:
b = 100
b = 200
d = 300
20. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Pointer to derived classes
#include <iostream>
using namespace std;
class Polygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};
class Rectangle: public Polygon
{
public:
int area()
{ return width*height; }
};
class Triangle: public Polygon
{
public:
int area()
{ return width*height/2; }
};
22. • If there are member functions with same
name in base class and derived class, virtual
functions gives programmer capability to
call member function of different class by a
same function call depending upon different
context.
• This feature in C++ programming is known
as polymorphism which is one of the
important feature of OOP.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual functions
23. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual functions
#include <iostream>
using namespace std;
class B
{
public:
void display()
{ cout<<"Content of base class.n"; }
};
class D : public B
{
public:
void display()
{ cout<<"Content of derived class.n"; }
};
24. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual functions
int main()
{
B *b = new B;
D d;
b->display();
b = &d; /* Address of object d in pointer variable */
b->display();
return 0;
}
Output:
Content of base class.
Content of base class.
25. • If you want to execute the member function
of derived class then, you can declare
display() in the base class virtual which
makes that function existing in appearance
only but, you can't call that function.
• In order to make a function virtual, you have
to add keyword virtual in front of a function.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual functions
26. • The virtual functions should not be static
and must be member of a class.
• A virtual function may be declared as friend
for another class. Object pointer can access
virtual functions.
• Constructors cannot be declared as a virtual,
but destructor can be declared as virtual.
• The virtual function must be defined in
public section of the class. It is also possible
to define the virtual function outside the
class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Rules for virtual functions
27. • It is also possible to return a value from
virtual function like other functions.
• The prototype of virtual functions in base
and derived classes should be exactly the
same.
– In case of mismatch, the compiler neglects the
virtual function mechanism and treats them as
overloaded functions.
• Arithmetic operation cannot be used with
base class pointer.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Rules for virtual functions
28. • If base class contains virtual function and if
the same function is not redefined in the
derived classes in that base class function is
invoked.
• The operator keyword used for operator
overloading also supports virtual
mechanism.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Rules for virtual functions
29. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual functions
#include <iostream>
using namespace std;
class B
{
public:
virtual void display() /* Virtual function */
{ cout<<"Content of base class.n"; }
};
class D1 : public B
{
public:
void display()
{ cout<<"Content of first derived class.n"; }
};
class D2 : public B
{
public:
void display()
{ cout<<"Content of second derived class.n"; }
};
30. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual functions
int main()
{
B *b = new B;
D1 d1;
D2 d2;
/* b->display(); // You cannot use this code here
because the function of base class is virtual. */
b = &d1;
b->display(); /* calls display() of class derived D1 */
b = &d2;
b->display(); /* calls display() of class derived D2 */
return 0;
}
Output:
Content of first derived class.
Content of second derived class.
31. • In above program, display( ) function of two
different classes are called with same code
which is one of the example of
polymorphism in C++ programming using
virtual functions.
• Remember, run-time polymorphism is
achieved only when a virtual function is
accessed through a pointer to the base class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual functions
32. • If expression =0 is added to a virtual
function then, that function is becomes pure
virtual function.
• Note that, adding =0 to virtual function does
not assign value, it simply indicates the
virtual function is a pure function.
• If a base class contains at least one virtual
function then, that class is known as
abstract class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Declaration of a Abstract Class
33. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
#include <iostream>
using namespace std;
class Shape /* Abstract class */
{
protected:
float l;
public:
void get_data() /* Note: this function is not virtual. */
{
cin>>l;
}
virtual float area() = 0; /* Pure virtual function */
};
34. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
class Square : public Shape
{
public:
float area()
{ return l*l; }
};
class Circle : public Shape
{
public:
float area()
{ return 3.14*l*l; }
};
35. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
Output :
Enter length to calculate area of a square: 2
Area of square: 4
Enter radius to calcuate area of a circle:3
Area of circle: 28.26
int main()
{
Shape *ptr;
Square s;
Circle c;
ptr = &s;
cout<<"Enter length to calculate area of a square: ";
ptr -> get_data();
cout<<"Area of square: "<<ptr ->area();
ptr = &c;
cout<<"nEnter radius to calcuate area of a circle:";
ptr -> get_data();
cout<<"Area of circle: "<<ptr ->area();
return 0;
}
36. • Consider a book-shop which sells both
books and videos-tapes.
• We can create media that stores the title
and price of a publication.
• We can then create two derived classes, one
for storing the number of pages in a book
and another for storing playing time of a
tape.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Implementation in practice
37. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Implementation in practice
Fig: class hierarchy for book shop
mediamedia
tapetapebookbook
38. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
#include <iostream>
using namespace std;
class media /* Abstract class */
{
protected:
char *title;
double price;
public:
media(char *s, double p)
{
title = new char [strlen(s)+1];
strcpy (title, s);
price = p;
}
virtual void display() = 0; /* Pure virtual function */
};
39. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
class book : public media
{
int pages;
public:
book(char *s, double p, int pg):media(s,p)
{
pages = pg;
}
void display()
{
cout << "Book details :" << endl;
cout << "Title : "<< title << endl;
cout << "Price : "<< price << endl;
cout << "No of pages: "<< pages << endl;
}
};
40. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
class tape : public media
{
int duration;
public:
tape(char *s, double p, int dr):media(s,p)
{
duration = dr;
}
void display()
{
cout << "Tape details :" << endl;
cout << "Title : "<< title << endl;
cout << "Price : "<< price << endl;
cout << "Duration: "<< duration << endl;
}
};
41. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
int main()
{
media *ptr;
book b("My Life....",12.22,200);
tape t("Tech Talks..",56.23,80);
ptr = &b;
ptr -> display ();
ptr = &t;
ptr -> display ();
return 0;
}
Output :
Book details :
Title : My Life....
Price : 12.22
No of pages: 200
Tape details :
Title : Tech Talks..
Price : 56.23
Duration: 80