UNIT IV Pointers
UNIT IV Pointers
Pointers & Binding Polymorphisms and Virtual Functions: Pointer, Features of Pointers, Pointer
Declaration, Pointer to Class, Pointer Object, The this Pointer, Pointer to Derived Classes and
Base Class, Binding Polymorphisms and Virtual Functions, Introduction, Binding in C++, Virtual
Functions, Rules for Virtual Function, Virtual Destructor.
Pointer:
Pointer is a variable in C++ that holds the address of another variable. They have data type just
like variables, for example an integer type pointer can hold the address of an integer variable and
an character type pointer can hold the address of char variable.
Syntax of pointer:
data_type *pointer_name;
Assignment:
An integer type pointer can hold the address of another int variable. Here we have an integer
variable var and pointer p holds the address of var. To assign the address of variable to pointer
we use ampersand symbol (&).
1
Features of Pointers
Pointers can have many advantages and can be used for many cases, but here we have their most
important features:
Execution time is faster since you’ll be dealing with the memory address directly.
It can be used for data structures. It’s useful even to multi-dimensional representation.
In C++, a pointer declared to a base class could access the object of a derived class. However,
Output Output:
Value at ptr = 0x7ffe454c08cc Address of var: 0x7fff5dfffc0c
Address of var: 0x7fff5dfffc0c
Value at var = 20 Address of p: 0x7fff5dfffc10
Value of var: 101
Value at *ptr = 20
The following table shows the symbols that are used with pointers.
Although the memory addresses used by variables aren’t exposed to us by default, we do have access to
this information. The address-of operator (&) returns the memory address of its operand. This is pretty
straightforward:
#include <iostream>
int main()
{
int x{ 5 };
std::cout << x << '\n'; // print the value of variable x
std::cout << &x << '\n'; // print the memory address of variable x
return 0;
}
Output:
0027FEA0
#include <iostream>
3
int main()
{
int x{ 5 };
std::cout << x << '\n'; // print the value of variable x
std::cout << &x << '\n'; // print the memory address of variable x
std::cout << *(&x) << '\n'; // print the value at the memory address of variable x (parentheses
not required, but make it easier to read)
return 0;
}
Output:
0027FEA0
Pointer to Class:
A pointer to a C++ class is done exactly the same way as a pointer to a structure
and to access members of a pointer to a class you use the member access
operator -> operator, just as you do with pointers to structures. Also as with all
pointers, you must initialize the pointer before using it.
A class pointer is a pointer variable that stores address of an object of a class. As shown in
the above diagram we have a class Rectangle with 2 data members and 1 member
function. We have also created an object of that class named var1.
4
Now we create a pointer variable *ptr of type Rectangle and assign the address of the
object var1 to this pointer variable. As shown in the diagram the address of the
object var1 is stored in the pointer variable ptr.
Pointer to a Class:
class Simple #include <iostream>
{ using namespace std;
public: class Rectangle
int a=10; {
}; private:
int length =10;
int main() int breadth =10;
{ public:
Simple obj; int getArea()
Simple* ptr; // Pointer of class type {
ptr = &obj; return 2*length*breadth;
}
cout << obj.a; };
cout << ptr -> a; // Accessing member
with pointer int main()
} {
// creating an object of Rectangle
Output: Rectangle var1;
10 Rectangle* ptr = &var1;
10 int area = ptr->getArea();
cout<<"Area of rectangle is: "<<area;
return 0;
}
Output
#include <iostream>
using namespace std;
class Data
{
public:
int a;
void print()
{
cout << "a is "<< a;
}
};
int main()
{
Data d, *dp; // Pointer to Object
dp = &d;
5
dp->a=20;
dp->print();
}
Output: 20
Accessing Class Variables/Members by Pointers in C++
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class Data
class Simple {
{ public:
public: int a;
int a; void print()
}; {
cout << "a is "<< a;
int main() }
{ };
Simple obj;
Simple* ptr; // Pointer of class type int main()
ptr = &obj; {
Data d, *dp;
cout << obj.a; dp = &d;
cout << ptr->a; // Accessing with
pointer dp->a=20;
} dp->print();
}
Output: Output:
20
10
10
int main()
{
// creating an object of Rectangle
Rectangle var1;
Rectangle* ptr = &var1;
int area = ptr->getArea();
6
cout<<"Area of rectangle is: "<<area;
return 0;
}
Output:Area of rectangle is: 200