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.
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
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations...
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations instead of variables to calculate the sum of those 4 numbers Print the sum Use a pointer to a pointer for print operation
Write application in C# that enables a user to: Use Methods for user input and calculations...
Write application in C# that enables a user to: Use Methods for user input and calculations input the grade and number of credit hours for any number of courses. Calculate the GPA on a 4.0 scale using those values. Grade point average (GPA) is calculated by dividing the total amount of grade points earned, sometimes referred to as quality points, by the total number of credit hours attempted. For each hour, an A receives 4 grade or quality points, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT