Write a program in C++ to implement classes with pointers as data members.

Algorithm to solve the above problem:
1. Start
2. Create a class called pointer
3. Define a function called getdata() and display() the variable called r, a and pointer *ptr.
4. Declare a pointer to object in main function
5. Call the function with the help of getdata() and display the result with the help of display()
6. Stop


Implementation of the above problem in C++:
#include<iostream>
using namespace std;
class pointer
{
private:
int r;
float a, *ptr;
public:
void getdata();
void display();
};
void pointer::getdata()
{
cout<<"\n Enter the radius of the Circle: ";
cin>>r;
a=3.14*r*r;
ptr=&a;
}
void pointer::display()
{
cout<<" \nValue of a = "<<a;
cout<<"\n Address of a = "<<&a;
cout<<"\n Address of ptr = "<<ptr;
cout<<"\n Value stored at ptr = "<<*ptr;
}
int main()
{
cout<<" Area of Circle \n";
pointer obj;
obj.getdata();
obj.display();
return 0;
}


The output of the above program is tested on www.jdoodle.com
Output:
Area of Circle 

 Enter the radius of the Circle: 12
 Value of a = 452.16
 Address of a = 0x7fff876c0be4
 Address of ptr = 0x7fff876c0be4
 Value stored at ptr = 452.16


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: