CS Electrical And Electronics
@cselectricalandelectronics

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?
CS Electrical And Electronics Staff asked 4 years ago

I need code.

1 Answers
CS Electrical And Electronics Staff answered 4 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