Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
**my project talk about Shape hierarchy for every shape needs Inheritance, Polymorphism and Files I/0
I did a simple thing of the requirements, but I did not do it 100%, such as requirements 1, 2, 3 or requirements 6 and 7, so I sent all the requirements, please help me**
All classes must have a color and name attributes, both of a string type and stored in the base class (Shape).
All 2D shapes must have area and perimeter attributes, both of double type and stored in their base class (Sahpe2D).
All 3D shapes must have surfaceArea and volume attributes, both of double type and stored in their base class (Shape3D),
readData(), print(), printToFile(ofstream &output), readFromFile(ifstream &input) functions must be implemented in all classes (base and derived), taking into consideration they will be used for polymorphism,
computeArea() and computePerimeter() functions must be implementer in all 2D shape classes (including their base class), taking into consideration their use in polymorphism,
computeSurfaceArea() and computeVolume() functions must be implemented in all 3D shape classes (including their base class), taking into consideration their use in polymorphism,
Requirements
For every class, you need to build a default/parametrized constructor,
The default constructors set the objects' data members to 1.0,
Parametrized constructors are used to set objects' data members using the values passed to the objects during instantiation
readData() function is used to read all the object's data members from the user
print() function is used to print all object's data to the screen,
Your program must be able to write all objects' data to the binary file "Shapes.dat", using the printToFile functions,
Your program should be able to read and append to the "Shapes.dat" file as needed, using the readFromFile functions,
Your main file must be able to store/retrieve data to/from output/input files depending on the user's choice,
It is not known how many objects will be added or their order of addition.
//Shapes.h
#pragma once
#include<iostream>
#include<fstream>
using namespace std;
class Shape {
public:
string color, name;
Shape()
{
cout << "Base Class is Shape for this element . \n";
}
};
//Shape2D.h
#pragma once
#include"shapes.h"
class Shape2D : public Shape {
public:
double area, perimeter;
Shape2D()
{
cout << "Parent Class is Shape2D for this element .\n";
}
};
//Shape3D.h
#pragma once
#include"shapes.h"
class Shape3D : public Shape {
public:
Shape3D()
{
cout << "Parent Class is Shape3D for this element .\n";
}
};
//Square.h
#include"shapes2D.h"
class Square : public Shape2D {
public:
Square()
{
cout << "Calculating square area and perimeter \n";
}
void computeArea(double side)
{
double area;
area = side * side;
print("Area", area);
printToFile("Area", area);
}
void computePerimeter(double side) {
double perimeter;
perimeter = 4 * side;
print("Perimeter", perimeter);
printToFile("Perimeter", perimeter);
}
void print(string x, double y)
{
cout << x << " of Square = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Square = " << y << "\n";
}
};
//Triangle.h
#include"shapes2D.h"
class Triangle : public Shape2D {
public:
Triangle()
{
cout << "This is a Triangle\n";
}
void computeArea(double base, double height) {
double area;
area = (height * base) / 2;
print("Area", area);
printToFile("Area", area);
}
void computePerimeter(double base, double height) {}
void print(string x, double y)
{
cout << x << " of Triangle = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Triangle = " << y << "\n";
}
};
//Sphere.h
#include"shapes3D.h"
class Sphere : public Shape3D {
public:
Sphere()
{
cout << "This is a Sphere\n";
}
void computeSurfaceArea(double radius) {
double area;
area = 4 * 3.14 * radius * radius;
print("Area", area);
printToFile("Area", area);
}
void computeVolume(double radius) {
double perimeter;
perimeter = (4 / 3) * (3.14 * radius * radius * radius);
print("Perimeter", perimeter);
printToFile("Perimeter", perimeter);
}
void print(string x, double y)
{
cout << x << " of Sphere = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Sphere = " << y << "\n";
}
};
//Hexagon.h
#include"shapes2D.h"
class Hexagon : public Shape2D {
public:
Hexagon()
{
cout << "This is a Hexagon\n";
}
void computeArea(double side) {
double area;
area = 3 * sqrt(3) * side * side / 2;
print("Area", area);
printToFile("Area", area);
}
void computePerimeter(double side) {
double perimeter;
perimeter = 6 * side;
print("Perimeter", perimeter);
printToFile("Perimeter", perimeter);
}
void print(string x, double y)
{
cout << x << " of Hexagon = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Hexagon = " << y << "\n";
}
};
//Cylinder.h
#include"shapes3D.h"
class Cylinder : public Shape3D {
public:
Cylinder()
{
cout << "This is a Cylinder\n";
}
void computeSurfaceArea(double radius, double height) {
double area;
area = (2 * 3.14 * radius) * (radius + height);
print("Surface Area", area);
printToFile("Surface Area", area);
}
void computeVolume(double radius, double height) {
double volume;
volume = 3.14 * radius * radius * height;
print("Volume", volume);
printToFile("Volume", volume);
}
void print(string x, double y)
{
cout << x << " of Cylinder = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Cylinder = " << y << "\n";
}
};
//Cube.h
#include"shapes3D.h"
class Cube : public Shape3D {
public:
Cube()
{
cout << "This is a Cube\n";
}
void computeSurfaceArea(double side) {
double area;
area = 6 * side * side;
print("Surface Area", area);
printToFile("Surface Area", area);
}
void computeVolume(double side) {
double volume;
volume = side * side * side;
print("Volume", volume);
printToFile("Volume", volume);
}
void print(string x, double y)
{
cout << x << " of Cube = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Cube = " << y << "\n";
}
};
//Cone.h
#include"shapes3D.h"
#include<cmath>
class Cone : public Shape3D {
double radius, height;
public:
Cone()
{
cout << "This is a Cone\n";
}
void computeSurfaceArea(double radius, double height) {
double area;
area = 3.14 * radius * (radius + sqrt(radius * radius + height * height));
print("Surface Area", area);
printToFile("Surface Area", area);
}
void computeVolume(double radius, double height) {
double volume;
volume = (3.14 * radius * radius * height) / 3;
print("Volume", volume);
printToFile("Volume", volume);
}
void print(string x, double y)
{
cout << x << " of Cone = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Cone = " << y << "\n";
}
};
//Circle.h
#include"shapes2D.h"
class Circle : public Shape2D{
double radius;
public:
Circle()
{
cout << "Calculating circle area and perimeter \n";
}
void computeArea(double r)
{
double area;
area = 3.14 * r * r;
print("Area", area);
printToFile("Area", area);
}
void computePerimeter(double r) {
double perimeter;
perimeter = 2 * 3.14 * r;
print("Perimeter", perimeter);
printToFile("Perimeter", perimeter);
}
void print(string x, double y)
{
cout << x << " of Square = " << y << "\n";
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat");
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Circle = " << y << "\n";
}
};
//main.cpp
#include"Cone.h"
#include"Cube.h"
#include"Cylinder.h"
#include"Hexagon.h"
#include"sphere.h"
#include"square.h"
#include"Triangle.h"
#include "Circle.h"
int main()
{
Cylinder a;
Cube b;
Cone c;
double r, side, base, height;
int n;
cout << "Select any Number to calculate : ";
cout << "1. Circle \n";
cout << "2. Square\n";
cout << "3. Triangle\n";
cout << "4. Hexagon\n";
cout << "5. Sphere\n";
cout << "6. Cube\n";
cout << "7. Cylinder\n";
cout << "8. Cone\n";
cin >> n;
if (n == 1) {
Circle obj1;
cout << "Circle Radius";
cin >> r;
obj1.computeArea(r);
obj1.computePerimeter(r);
}
if (n == 2) {
Square obj2;
cout << "Square Side";
cin >> side;
obj2.computeArea(side);
obj2.computePerimeter(side);
}
if (n == 3) {
Triangle obj3;
cout << "Triangle Base";
cin >> base;
cout << "Triangle Height";
cin >> height;
obj3.computeArea(base, height);
// obj3.computePerimeter(base,height) ;
}
if (n == 4) {
Hexagon obj4;
cout << "Hexagon Side";
cin >> side;
obj4.computeArea(side);
obj4.computePerimeter(side);
}
if (n == 5) {
Sphere obj5;
cout << "Sphere Radius";
cin >> r;
obj5.computeSurfaceArea(r);
obj5.computeVolume(r);
}
if (n == 6) {
Cube obj6;
cout << "Cube Side";
cin >> side;
obj6.computeSurfaceArea(side);
obj6.computeVolume(side);
}
if (n == 7) {
Cylinder obj7;
cout << "Cylinder Radius";
cin >> r;
cout << "Cylinder Height";
cin >> height;
obj7.computeSurfaceArea(r, height);
obj7.computeVolume(r, height);
}
if (n == 8) {
Cone obj8;
cout << "Cone Radius";
cin >> r;
cout << "Cone Height";
cin >> height;
obj8.computeSurfaceArea(r, height);
obj8.computeVolume(r, height);
}
return 0;
}
There are quite a few things going on here.
You are trying to include header files all over the place. I dont know why you want/need them. It's fine (if you have those files defined somewhere) but for a simple case of defining some classes one monolithic file is ok.
You haven't satisfied the problem statement. It said "default/parametrized constructor" you have default constructors for each i.e. 'myClass()' but no parameterized one ex 'myClass(int heigh, int width)...' so you need to implement those so you can initialize shapes at construction time instead of setting them later.
The formatting is all over the place, I would recommend cleaning that up.
To your main question: "it appears to me that the classes are not defined What is the problem?"
You have defined classes, this is a class definition:
class Shape
{
public:
Shape()
{
cout << "Base Class is Shape for this element . \n";
}
private:
string color, name;
};
Down in main Shape myshape; is creating an object of type shape.
Here is a easy write up to follow as well:
https://www.geeksforgeeks.org/c-classes-and-objects/
I got an error in Visual Studio 2019, when building and using IntelliSense at the same time, which said that it expected a ';' (CODE: E0065 and the error is supposed to be on line 9), I am extremely confused because this is my first time getting an error like this when defining functions because it is also my first time defining functions in c++, I really don't know where I could be missing a semi colon. The program doesn't run.
#include <iostream>
using namespace std;
int main() {
const double pi{3.14159};
double calc_area_circle(double radius) {
return pi * (radius * radius);
}
void area_circle() {
double radius{};
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
}
return 0;
}
If any one could clear this up for me, I would really appreciate it.
Nesting of function definition (defining functions inside another functions) is not allowed in C++ unless you use lambda functions.
Put the function definitions outside the function
#include <iostream>
using namespace std;
const double pi{3.14159};
double calc_area_circle(double radius) {
return pi * (radius * radius);
}
void area_circle() {
double radius{};
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
}
int main() {
return 0;
}
or convert them to lambda function
#include <iostream>
using namespace std;
int main() {
const double pi{3.14159};
const auto calc_area_circle = [&](double radius) -> double {
return pi * (radius * radius);
};
const auto area_circle = [&]() {
double radius{};
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
};
return 0;
}
Note: these programs will do nothing because the defined functions are not called.
I am playing around with hierarchical objects and pointers and have written a base class Circle, then two classes Cylinder and Sphere that both derive from Circle.
When I run the program I get the error: double free or corruption (out): 0x00007fffffffddf0 ***
So I tried running the code through GDB. I found that the error occurs when I call the Cylinder destructor, which calls the virtual destructor for Circle. However I don't understand why this is happening.
From my research it seems this kind of error occurs when the code tries to deallocate memory that is not available to it.
I thought perhaps the destructor for Cylinder was causing the problem, since it is called first, so I commented out all of the Sphere and Circle object lines in main() and just used the Cylinder code.
When the destructor for the Cylinder pointer was called it resulted in a Segmentation Fault, so I am trying to access memory out of range.
Now I'm thoroughly confused.
Here is my code:
#include <iostream>
#include <cmath> // used for square
static constexpr float PI = 3.14159;
using namespace std;
class Circle{
protected:
float radius;
public:
float getRadius(){
return radius;
}
void setRadius(float r){
radius = r;
}
float calcCircleArea(){
return PI * pow(radius, 2);
}
float calcCircumference(){
return 2 * PI * radius;
}
Circle(){
// default constructor
}
Circle(float rad){
radius = rad; // overloaded constructor
}
virtual ~Circle(){ // virtual destructor
cout << "Destroying Circle Constructor" << endl;
}
};
class Cylinder: public Circle{
private:
float height;
public:
float getHeight(){
return height;
}
void setHeight(float h){
height = h;
}
float calcVol(){
float circleArea = calcCircleArea();
float vol = circleArea * height;
}
float calcSurfaceArea(){
float circum = calcCircumference();
float circleArea = calcCircleArea();
float cylSurfArea = (circleArea *2) + (circum * height);
}
Cylinder(){} // default constructor
Cylinder(float r, float h){ // overloaded constructor
radius = r;
height = h;
}
~Cylinder(){ // destructor
cout << "Destroying Cylinder Constructor" <<endl;
}
};
class Sphere : public Circle {
public:
float calcSurfaceArea(){
float r = getRadius();
return 4* PI * pow(r,2);
}
float calcVol(){
float r = getRadius();
return (4.0/3.0) * PI * pow(r,3);
}
Sphere(){} // default constructor
Sphere(float r){
radius = r;
}
~Sphere(){ // destructor
cout << "Destroying Sphere Constructor" << endl;
}
};
int main(){
cout << "Enter radius of circle and height of cyl:" << endl;
float r;
float h;
cin >> r >> h;
Sphere s1(r);
Cylinder cyl1(r,h);
Circle cir1(r);
//****************************
// Set up pointers
//****************************
Circle *circPtr;
circPtr = &cir1;
Sphere *sPtr;
sPtr = &s1;
Cylinder *cylPtr;
cylPtr = &cyl1;
cout << "Sphere vol : " << sPtr->calcVol() << endl;
cout << "Sphere SA : " << sPtr->calcSurfaceArea() << endl;
cout << "Cyl vol : " << cylPtr->calcVol() << endl;
cout << "Cyl SA : " << cylPtr->calcSurfaceArea() << endl;
cout << "Circle area : " << circPtr->calcCircleArea() << endl;
cout << "Circle circum : " << circPtr->calcCircumference() << endl;
cout << "sphere RADIUS : " << sPtr->getRadius() << endl;
cout << "cyl pointer VOLUME : " << cylPtr->calcVol() << endl;
cout << "circ pointer AREA: " << circPtr->calcCircleArea() << endl;
delete cylPtr;
delete sPtr;
return 0;
}
You're allocating your cylinder and sphere on the stack, then calling delete on pointers to them. This will destroy your objects twice. Once when you call delete, and once when they go out of scope (main ends).
Don't call delete on objects that you didn't create with new. Especially don't call delete on the address of stack objects.
#include <iostream>
using namespace std;
int main(){
float const PI = 3.1415926;
int radius = 4;
int peri = 0;
int area = 0;
peri =(float) (PI * 2)* radius;
area = (float) PI * (radius * radius);
cout << "Radius is " << radius << endl;
cout << "Perimeter is " << peri << endl;
cout << "Area is " << area << endl;
return 0;
};
peri and area are not converting to float and always receiving a warning "converting to int from float" what seems to be the problem ..
If you really want to truncate peri and area to integers, you should do so explicitly:
peri=static_cast<int>(2*PI*radius);
area=static_cast<int>(PI*radius*radius);
Otherwise, you'll get a warning and it will look like a mistake to anyone who reads your code.
My professor wants me to output the "area" from calculateArea as a char/string. I'm not sure exactly what he means, but maybe some of you might understand.
#include <iostream>
#include "math.h"
#include <cmath>
#include <sstream>
#include <string>
using namespace std;
const char& calculateArea(double diameter, double chord)
{
double length_1, length_2, angle; //This creates variables used by the formula.
angle = acos( (0.5 * chord) / (0.5 * diameter) ); //This calculates the angle, theta, in radians.
cout << "Angle: " << (angle * 180) / 3.14159 << "\n"; //This code displays the angle, currently in radians, in degrees.
length_1 = (sin(angle)) * 6; //This finds the side of the triangle, x.
cout << "X: " << length_1 << " inches "<< "\n"; //This code displays the length of 'x'.
length_2 = (0.5 * diameter) - length_1; /*This code finds the length of 'h', by subtracting 'x' from the radius (which is half the diameter).*/
cout << "h: " << length_2 << " inches" << "\n"; //This code displays the length of 'h'.
double area = ((2.0/3.0) * (chord * length_2)) + ( (pow(length_2, 3) / (2 * chord) ) ); /*This code calculates the area of the slice.*/
ostringstream oss;
oss << "The area is: "<< area << " inches";
string aStr = oss.str();
cout << "Debug: "<< aStr.c_str() << "\n";
const char *tStr = aStr.c_str();
cout << "Debug: " << tStr << "\n";
return *tStr;
//This returns the area as a double.
}
int main(int argc, char *argv[]) {
double dia, cho; //Variables to store the user's input.
cout << "What is your diameter? "; //
cin >> dia; // This code asks the user to input the diameter & chord. The function will calculate
cout << "What is your chord? "; // the area of the slice.
cin >> cho; //
const char AreaPrint = calculateArea(dia, cho); //Sends the input to the function.
cout << AreaPrint; //Prints out the area.
return 0;
}
I get the output as this though:
What is your diameter? 12
What is your chord? 10
Angle: 33.5573
X: 3.31662 inches
h: 2.68338 inches
Debug: The area is: 18.8553 inches
Debug: The area is: 18.8553 inches
T
I need to figure out how to return the string tStr points to. If you don't get what I'm saying, sorry, not really sure what the professor is asking for.
You are returing a char reference not a string.
(the *tStr says 'give me the contents of the pointer')
A more correct version of what you are trying is:
const char* calculateArea(double diameter, double chord)
and
return tStr; // no 'content of'
But this is still bad: the string returned is "out of scope" when aStr goes out of scope, so you really need to return a copy of the characters or just return the string itself (and let the std lib worry about the copy for you)
const string calculateArea(double diameter, double chord)
...
return aStr;