0% found this document useful (0 votes)
17 views15 pages

13 Thispointer DynamicBinding

The document discusses the concept of the this pointer in C++. It provides an example class with member functions that use this to refer to member variables of the current object. It then discusses how this works implicitly in binary operators overloaded as member functions. Finally, it provides an example demonstrating how this works in a binary operator overloaded member function.

Uploaded by

horizonsahib1980
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)
17 views15 pages

13 Thispointer DynamicBinding

The document discusses the concept of the this pointer in C++. It provides an example class with member functions that use this to refer to member variables of the current object. It then discusses how this works implicitly in binary operators overloaded as member functions. Finally, it provides an example demonstrating how this works in a binary operator overloaded member function.

Uploaded by

horizonsahib1980
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/ 15

Sanskriti Computer Education College, Beawar

BCA Part – II | Kshitij Sir


Class Material : C++ Programming

this pointer 

C++ uses a this keyword as a pointer to refer to the current


object under reference (through which member function is
called).

class sample

int a;

public:

void seta(int x)

a=10; //this->a=10

void show()

cout<< a; //cout<<this->a

};

Sample S1,S2;

S1.seta(); //this=&s1

S1.show(); //this=&s1

S2.seta(); //this=&s2

S2.show(); //this=&s2
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

In operator overloading when a unary operator is overloaded


using member function we have not passed any argument and said
it will pass implicitly.

Similar to this overloading a binary operator using member, we


have just passed one argument and said second will be
automatically available. That implicit work was done by using
this pointer.

Using this pointer example:

#include <iostream>
#include<string.h>
using namespace std;
class person
{
int age;
char name[25];
public:
person()
{
age=0;
strcpy(name,"");
}
person(char *s,int a)
{
strcpy(name,s);
age=a;
}
void show()
{
cout<<"\n\tPerson - "<<name<<" Age –
"<<age<<endl;
}
person elder(person &p)
{
if(age>p.age)
{
return *this;
}
else
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

{
return p;
}
}
};

int main()
{
person p1("Ankit",21),p2("Vikas",20),p3("Devesh",19),p;

cout<<"\n\nPerson 1 : ";
p1.show();
cout<<"\n\nPerson 2 : ";
p2.show();
cout<<"\n\nPerson 3 : ";
p3.show();

p=p2.elder(p1); //return p //this=&p2


cout<<"\n\nElder Person between p2>p2 :";
p.show();

p=p2.elder(p3); //return this //this=&p2


cout<<"\n\nElder Person between p2>p3";
p.show();
cout << endl << endl;
return 0;
}

--xxx--
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

Run time polymorphism (dynamic or late binding)

When a function is called and compiler doesn’t know until


runtime that which code have to run against that call and it
is resolved much after compilation means at run time, than it
is called dynamic binding or late binding or run time
polymorphism.

To achieve runtime polymorphism/late binding we must know


following two concepts:
1. pointer to derived classes : (pointer to base class can
point to derived class also)

In inheritance, a base class pointer can also point to all


derived classes, as derived class contain members from base
class, so both are compatible.
Conventionally, we do work as below:

class alpha
{
};
Class beta:public alpha
{
};
alpha A,*aptr; //pointer to base

beta B,*bptr; //pointer to derived

aptr=&A;

aptr->show()

bptr=&B;

bptr->show();
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

In C++,

pointer to base class can point to derived class also

aptr=&B;

Here aptr is of base type pointer while it is pointing to beta


object which is of derived class object. It is perfectly valid
due to type compatibility.
Note:

while vice versa is not valid, means derived type pointer


cannot refer to base type.

bptr=&A; //Not Valid

But it doesn’t access any member of derived class itself,


and can access only those members which are derived from base
class.
Example: of problem

#include <iostream>
#include<string.h>
using namespace std;
class alpha
{
int x;
public:
alpha()
{
x=10;
}
void display()
{
cout<<"\n\nAlpha show() called and x =
"<<x<<endl;
}
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

};
class beta:public alpha
{
int y;
public:
beta()
{
y=20;
}
void show()
{
cout<<"\n\nBeta show() called and y =
"<<y<<endl;
}
};
int main()
{
alpha A,*aptr;

aptr=&A;
aptr->display();

beta B,*bptr;

bptr=&B;
bptr->show();

//Type compatibility
aptr=&B;
//alpha (base)type pointer is point to beta (derived)

//aptr->show(); Error
aptr->display();
//will work perfectly due to type of pointer

cout << endl << endl;


return 0;
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

}
Note: to understand problem
//aptr->show(); Error

Above statement is working perfectly as per the pointers


basic rule. That it emphasizes on type of pointer, rather than
the contents or references it holds.
That’s why aptr is point to beta type object B but giving
error in accessing show () which is of derived class. It can
only access those members which are derived from base type.

Now what we need to resolve the problem is that pointer must


behave based on the type of reference rather than emphasizing
on type.

If this happens will be a perfect example of polymorphism


which will be achieved.

To solve this problem, we can make a function virtual by


prefixing virtual keyword.
This applies only when signature functions or prototype
are similar into both classes.

(java & other modern languages calls it overriding)


Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

Revised the above example:


#include <iostream>
#include<string.h>
using namespace std;
class alpha
{
int x;
public:
alpha()
{
x=10;
}
void show()
{
cout<<"\n\nAlpha show() called and x = "<<x<<endl;
}
};
class beta:public alpha
{
int y;
public:
beta()
{
y=20;
}
void show()
{
cout<<"\n\nBeta show() called and y = "<<y<<endl;
}
};
int main()
{
alpha A,*aptr;

aptr=&A;
aptr->show(); //calls alpha’s show()

beta B,*bptr;

bptr=&B;
bptr->show();
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

//Type compatibility
aptr=&B; //alpha (base)type pointer is point to beta
(derived)
aptr->show();
//also calls alpha’s show() while point to beta object

cout << endl << endl;


return 0;
}

Solution:

Declare show() in alpha as virtual function.

class alpha
{
int x;
public:
alpha()
{
x=10;
}
virtual void show()
{
cout<<"\n\nAlpha show() called and x = "<<x<<endl;
}
};

Solution is elaborated next..

--xxx--
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

2. virtual functions:

When both base class and derived class contains a function


which is same in its prototype or signature, then to resolve
this we makes a function virtual in base class by prefixing
virtual keyword in function definition.

This virtual function can be called using pointer to base


class. Thus ensuring run time polymorphism or dynamic / late
binding.
Example:

#include <iostream>
#include<string.h>
using namespace std;
class alpha
{
public:
virtual void show()
{
cout<<"\n\nAlpha show() called"<<endl;
}
void display()
{
cout<<"\n\nAlpha Display() called"<<endl;
}
};
class beta:public alpha
{
public:
void show()
{
cout<<"\n\nBeta show() called"<<endl;
}
void display()
{
cout<<"\n\nBeta Display() called"<<endl;
}
};
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

int main()
{
alpha A;
alpha *aptr;

aptr=&A;
aptr->show(); //Alpha’s show() will be called
aptr->display();//Alpha’s display() will be called

beta B;

aptr=&B;
//assigning reference of derived class object into base
//type pointer

aptr->show(); //Beta’s show() will be called


aptr->display();//Alpha’s display() will be called

cout << endl << endl;


return 0;
}

--xxx--

Pure virtual:

In inheritance, seldom we have definition of a function in


base class and we usually never creates object of these
classes.
To protect the base class and make it inheritance only class
than define at least one pure virtual function in base class.

Those classes which cannot be used to create object, are


called abstract base classes.
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

Unlike Virtual function, pure virtual function have to not


even define empty body it must not at all have any body means
prototype=0.

virtual void show(){} //virtual function

virtual void show()=0; //pure virtual function

But if you have a pure virtual function in base class, then


all classes which are derived must define the function in
their class otherwise re-declare it.

Program to explain Pure virtual function. Also distinguished


virtual and pure virtual function.

#include <iostream>
#include <cstring>
using namespace std;
class media
{
protected:
char title[25];
float price;
public:
media(char *t,float p)
{
strcpy(title,t);
price=p;
}
virtual void show()=0; //pure virtual function
virtual void update(){}; //virtual function

};
class book:public media
{
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

int npages;
public:
book(char *t,float p,int np):media(t,p)
{
npages=np;
}
void show()
{
cout<<"\n\nBook Details : ";
cout<<"\n\t\tTitle : "<<title;
cout<<"\n\t\tNo. of Pages : "<<npages;
cout<<"\n\t\tPrice : "<<price;
}
void update(int n,float p)
{
npages=n;
price=p;
}
};
class album:public media
{
int playtime;
public:
album(char *t,float p,int pt):media(t,p)
{
playtime=pt;
}
void show()
{
cout<<"\n\nAlbum Details : ";
cout<<"\n\t\tTitle : "<<title;
cout<<"\n\t\tPlaytime : "<<playtime;
cout<<"\n\t\tPrice : "<<price;
}
//update function has not work related to album
//so we have not created it
};
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

int main()
{
media *mptr;

book b("C++ Programming",790.00,1080);


album a("Back to Black",140.25,70);

mptr=&b;
mptr->show();
mptr->update(1350.00,900);
mptr->show();

mptr=&a;
mptr->show();

cout << endl << endl;


return 0;
}
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming

One more theory example for illustration of program design


concept used by using abstract class.

class vehicle:

Protected:

Char model[25];

Char brand[25];

Char egnineno[25];

Public:

virtual int confirmage()=0;

virtual void specification()=0;

void twwheeler(){};

virtual lmv(){};

virtual hmv(){};

};

vehicle V; //error
Can’t create object as vehicle is abstract base class.

confirmage(),specification() are pure virtual that’s why every


class which is derived from vehicle must compulsorily
implement them or redefine it with do nothing body.;

while twwheeler(),lmv() and hmv() are virtual functions which


can be defined or not as per the need of the derived class.

--End--

You might also like