Question

Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.  

please put comment with code! and please do not just copy other solutions.

Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to dShapes Problem 1 20 Points Implement a program that manages shapes. Implement a class named Shape with a virtual function are

Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single statement per line 6. Always insert a space before and after each operator, e.g. ab ci not a-b+c; 7. Use meaningful variable names following conventions using camelCase 8. Comment your code to clarify your thoughts if needed but do NOT comment every single line of code! You may not use any library functions unless explicitly specified. You must always include 9. 10. Don't forget to uncomment entirely commented files before submission 11. Name each program probleml.cpp, problem2.cpp, etc. and upload each file to Canvas. 12. Never use goto statements or while (I) or while (true). Use a condition that eventually turns false. 13. Validate all input and re-prompt as long as an invalid value is entered! You don't need to validate the type.
Shapes Problem 1 20 Points Implement a program that manages shapes. Implement a class named Shape with a virtual function area0 which returns the double value 0.0.Implement three derived classes named Rectangle, Square, and Circle. Declare necessary properties in each including getter and setter function and a constructor that sets the values of these properties. Override the area) function in each by calculating the area using the defined properties in that class Write a program that repeatedly shows the user a menu to create one of the three main shapes or to print the shapes created so far. If the user selects to create a new shape, the program prompts the user to enter the values for the size of the selected shape. The shape is then stored in an array*. If the user selects to print the current shapes, print the name and the total area of each shape to the console. The array must hold pointers to Shape objects. You may limit the size of the array to 10.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

class Shape //virtual base class
{
public:
virtual double Area() // virtual function Area()
{
   return 0.0;
}

};
class Circle : public Shape //inheritance
{
private:
double radius;
public:
Circle(double r) //constructor
{
radius =r;
}
double getRadius() //get methods
{
return radius;
}
double Area() //redefining Area() function
{
cout << "\nArea of circle :";
return (3.14*getRadius()*getRadius());
}
};
class Rectangle : public Shape //inheriting abstract base class Shape
{
private:
double length,width;
public:
Rectangle(double l,double w) //constructor to set values
{
length =l;
width = w;
}
double getLength() //get methods
{
return length;
}
double getWidth()
{
return width;
}

double Area() //redefining Area() function
{
cout << "\nArea of rectangle : ";
return (getLength()*getWidth());
}
};
class Square : public Shape //inheriting abstract base class
{
private:
double side;
public:
Square(double s) //constructor to set radius
{
side = s;
}
double getSide() //get method
{
return side;
}
double Area() //redefining virtual method
{
cout<<"\nArea of square :";
return (getSide()*getSide());
}
};

int main()
{

Shape *shape[10];
int option;
int i= 0;
double length,width,radius,side;

do
{
   cout<<"\n1. Create Rectangle ";
   cout<<"\n2. Create Circle ";
   cout<<"\n3. Create Square ";
   cout<<"\n4. Print the Shapes created so far : ";
   cout<<"\n5. Exit ";
  
   cout<<"\nEnter option <1,2,3,4 or 5> : ";
   cin>>option;
  
   switch(option)
   {
       case 1: cout<<"\nEnter the length and width of rectangle : ";
               cin>>length>>width;
               shape[i] = new Rectangle(length,width);
               i++;
              
               break;
      
      
       case 2:cout<<"\nEnter the radius of circle : ";
               cin>>radius;
               shape[i] = new Circle(radius);
               i++;
              
               break;
      
      
       case 3:cout<<"\nEnter the side of square : ";
               cin>>side;
               shape[i] = new Square(side);
               i++;
          
               break;
      
       case 4: for(int j=0;j<i;j++)
       cout<<shape[j]->Area();
       break;
      
       case 5: break;
      
      
       default : cout<<"\nInvalid option";
                   break;
   }
  
}while(option != 5);

return 0;
}

Output:

1. Create Rectangle 
2. Create Circle 
3. Create Square 
4. Print the Shapes created so far : 
5. Exit 
Enter option <1,2,3,4 or 5> : 1
Enter the length and width of rectangle : 6.5 4.9
1. Create Rectangle 
2. Create Circle 
3. Create Square 
4. Print the Shapes created so far : 
5. Exit 
Enter option <1,2,3,4 or 5> : 2
Enter the radius of circle : 4.5
1. Create Rectangle 
2. Create Circle 
3. Create Square 
4. Print the Shapes created so far : 
5. Exit 
Enter option <1,2,3,4 or 5> : 3
Enter the side of square : 6.0
1. Create Rectangle 
2. Create Circle 
3. Create Square 
4. Print the Shapes created so far : 
5. Exit 
Enter option <1,2,3,4 or 5> : 1
Enter the length and width of rectangle : 3.3 4.4
1. Create Rectangle 
2. Create Circle 
3. Create Square 
4. Print the Shapes created so far : 
5. Exit 
Enter option <1,2,3,4 or 5> : 4
Area of rectangle : 31.85
Area of circle :63.585
Area of square :36
Area of rectangle : 14.52
1. Create Rectangle 
2. Create Circle 
3. Create Square 
4. Print the Shapes created so far : 
5. Exit 
Enter option <1,2,3,4 or 5> : 5

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solu...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • Hi I need help doing this problem *The program must re-prompt as long as invalid input...

    Hi I need help doing this problem *The program must re-prompt as long as invalid input is inserted Implement a program that manages shapes. Implement a class named Shape with a virtual function area()which returns the double value. Implement three derived classes named Diamond, Oval, and Pentagon. Declare necessary properties in each including getter and setter function and a constructor that sets the values of these properties. Override the area() function in each by calculating the area using the defined...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. 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 though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns...

  • Instructions: For this assignment, you will write the first part of a program that allows a...

    Instructions: For this assignment, you will write the first part of a program that allows a customer to plan for retirement. Note: This program has no user input Write a "pure" Python function named 'calc_final_balance' that calculates the final balance in a retirement account after annual savings accrue for and earn interest for a number of years. This is a simulation problem similar to the Credit Card sample program, except it is to be written as a function definition (which...

  • Exercise #1: Write a C program that contains the following steps (make sure all variables are...

    Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4: A palindromic prime number is a number that is both prime number and a palindrome number. For example, 131, 313, and 757 are palindromic prime numbers. Design (pseudocode) and implement (source code) a program (name it PalindromicPrime) to display the first 50 palindromic prime numbers, 10 per line separated by one space. The program defines the following methods: Method isPalindome() to check if a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT