Question

In: Computer Science

C++ code Write a program to illustrate how to use the temporary class. Your program must...

C++ code

Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object.

Use three header files named main.cpp, temporary.h, and temporaryImp.cpp

An example of the program is shown below:

Enter object name (rectangle, circle, sphere, or cylinder: circle

Enter object's dimensions: 
    rectangle (length and width) 
    circle (radius and 0) 
    sphere (radius and 0) 
    rectangle (base raidus and height) 
10
0


After setting myObject: 
circle: radius = 10.00, area = 

Solutions

Expert Solution

// Temporary.h

#include<iostream>
using namespace std;

class Rectangle                                 // Rectangle Class
{
   float length, width;
public:
  
   void setValues(float len, float wid);
   void printValues();
};

class Circle                                      // Circle Class
{
   float radius,area;
public:
   void setValues(float radius, float area);
   void printValues();
};
class Sphere                                           // Sphere Class
{
   float radius, area;
public:
   void setValues(float radius, float area);
   void printValues();
};
class Cylinder                                  // Cylinder Class
{
   float radius, height;
public:
   void setValues(float radius, float height);
   void printValues();
};


******************************************************************************************************************

// TemporaryImp.cpp

// Implementation of All methods.

#include "Temporary.h"

void Rectangle::setValues(float len, float wid)        // Setter Method
{
   this->length = len;
   this->width = wid;
}
void Rectangle::printValues()                                  // To display data
{
   cout << "Rectangle: length = " << this->length << ", width" << this->width;
}

void Circle::setValues(float radius, float area)
{
   this->radius = radius;
   this->area = area;
}
void Circle::printValues()
{
   cout << "circle: radius = " << this->radius << ", area = " << this->area;
}

void Sphere::setValues(float radius, float area)
{
   this->radius = radius;
   this->area = area;
}
void Sphere::printValues()
{
   cout << "sphere: radius = " << this->radius << ", area = " << this->area;
}

void Cylinder::setValues(float radius, float height)
{
   this->radius = radius;
   this->height = height;
}
void Cylinder::printValues()
{
   cout << "cylinder: radius = " << this->radius << ", height = " << this->height;
}

**********************************************************************************************************************

// main.cpp

#include "Temporary.h"

int main()
{
   char str[10] = { 0 };
   float val1 = 0, val2 = 0;
   cout << "Enter object name (Rectangle, Circle, Sphere or Cylinder):";
   cin >> str;
   cout << "Enter object's dimensions:";
   cin >> val1 >> val2;
  
   if (0 == _strcmpi(str, "Rectangle"))
   {
       Rectangle rec;                                            // Object creation as per user input.
       rec.setValues(val1,val2);                            // Setter method call to set values.
       rec.printValues();

   }
   else if (0 == _strcmpi(str, "Circle"))
   {
       Circle cir;                                                    // Object creation as per user input.
       cir.setValues(val1, val2);                            // Setter method call to set values.
       cir.printValues();

   }
   else if (0 == _strcmpi(str, "Sphere"))
   {
       Sphere sp;                                              // Object creation as per user input.
       sp.setValues(val1, val2);                        // Setter method call to set values.
       sp.printValues();
   }
   else if (0 == _strcmpi(str, "Cylinder"))
   {
       Cylinder cyl;                                          // Object creation as per user input.
       cyl.setValues(val1, val2);                      // Setter method call to set values.
       cyl.printValues();
   }

   return 0;
}

******************************************************************************************************************************

OUTPUT :

Enter object name (Rectangle, Circle, Sphere or Cylinder):circle
Enter object's dimensions:
10
0
circle: radius = 10, area = 0

******************************************************************************************************************************

Explanation :

I have done object creation as per user input and initialize values to member of that class, display those values. i didnt do any calculation operation. if any help need, plz comment.

******************************************************************************************************************************


Related Solutions

CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
CODE must using C++ language. Write the definition of the class dayTye that implements the day...
CODE must using C++ language. Write the definition of the class dayTye that implements the day of the week in a program. The class dayType should store the day of the week as integer. The program should perform the following operations on an object of type dayType 1. set the day 2. display the day as a string - Sunday, ... Saturday 3. return the day as an integer 4. return the next as an integer 5. return the previous...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes. Example Program Session (implement some linefeed ‘\n’ formatting): Enter the limit: 1000 Primes up to 1000    2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53 59   61   67   71   73   79   83   89   97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT