Write a C++ program to create three objects for a class named pntr_obj with data members such as roll_no & name. Create a member function set_data() for setting the data values and print() member function to print which object has invoked it using ‘this’ pointer.





Implementation of the above problem:
#include<iostream.h>
class prn_obj // class declaration
{
//private data members
int rno;
char *name;
public:
void set_data(char *n, int r) //set data member values
{
name=n;
rno=r;
}
void print() // prints data member using 'this' pointer
{
cout<<this->name<<" has invoked print() function"<<endl;
cout<<"The roll number is "<<this->rno<<endl;
}
};
int main()
{
prn_obj ob1,ob2,ob3; // object declaration
ob1.set_data("Suba",1);
ob2.set_data("kayal",2);
ob3.set_data("Jeysree",3);
// calling print function using objects
ob1.print();
ob2.print();
ob3.print();
return 0; //denotes successfull termination

}





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: