13 Thispointer DynamicBinding
13 Thispointer DynamicBinding
this pointer
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
#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();
--xxx--
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming
class alpha
{
};
Class beta:public alpha
{
};
alpha A,*aptr; //pointer to base
aptr=&A;
aptr->show()
bptr=&B;
bptr->show();
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming
In C++,
aptr=&B;
#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
}
Note: to understand problem
//aptr->show(); Error
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
Solution:
class alpha
{
int x;
public:
alpha()
{
x=10;
}
virtual void show()
{
cout<<"\n\nAlpha show() called and x = "<<x<<endl;
}
};
--xxx--
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Class Material : C++ Programming
2. virtual functions:
#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
--xxx--
Pure virtual:
#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;
mptr=&b;
mptr->show();
mptr->update(1350.00,900);
mptr->show();
mptr=&a;
mptr->show();
class vehicle:
Protected:
Char model[25];
Char brand[25];
Char egnineno[25];
Public:
void twwheeler(){};
virtual lmv(){};
virtual hmv(){};
};
vehicle V; //error
Can’t create object as vehicle is abstract base class.
--End--