In: Computer Science
Define a problem with user input, user output, -> operator and destructors. C ++ please
#include <iostream>
using namespace std;
class Counter
{
//static data member
int a;
int b;
public:
//default constructor
Counter()
{
a=0;
b=0;
}
Counter(int aa, int bb)
{
a = aa;
b = bb;
}
//destructor
~Counter()
{
a=0;
b=0;
}
//method to display the values
void display()
{
cout<<endl<<"a = "<<a<<endl<<"b =
"<<b;
}
};
int main()
{
//variable declaration
int a, b;
//create object pointer
Counter *ptr;
//get user input
cout<<"Enter two values for first object:
";
cin>>a>>b;
//create object
Counter t1(a,b);
//get user input
cout<<"Enter two values for first object:
";
cin>>a>>b;
//create object
Counter t2(a, b);
ptr = &t2;
//display object values
cout<<"The first object is:"<<endl;
t1.display();
//display object values
cout<<endl<<endl<<"The second object
is:"<<endl;
ptr->display();
//call destructor
t1.~Counter();
ptr->~Counter();
return 0;
}
OUTPUT:
Enter two values for first object: 10 20
Enter two values for first object: 50 60
The first object is:
a = 10
b = 20
The second object is:
a = 50
b = 60