Professional Documents
Culture Documents
#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; } };
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
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.
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; }
#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
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