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

CH 6

Uploaded by

sparthsalunke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

CH 6

Uploaded by

sparthsalunke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter-6

Pointers, Virtual Functions


 Pointers
• Pointer is a derived type that refers to another data variable by storing the variable’s memory address rather than data.
• A pointer variable defines where to get the value of a specific data variable instead of defining actual data.
• Like C, a pointer variable can also refer to (or point to ) another pointer in C++.
• Pointers provide an alternative approach to access other data objects.

 Declaring and initializing Pointers


• We can declare a pointer variable similar to other variables in C++. Like C, the declaration is based on the data type of
the variable it points to.
• The declaration of a pointer variable takes the following forms-
data-types *pointer-variables
Where,
pointer-variables is the name of the pointer. And the data-type refers to one of the valid C++ data types.
The data-type is followed by an asterisk(*) symbol, which distinguishes a pointer variable from other variables to
the complier.
• Like other programming languages, a variable must be initialized before using it in a C++ program.
• We can initialize a pointer variable as follows-
int *ptr, a; // declarations
ptr=&a; // initialization
• The pointer variable, ptr, contains the address of the variable a. Like C. we use the ‘address of’ operator or reference
operator i.e. ‘&’ to retrieve the address of a variable.
• The second statement assigns the address of the variable a to the pointer ptr.
 Pointers to Objects
• A pointer can point to an object created by a class.
e.g.
item x;
Where item is a class and x is an object defined to be of type item. Similarly, we can define a pointer it_ptr of type item
as follows:
item *it_ptr;
Object pointers are useful in creating objects at run time. We can also use an object pointer to access the public members
of an object. Consider a class item defined as follows:

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;

We can also use the following statement to do the same job-


this->a=123;
Program:

#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:

B *cptr; // pointer to class B type variable


B b; // base object
D d; // derived object
cptr=&b; // cptr points to object b

We can make cptr to point to the object d as follows:


cptr=&d; // cptr points to object d

• 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

// bptr->d=300; //won't work Output:


cout<<"bptr now points to derived object \n"; bptr points to base object
bptr->show(); //bptr now points to derived object b=100
bptr now points to derived object
// accessing d using a pointer of type derived class DC
DC *dptr; //derived type pointer b=200
dptr= &derived; dptr is derived type pointer
dptr->d=300; b=200
cout<<"dptr is derived type pointer \n "; d=300
dptr->show();
return 0;
}
 Virtual Functions
• When we use the same function name in both the base and derived classes, the function in base class is declared as
virtual using the keyword virtual preceding its normal declaration.
• When a function is made virtual, C++ determines which function to use at run time based on the type of object pointed
to by the base pointer, rather than the type of the pointer.
• Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual function.

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.

 Rules for virtual Functions


1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even through it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived class versions must be identical. If two
functions with the same name have different prototypes, C++ considers them as overloaded functions, and the virtual
function mechanism is ignored.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. While a base pointer can point to any type of the derived object, the reverse is not true. That is to say, we cannot use a
pointer to a derived class to access an object of the base type.
9. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases,
calls will invoke the base function.
 Pure Virtual Functions
• It is normal practice to declare a function virtual inside the base class and redefine it in the derived classes.
• The function inside the base class is never used for performing any task. It only servers as a placeholder.
e.g.
We have not defined any object of class media and therefore the function display() in the base class has been defined
‘empty’ . Such function are called “do-nothing” functions.
• A “do-nothing” function may be defined as follows-

virtual void display()=0;


• Such functions are called pure virtual functions.
• A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such
cases, the complier requires each derived class to either define the function or redeclare it as a pure virtual function.
Program:
#include<iostream>
using namespace std;

class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};

// This class inherits from Base and implements fun()


class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
}; Output:
fun() called
int main(void)
{
Derived d;
d.fun();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy