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.
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
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT