You are on page 1of 20

POINTERS , VIRTUAL FUNCTIONS AND POLYMORPHISM

Pointer to class objects


A pointer variable can point to an object of a class Members of class are accessed by pointer variable with arrow() operator If objects are maintained in an array then pointer variable can be incremented to access the next object in the array (same as array of built in data types)

#include"iostream.h" #include"conio.h"

int main (void) { clrscr(); student *p,s[3]; p=&s[0];

class student { int rollno ; char name[12]; public: void getdata (void) { cout<<endl<<"enter roll no and name"; cin>>rollno>>name; } void display() { cout<<endl<<"roll no and name are"<<rollno<<"\t"<<name; } };

For (int i=0;i<3;i++) { p->getdata (); p->display(); p++; }


return 0; }

Output
enter roll no and name12 abc roll no and name are12 abc enter roll no and name11 drf roll no and name are11 drf

enter roll no and name13 sde roll no and name are13 sde

The this pointer


At the time of calling of a member function using an object , a pointer to the invoking object is passed to the invoked member. Say class C definition is { public: int a,b; void compute(); }; a ,b and compute() are members of class C C obj; When obj.compute() is called a pointer to obj is passed to compute()

Using this pointer members of object are accessed. If compute() is { a=a + b ;} is same as { this->a=this->a + this->b; } this pointer is used explicitly when overloading the operators using member function this pointer can be used at places where pointers of objects are to return, or to show the difference between members of invoking object and passed arguments.

C++ Program to find eldest of three persons


#include"iostream.h" #include"conio.h" #include<string.h> class person { int age;char name[12]; public: person (int a,char *s) { age=a; strcpy(name, s); } void show() {cout<<"name is "<<name<<"and age is"<<age<<endl; } person & elder (person & p) { If (age)>=p.age) return *this; else return p; }

int main(void) { person p1(12,"abc"), p2(20,"def"), p3(23,third"); clrscr(); person p=p1.elder(p3) ; cout<<Elder person is:\n ; p.display(); p=p1.elder(p2) ; cout<<Elder person is:\n ; p.display(); return 0; }

Pointers to derived objects


A pointer to base class can also point to an object of derived class. The opposite of above is not true. Through such pointer, only inherited members from base class can be accessed. By casting such pointer to derived type full access of all members is possible.

#include"iostream.h" #include"conio.h" class BC { public: int b; void show(void) {cout<< b=<<b<<\n;} }; class DC : public BC { public : int d; void show() {cout<< b=<<b<<\n; cout<< d=<<d<<\n; } };

int main() { BC *bptr ; BC base; bptr = &base; bptr -> b=100; cout<<bptr points to base object; bptr->show();

DC derived; bptr = &derived; bptr->b=200; bptr->d=400; // wont work cout<<bptr points to derived object; bptr->show();

DC *dptr; dptr =&derived; dptr->d = 300; cout<<dptr is derived type pointer \n; dptr->show(); cout<< using ((DC*)bptr) \n; ((DC*)bptr) -> d=400 ; ((DC*)bptr) -> show(); return 0; }

#include"iostream.h" #include"conio.h" class student { int rollno; char name[12]; public: void getdata(void) { cout<<endl<<"enter roll no and name"; cin>>rollno>>name; } void display() { cout<<endl<<"roll no and name are"<<rollno<<"\t"<<name; } };

class result :public student { int marks[3]; public: result (int r_no, char n[ ] ): student (r_no , n) { cout<<"enter marks in 3 subjects"; For (int i=0;i<3;i++) cin>>marks[i]; } void total() { cout<<"sum of marks are"<<marks[0]+marks[1]+marks[2]; } };

int main (void) { clrscr(); student *p; result rslt(1000,"ANJALI") ,*ptr; p=&rslt; ptr=&rslt; //p->total(); ERROR: total is not a member of student ((result*)p)->total(); // base class pointer is casted to derived class pointer //(student*)ptr=&s; //NOT VALID p=(student*)ptr; p->display(); return 0; }

OUTPUT
enter marks in 3 subjects12 36 65 sum of marks are113 roll no and name are 1000 ANJALI

VIRTUAL FUNCTIONS

What is a virtual function?


A member function of the class declared with keyword VIRTUAL. The functionality of virtual functions can be over-ridden in its derived classes A virtual function allows derived classes to replace the implementation provided by the base class. Implements polymorphism: If a pointer to base class is made to point an object of derived class then function definition of derived class will be called

A member function declared (and defined) within base class but redefined in derived class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. Non-virtual member functions are resolved statically (compile time) whereas virtual member functions are resolved dynamically (at run-time)

Example
#include<iostream.h> #include<conio.h> class base { public: virtual void display (void ) { cout<<endl<<"this is base class"; }}; class derived : public base { public: void display (void) { cout<<"\n"<<"this is derived class"; }}; void main() { clrscr(); base b,*ptr; derived d; ptr=&b; cout<<endl<<"ptr is pointing to base class object"; ptr->display(); ptr=&d; cout<<endl<<"ptr is pointing to derived class object"; ptr->display(); return 0; }

OUTPUT
ptr is pointing to base class object this is base class ptr is pointing to derived class object this is derived class

Pure Virtual Functions


The function in base class has been defined empty such functions are called do-nothing functions.such functions are called pure virtual functions. A pure virtual function is a function declared in a base class that has no defination relative to base class. A class containing pure virtual functions cannot be used to declare any objects of its own.such classes are called abstract classes.

You might also like