0% found this document useful (0 votes)
33 views7 pages

UNIT IV Pointers

Uploaded by

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

UNIT IV Pointers

Uploaded by

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

UNIT IV

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;

How to declare a pointer?

/* This pointer p can hold the address of an integer


* variable, here p is a pointer and var is just a
* simple integer variable
*/
int *p, var

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 (&).

/* This is how you assign the address of another variable


* to the pointer
*/
p = &var;

1
Features of Pointers

Pointers can have many advantages and can be used for many cases, but here we have their most

important features:

 They save memory space

 Execution time is faster since you’ll be dealing with the memory address directly.

 Efficiently access memory (memory of pointers is dynamically allocated).

 It can be used for data structures. It’s useful even to multi-dimensional representation.

 It can be used to access an array’s content.

 Pointers can be used for file handling.

 In C++, a pointer declared to a base class could access the object of a derived class. However,

a pointer to a derived class cannot access the object of a base class.

// C++ program to illustrate Pointers #include <iostream>


using namespace std;
#include <bits/stdc++.h> int main(){
using namespace std; //Pointer declaration
void geeks() int *p, var=101;
{
int var = 20; //Assignment
int* ptr; p = &var;
ptr = &var;
// assign the address of a variable to a cout<<"Address of var: "<<&var<<endl;
pointer cout<<"Address of var: "<<p<<endl;
cout << "Value at ptr = " << ptr << "\n"; cout<<"Address of p: "<<&p<<endl;
cout<<"Value of var: "<<*p;
cout << "Value at var = " << var << "\n";
return 0;
cout << "Value at *ptr = " << *ptr << "\n";
}
}
// Driver program
int main()
{
2
geeks();
return 0;
}

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

Symbols Used in Pointers

The following table shows the symbols that are used with pointers.

Symbol Name Description


& Address of operator Used to find the address of a variable
∗ Indirection operator Used to access the value at an address

The address-of operator (&)

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

The dereference operator (*)

Getting the address of a variable isn’t very useful by itself.

#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

Area of rectangle is: 200

Pointer to a Object in C++:

#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

Accessing Class Functions by Pointers in C++


#include <iostream>
using namespace std;
class Rectangle
{
private:
int length =10;
int breadth =10;
public:
int getArea()
{
return 2*length*breadth;
}
};

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

You might also like