Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Pointers with Arithmetic Operations

C++ Object Oriented Programming

Pointer is an important concept in C++. The pointer can be defined as a data variable which is of used to store address of another variable.

chapter:4

Pointers and Runtime Polymorphism

 

Pointers with Arithmetic Operations

• Pointer is an important concept in C++. The pointer can be defined as a data variable which is of used to store address of another variable. The syntax declaring pointer is

           data_type *name_of_pointer_variable;

For example:

         int *x;

•  Here x is a pointer variable. Due to asterisk(*) we can easily understand that x is a pointer variable. This variable stores the address of integer variable because its data type is declared as integer. Similarly if

      float *y;

means y is a pointer variable which stores the address of float type of variable.

We can initialize any pointer variable by storing the address of another variable.

 For example:

int *x; h

int y;

x=&y;

We are storing the address of variable y in the pointer variable x. Note that since we have declared x as of integer type, the data type of y must be integer because the pointer variable x can hold the address of integer type of data.

The ampersand sign (&) can be read as 'address of'.

Let us now see a simple C++ program which makes use of pointers.

C++ Program

#include<iostream>

#include<cstdlib>

using namespace std;

void main()

{

      int x;

      clrscr();

      x=5;

      cout<<"\n The value of x is = "<<<x;

      cout<<"\n The address of x is = "<<&x;

      getch();

}

Output

The value of x is = 5

The address of x is = 0x8f8cfff4

In the first cout statement we are printing the value of x and the second cout prints the address of x. For getting the address of variable x we have given the &x in the cout.

 

Accessing Data through Pointer

Let us now discuss how to access value stored at pointer variable.

C++ Program

/************************************************************

This is a Program which illustrates how the variable can be accessed by pointer. 

************************************************************/

#include<iostream>

using namespace std;

void main()

{

int I;

float a, *b;

clrscr();

cout<<"\n Enter the radius of the circle

cin>>r;

a=3.14*r*r;

b=&a;

cout<<"\n value of a = "<<a;

cout<<"\n address of a = "<<&a;

cout<<"\n address of b = "<<b;

cout<<"\n value stored at b = "<<*b;

getch();

}

Output


Enter the radius of the circle 5

value of a = 78.5

address of a = 0x8f41fff0

address of b = 0x8f41fff0

value stored at b = 78.5

Explanation

In the above program, void is a data type given to main. The void data type indicates we are returning nothing from main function. The variable b is of pointer type variable at which address of a can be stored. Note that when a is of type float the variable b must be of float type.

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Pointers with Arithmetic Operations


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation