Question

In: Computer Science

Define a problem with user input, user output, -> operator and destructors. C ++ please

Define a problem with user input, user output, -> operator and destructors. C ++ please

Solutions

Expert Solution

#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


Related Solutions

C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
Define a java problem with user input, user output, Do While Statement and some mathematical computation....
Define a java problem with user input, user output, Do While Statement and some mathematical computation. Write the pseudocode, code and display output.
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?
Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?
java.. please dont change the format and give me an output sample! user need to input...
java.. please dont change the format and give me an output sample! user need to input k. public class Josephus {    /**    * All persons sit in a circle. When we go around the circle, initially starting    * from the first person, then the second person, then the third...    * we count 1,2,3,.., k-1. The next person, that is the k-th person is out.    * Then we restart the counting from the next person, go...
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
5. Take user input and give corresponding output. User will enter a sentence. The program will...
5. Take user input and give corresponding output. User will enter a sentence. The program will output the word that appears most frequently in this sentence. If there are multiple words with same frequency, output the first of these words. Please enter a sentence: I like batman because batman saved the city many times. The most frequent word is: batman The frequency is: 2 PYTHON
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT