Creating Shapes: Exploring Area & Descriptions

In summary: However, the way you are doing it, the code would work if you declared an array of 6 pointers to Shape objects as a global variable instead of a local one.
  • #1
ghost305
14
0
Code:
#ifndef SHAPE_H
#define SHAPE_H

#include <string.h>
using std::string;

using namespace std;



class Shape

{
public:
	Shape(const string &color); //constructor
	~Shape();
	string getColor()const; //returns objects color value
	virtual double area()const = 0;// a const pure virtual member function that computes and returns the object's area
	virtual string toString() const = 0; //a const pure virtual member function that returns the shape's description
private:
	string s_color;

};


#endif //SHAPE_H

#include <string>
using std::string;

#include <sstream>

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "square.h"

//ctor
square::square(const string &color, double length)
:Shape(color)
{
	length = len;
}//end ctor

//dtor
double square::getLength()
{
	return len;
}
//Function to calculate are
double square::area()const
{
	return len * len;
}//end function to calculate area

//Function to returns square's description
string square::toString()const
{
  ostringstream os;
  os << getColor() <<"square with side length of " << len << " " << "and area of " << area();
  return os.str();

}//end of function to return's square description

#include <string>
using std::string;

#include <sstream>

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "rectangle.h"

//ctor
rectangle::rectangle(const string &color, double width, double length)
:Shape(color)
{
length = len;
wid = width;
}//end ctor

//dtor
double rectangle::getWidth()
{
	return wid;
}

double rectangle::getLength()
{
	return len;
}
//function to calculate rectangle area
double rectangle::area() const
{
	return len * wid;
}//end function to get rectangle area

//returns rectangle's description
string rectangle::toString()const
{
  ostringstream os;
  os << getColor() <<"rectangle with length of " << len << " and width of " << wid << " and area of " << area();
  return os.str();
}//end function to return rectangle's description


#include <string>
using std::string;

#include <sstream>

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "circle.h"

//ctor
circle::circle(const string& color, double radius)
:Shape(color)
{
	radius = rad;
}//end ctor

double circle::getRadius()
{
	return rad;
}
//function to calculate circle area
double circle::area()const
{
	return rad * rad * 3.14;
}//end function to get circle area

//returns circle description
string circle::toString()const
{
  ostringstream os;
  os << getColor() <<" circle with radius of " << rad << " and area of " << area();
  return os.str();
}//end function to return circle description


#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "circle.h"
#include "rectangle.h"
#include "square.h"


Shape* getShape()
{	


	int i;
	string shapetype;
	char choice;

	Shape* myShape[6];
	
	
	

	cout << "Creating a Shape  ";
	cout << "============================================== "<<endl;
	cout << " 1: Create a circle "<<endl;
	cout << " 2: Create a rectangle "<<endl;
	cout << " 3: Create a square "<<endl;
	cout << " 4: Done "<<endl;
	cout << "============================================== "<<endl;
	for ( i = 0; i < 6; ++i){
	cout << "Enter number for shapetype" <<endl;

	switch(choice){
	
	case '1':
	cout << "\nEnter the shape's color (or 'done')...";
	cin >>  s_color   ;
	cout << "\nEnter shape type..." ;
	cin >> shapetype;
	cout << "\nEnter radius... ";
	cin >> radius;
	myShape[i] = new circle(color, radius);
	break;
	
	case '2':
	cout << "\nEnter the shape's color (or 'done')...";
	cin >> color;
	cout << "\nEnter shape type...";
	cin >> shapetype;
	cout << "\nEnter the length and width...  ";
	cin >> width >> length;
	myShape[i] = new rectangle(color, length, width); 
	break;
	
	case '3':
	cout << "\nEnter the shape's color (or 'done')...";
	cin >> color;
	cout <<"\nEnter shape type...";
	cin >> shapetype;
	cout << "\nEnter the length of a side...";
	cin >> length;
	myShape[i] = new square(color, length);
	break;
	
	case '4':
	cout <<"\nEnter the shape's color (or 'done')...";
	cout << "done"<< endl;}

	}
	return myShape[i];

}

I try to run the function in int main(); with the function Shape* getShape(); but nothings comes up in the terminal.
 
Technology news on Phys.org
  • #2
C:
Shape* getShape()
{    
    int i;
    string shapetype;
    char choice;

    Shape* myShape[6];    

    cout << "Creating a Shape  ";
    cout << "============================================== "<<endl;
    cout << " 1: Create a circle "<<endl;
    cout << " 2: Create a rectangle "<<endl;
    cout << " 3: Create a square "<<endl;
    cout << " 4: Done "<<endl;
    cout << "============================================== "<<endl;
    for ( i = 0; i < 6; ++i){
    cout << "Enter number for shapetype" <<endl;

    switch(choice){    
        <snip -- deleted long switch statement>  
    }
    return myShape[i];
}
myShape is an array of 6 pointers to Shape objects. It's a local variable, meaning it has automatic duration. This is crucial for what you're trying to do. Right after your final return statement, myShape essentially dies off, This means that whatever address you've stored in the array, of a circle, or a rectangle, or whatever, the whole array is reclaimed, so the return statement just sends back garbage.

A better way to do things would be to pass a Shape object as a parameter to your getShape() function, and have this function construct the appropriate type of shape.
 

1. How do you calculate the area of a shape?

The area of a shape is calculated by multiplying the length of the shape by its width. For example, the area of a rectangle with a length of 4 units and a width of 3 units would be 4 x 3 = 12 square units.

2. What is the difference between area and perimeter?

Area is the measure of the surface inside a shape, while perimeter is the distance around the outside of a shape. In other words, area is the amount of space a shape takes up, while perimeter is the length of the shape's boundary.

3. How do you find the area of a complex shape?

To find the area of a complex shape, you can break it down into smaller, simpler shapes and calculate the area of each one. Then, you can add up the areas of all the smaller shapes to get the total area of the complex shape.

4. Can the area of a shape be a negative number?

No, the area of a shape cannot be a negative number. The area is always a positive value, representing the amount of space inside the shape. If a shape has negative dimensions, it is not considered a valid shape.

5. How is the concept of area used in real life?

The concept of area is used in many real-life scenarios, such as calculating the amount of paint needed to cover a wall, determining the size of a room or piece of land, and measuring the amount of fabric needed to make a piece of clothing. It is also used in fields such as architecture, engineering, and construction to design and build structures with specific area requirements.

Similar threads

  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
Replies
10
Views
958
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
23
Views
1K
Back
Top