Write a Cpp program to display two numbers using this pointer?

All QuestionsCategory: Cpp LanguageWrite a Cpp program to display two numbers using this pointer?
Chetan Shidling Staff asked 5 years ago

I need code.

1 Answers
Chetan Shidling Staff answered 5 years ago

Code:

#include <iostream>
using namespace std;
class test
{
int a, b;
public:
void show()
{
a = 10;
b = 20;
cout<<“Object address = “<<this;
cout<<“\na =”<<this->a<<endl;
cout<<“b =” <<this->b;
}
};
int main()
{
test t;
t.show();
return 0;
}

Output:

Object address = 0x69fee8
a =10
b =20

Code 02:

#include <iostream>
using namespace std;
class test
{
int a, b;
public:
void show(int a, int b)
{
this->a=a; //(*this).a=a;
this->b=b; //(*this).b=b;
}
void disp()
{
cout<<a<<“\n”<<b<<endl;
}
};
int main()
{
test t;
t.show(10,20);
t.disp();
return 0;
}

Output:

10
20