How Can You Explore Area and Descriptions of Shapes in Programming?

  • Thread starter Thread starter ghost305
  • Start date Start date
  • Tags Tags
    Area Shapes
Click For Summary
SUMMARY

The discussion focuses on implementing a shape hierarchy in C++ using abstract classes and polymorphism. The base class, Shape, defines pure virtual functions for calculating area and generating descriptions, while derived classes circle, rectangle, and square implement these functions. A critical issue raised is the improper handling of dynamic memory in the getShape() function, where local variables lead to memory reclamation, resulting in garbage values. The recommendation is to pass a Shape object as a parameter to ensure proper memory management.

PREREQUISITES
  • C++ programming language fundamentals
  • Understanding of object-oriented programming concepts
  • Familiarity with dynamic memory management in C++
  • Knowledge of polymorphism and abstract classes
NEXT STEPS
  • Explore C++ dynamic memory management techniques using new and delete
  • Learn about C++ smart pointers for better memory management
  • Investigate C++ inheritance and polymorphism in depth
  • Study design patterns related to object creation, such as the Factory Pattern
USEFUL FOR

Software developers, particularly those working with C++ and interested in object-oriented design, as well as educators teaching programming concepts related to shapes and geometry in code.

ghost305
Messages
14
Reaction score
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
C:
Shape* getShape()
{    
    int i;
    string shapetype;
    char choice;

    Shape* myShape[6];    

    count << "Creating a Shape  ";
    count << "============================================== "<<endl;
    count << " 1: Create a circle "<<endl;
    count << " 2: Create a rectangle "<<endl;
    count << " 3: Create a square "<<endl;
    count << " 4: Done "<<endl;
    count << "============================================== "<<endl;
    for ( i = 0; i < 6; ++i){
    count << "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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K