CH 6
CH 6
class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code=a;
price=b;
}
void show(void)
{
cout<<“Code:”<<code<<endl;
<<“Price:”<<price<<endl;
}
};
• Let us declare an item variable x and a pointer ptr to x as follows:
item x;
item *ptr=&x;
The pointer ptr is initialized with the address of x.
• We can refer to the member functions of item in two ways-
1. By using the dot operator and the object.
2. By using the arrow operator and the object pointer.
e.g.
x.getdata(100,75.50);
x.show();
are equivalent to
ptr->getdata(100,75.50);
ptr->show();
this pointer
• C++ uses a unique keyword called this to represent an object that invokes a member function . this is a pointer that
points to the object for which this function was called.
e.g. the function call A.max() will set the pointer this to the address of the object A. The starting address is the same as the
address of the first variable in the class structure.
• This unique pointer is automatically passed to a member function when it is called . The pointer this acts as an implicit
argument to all the member functions.
e.g.
class ABC
{
int a;
…….
…….
};
The private variable ‘a’ can be used directly inside a member function , like
a=123;
#include <iostream>
using namespace std;
class Box
{
public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2))
{
cout << "Box2 is smaller than Box1" <<endl;
}
else
{
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
Output:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
Pointers to derived classes
• We can use pointers not only to the base objects but also to the objects of derived classes.
• Therefore, a single pointer variable can be made to point to objects belonging to different classes.
E.g.
if B is base class and D is a derived class from B, then a pointer declared as a pointer to B can also be a pointer to D.
• Consider the following declarations:
• However, there is a problem in using cptr to access the public members of the derived class D. Using cptr, we can
access only those members which are inherited from B and not the members that originally belong to D.
• In case a member of D has the same name as one of the members of B, then any reference to that member by cptr will
always access the base class member.
Program
#include<iostream>
using namespace std;
class BC
{
public:
int b;
void show()
{
cout<<"b="<<b<<endl;
}
};
class DC : public BC
{
public:
int d;
void show()
{
cout<<"b="<<b<<endl;
cout<<"d="<<d<<endl;
}
};
int main()
{
BC *bptr; //base pointer
BC base;
bptr = &base; // base address
bptr->b=100; // access BC via base pointer
cout<<"bptr points to base object \n ";
bptr->show();
//derived class
DC derived;
bptr = &derived; //address of derived object
bptr->b=200; // access DC via base pointer
Program:
#include<iostream>
using namespace std;
class base
{
public:
void display()
{
cout<<"\n Display base ";
}
virtual void show()
{
cout<<"\n show base";
}
};
class derived : public base
{
public:
void display()
{
cout<<"\n Display derived ";
}
void show()
{
cout<<"\n Show derived";
}
};
int main()
{ Output:
base b; bptr points to base
derived d;
base *bptr; Display base
cout<<"\n bptr points to base\n"; show base
bptr=&b;
bptr->display(); // calls base version
bptr points to derived
bptr->show(); // calls base version
cout<<"\n\n bptr points to derived \n ";
bptr=&d; Display base
bptr->display(); // calls base version Show derived
bptr->show(); // calls derived version
return 0; }
Imp. Note
when bptr is made to point to the object D, the statement
bptr->display();
calls only the function associated with the base (i.e. base :: display() ), whereas, the statement
bptr->show();
calls the derived version of show(). This is because the function display() has not been made virtual in the base class.
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};