I am having trouble with the output part of the problem, I am getting errors on the lines that say bottom right, top left, and dimension. What am i doing wrong?
I have tried many things and I just do not know how to get it to work correctly, and we have not gone over anything like this kind of output in class:
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
private:
double px;
double py;
public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};
class Triangle
{
private:
Point blPoint;
double length, height;
public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);
Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;
double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void display() const;
};
// FUNCTION PROTOTYPES GO HERE:
double read_triangle(Triangle & tri);
int main()
{
// Define local variables
Triangle tri;
double sx, sy;
//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);
// Display triangle information
tri.display();
// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;
cout << "Enter scale factor in y direction: ";
cin >> sy;
// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);
// Display triangle information
tri.display();
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:
void Point::setX(const double x)
{
px = x;
}
void Point::setY(const double y)
{
py = y;
}
double Point::getX() const
{
return (px);
}
double Point::getY() const
{
return (py);
}
void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
blPoint.setX(x);
}
void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
blPoint.setY(y);
}
void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */
length=inLength;
}
void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */
height=inHeight;
}
Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
return (blPoint);
}
Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */
Point getBottomRight;
double mx = (blPoint.getX()+ length);
getBottomRight.setX(mx);
return(getBottomRight);
}
Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
Point getTopLeft;
double my = (blPoint.getY()+ height);
getTopLeft.setY(my);
return (getTopLeft);
}
double Triangle::getLength() const
{
/* INSERT YOUR CODE */
return (length);
}
double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
return (height);
}
double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
//hypotenuse = (sqrt((height * height)+(length * length)));
return (sqrt((height * height)+(length * length)));
}
double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
//perimeter = ((sqrt((height * height)+(length * length)))+ height + length);
return ((sqrt((height * height)+(length * length)))+ height + length);
}
void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */
length = scalefact * length;
}
void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
height = scalefact * height;
}
void Triangle::display() const
{
/* INSERT YOUR CODE */
cout <<"---------------------------------------" << endl;
cout << "Lower Left Vertex (" << blPoint.getX() << ", " << blPoint.getY() << ')' <<endl;
cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft.getY() << ')' << endl;
cout << "Bottom Right Vertex (" << getBottomRight.getX() << ", " << blPoint.getY() << ')' << endl;
cout << "Dimensions (" << getBottomRight.getX()- blPoint.getX() << ", " << getTopleft.getY() - blPoint.getY() << ')' << endl;
cout << "Hypotenuse = " << hypotenuse() << endl;
cout << "Perimeter = " << perimeter() << endl;
cout <<"---------------------------------------" << endl;
}
double read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
double x, y, inLength, inHeight;
cout << "Enter bottom left x coordinate: ";
cin >> x;
tri.setBottomLeftX(x);
cout << "Enter bottom left y coordinate: ";
cin >> y ;
tri.setBottomLeftY(y);
cout << "Enter length: ";
cin >> inLength;
tri.setLength(inLength);
cout << "Enter Height: ";
cin >> inHeight;
tri.setHeight(inHeight);
}
You are using the functions like they are variables you need to add () to call them correctly:
cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft().getY() << ')' << endl;
^^
cout << "Bottom Right Vertex (" << getBottomRight().getX() << ", " << blPoint.getY() << ')' << endl;
^^
cout << "Dimensions (" << getBottomRight().getX()- blPoint.getX() << ", " << getTopLeft().getY() - blPoint.getY() << ')' << endl;
^^ ^^
Also, read_triangle does not have a return statement but you declare that it returns double. Flowing off the end of a value returning function is undefined behavior and therefore you can not rely on the results. It does not look like you are using the results so you may want to just change the function to return void and that will fix it.
Related
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 9 months ago.
Improve this question
I have tried to run the program with more than one inheritance i got many errors
For this program ,but when i use it for Source File, it will looks good
I don't know why but I think because i update my version or does make any sense
please help me with my project
int main
#include"Shapes.h"
#include"Cylinder.h"
#include"Sphere.h"
#include"Triangle.h"
#include"Square.h"
#define MAX_SHAPES 100
int main() {
ofstream myfile;
myfile.open("Shapes.dat");
Shape* shapes[MAX_SHAPES];
int currentShapes = 0;
int n = 0;
cout << "Select any Number to calculate : \n";
cout << "1. Square\n";
cout << "2. Triangle\n";
cout << "3. Sphere\n";
cout << "4. Cylinder\n";
cout << "Enter your choice: ";
cin >> n;
cin.ignore();
if (n == 1) {
Square* obj2 = new Square();
obj2->readData();
shapes[currentShapes] = obj2;
}
if (n == 2) {
Triangle* obj3 = new Triangle();
obj3->readData();
shapes[currentShapes] = obj3;
}
if (n == 3) {
Sphere* obj5 = new Sphere();
obj5->readData();
shapes[currentShapes] = obj5;
}
if (n == 4) {
Cylinder* obj7 = new Cylinder();
obj7->readData();
obj7->computeSurfaceArea();
obj7->computeVolume();
obj7->print();
obj7->printToFile();
shapes[currentShapes] = obj7;
}
return 0;
}
shape.h
class Shape {
private:
string color, name;
public:
// default constructor
Shape()
{
color = 1.0;
name = "";
cout << "Base Class is Shape for this element . \n";
}
// parameterized constructor
Shape(string color, string name) {
this->color = color;
this->name = name;
}
// read data
void readData() {
cout << "Enter color: ";
getline(cin, color);
cout << "Enter name: ";
getline(cin, name);
}
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
shape2d.h
class Shape2D : public Shape {
public:
double area;
double perimeter;
Shape2D() :Shape()
{
cout << "Parent Class is Shape2D for this element .\n";
}
// parameterized constructor
Shape2D(string color, string name) :Shape(color, name) {
}
// read data
void readData() {
Shape::readData();
}
virtual void computeArea() = 0;
virtual void computePerimeter() = 0;
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
Shape3d
class Shape3D : public Shape {
public:
double surfaceArea;
double volume;
Shape3D() :Shape()
{
surfaceArea = 1.0;
volume = 1.0;
cout << "Parent Class is Shape3D for this element .\n";
}
// parameterized constructor
Shape3D(string color, string name) :Shape(color, name) {
}
void readData() {
Shape::readData();
}
virtual void computeSurfaceArea() = 0;
virtual void computeVolume() = 0;
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
Square.h
class Square : public Shape2D {
private:
double side;
public:
Square() :Shape2D()
{
side = 1.0;
cout << "Calculating square area and perimeter \n";
}
// parameterized constructor
Square(string color, string name, double side) :Shape2D(color, name) {
this->side = side;
}
void readData() {
Shape2D::readData();
cout << "Square Side: ";
cin >> side;
cin.ignore();
computeArea();
computePerimeter();
}
void computeArea()
{
area = side * side;
}
void computePerimeter() {
perimeter = 4 * side;
}
void print(string x, double y)
{
cout << x << " of Square = " << y << "\n";
}
void print() {
print("Area: ", area);
print("Perimeter", perimeter);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Square = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Area: ", area);
printToFile("Perimeter", perimeter);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Triangle.h
class Triangle : public Shape2D {
private:
double base;
double height;
public:
Triangle() :Shape2D()
{
cout << "This is a Triangle\n";
}
// parameterized constructor
Triangle(string color, string name, double base, double height) :Shape2D(color, name) {
this->base = base;
this->height = height;
}
void readData() {
Shape2D::readData();
cout << "Triangle Base: ";
cin >> base;
cout << "Triangle Height: ";
cin >> height;
cin.ignore();
computeArea();
computePerimeter();
}
void computeArea() {
area = (height * base) / 2;
}
void computePerimeter() {
perimeter = 2 * height + base;
}
void print(string x, double y)
{
cout << x << " of Triangle = " << y << "\n";
}
void print() {
print("Area: ", area);
print("Perimeter: ", perimeter);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Triangle = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Area: ", area);
printToFile("Perimeter", perimeter);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Sphere.h
class Sphere : public Shape3D {
private:
double radius;
public:
Sphere() :Shape3D()
{
radius = 1.0;
cout << "This is a Sphere\n";
}
// parameterized constructor
Sphere(string color, string name, double side) :Shape3D(color, name) {
this->radius = radius;
}
void readData() {
Shape3D::readData();
cout << "Sphere Radius: ";
cin >> radius;
cin.ignore();
computeSurfaceArea();
computeVolume();
}
void computeSurfaceArea() {
surfaceArea = 4 * 3.14 * radius * radius;
}
void computeVolume() {
volume = (4 / 3) * (3.14 * radius * radius * radius);
}
void print(string x, double y)
{
cout << x << " of Sphere = " << y << "\n";
}
void print() {
print("Surface Area: ", surfaceArea);
print("Volume: ", volume);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Sphere = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Surface Area: ", surfaceArea);
printToFile("Volume", volume);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Cylinder .h
class Cylinder : public Shape3D {
private:
double radius;
double height;
public:
Cylinder() :Shape3D()
{
radius = 1.0;
height = 1.0;
cout << "This is a Cylinder\n";
}
// parameterized constructor
Cylinder(string color, string name, double radius, double height) :Shape3D(color, name) {
this->radius = radius;
this->height = height;
}
void readData() {
Shape3D::readData();
cout << "Cylinder Radius: ";
cin >> radius;
cout << "Cylinder Height: ";
cin >> height;
cin.ignore();
//computeSurfaceArea();
//computeVolume();
}
void computeSurfaceArea() {
surfaceArea = (2 * 3.14 * radius) * (radius + height);
}
void computeVolume() {
volume = 3.14 * radius * radius * height;
}
void print(string x, double y)
{
cout << x << " of Cylinder = " << y << "\n";
}
void print() {
print("Surface Area: ", surfaceArea);
print("Volume: ", volume);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Cylinder = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Surface Area: ", surfaceArea);
printToFile("Volume", volume);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
In shape2d.h and shape3d.h with have to add
#include "shape.h"
In square.h and triangle.h with have to add
#include "shape2d.h"
In sphere.h and cylinder.h you have to add
#include "shape3d.h"
And to avoid multiple inclusions add at the beginning of each .h file
#pragma once
or if your compiler doesn't support it enclose the hole code inside any .h in something like
#ifndef YOUR_H_FILE_NAME_H
#define YOUR_H_FILE_NAME_H
// ... your .h code here
#endif
Further in main.cpp you include shapes.h which doesn't exist but you can remove it because you already include all the specilized .h file
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 am trying to convert different coordinate systems. From polar to rectangular and vice versa. My pol_to_rect()function is not working properly. It is giving very small values(~10^(-44)) after converting and also before converting. There might be some problem while using the sin() and cos() functions. The rect_to_pol() is working fine for positive values.
Edit - When I changed atan() to atan2() how can I incorporate other values of x and y.
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.1415926
class Polar; // Forward declaration
class Rectangular {
private:
float x, y;
public:
Rectangular() {} // default constructor
Rectangular(float mv_x, float mv_y) {
x = mv_x;
y = mv_y;
}
void showData() const;
Polar rect_to_pol();
float& get_x() {
return x;
}
float& get_y() {
return y;
}
};
void Rectangular::showData() const {
cout << "--Rectangular--" << endl;
cout << "x: " << x << "\t" <<"y: " << y << endl;
}
class Polar {
private:
float r;
float theta;
public:
Polar() {} // default constructor
Polar(float mv_r, float mv_theta) {
r = mv_r;
theta = mv_theta;
}
void showData();
Rectangular pol_to_rect();
float& get_r(){
return r;
}
float& get_theta() {
return theta;
}
};
void Polar::showData() {
cout << "--Polar--" << endl;
cout << "r:" << r << "\t" << "Theta(Radians):" << theta << endl;
}
Rectangular Polar::pol_to_rect() {
Rectangular temp;
temp.get_x() = r * cos(theta*(PI/180.0)); // in degrees
temp.get_y() = r * sin(theta*(PI/180.0));
return temp;
}
Polar Rectangular::rect_to_pol() {
Polar temp;
temp.get_r() = sqrt(pow(x, 2) + pow(y, 2));
temp.get_theta() = atan2(y, x);
return temp;
}
int main()
{
Rectangular r1(-1, -1), r2;
Polar p1(12.0, 30.0), p2;
r1.showData();
p2 = r1.rect_to_pol();
cout << "After Conversion (RECT TO POLAR)->" << endl;
p2.showData();
p1.showData();
r2 = p1.pol_to_rect();
cout << "After Conversion (POLAR TO RECT)" << endl;
r2.showData();
return 0;
}
In my program I have created a constructor called Point with two values. I also have a set, get, scale and translate function. I'm trying to create a function that allows me to get the distance between the object and another point. I'm have trouble with it though any help would be brilliant.
#ifndef POINTMODEL
#define POINTMODEL
#define POINTDEB UG
#include <iostream>
#include <string.h>
using namespace std;
class Point {
public:
Point(void);
Point(double anX, double aY);
~Point();
void setPoint(double anX, double aY);
double getX();
double getY();
double scaleX(double theX);
double scaleY(double theY);
void translate(double theX, double theY);
void distance(const Point& aPoint);
protected:
private:
double theX;
double theY;
};
inline Point::Point(void)
{
theX = 1;
theY = 1;
cout << "\n The default constructor was called" << endl;
}
inline Point::Point(double anX, double aY)
{
cout << "\n regular constructor called";
}
inline Point::~Point()
{
cout << "\n the destructor was called" << endl;
}
inline void Point::setPoint(double anX, double aY)
{
theX = anX;
theY = aY;
}
inline double Point::getX()
{
return theX;
}
inline double Point::getY()
{
return theY;
}
inline double Point::scaleX(double theX)
{
return theX;
}
inline double Point::scaleY(double theY)
{
return theY;
}
inline void Point::translate(double offSetX, double offSetY)
{
cout << "X is translated by : " << offSetX << endl;
cout << "Y is translated by : " << offSetY << endl;
}
inline void Point::distance(const Point& aPoint)
{
}
#endif
Cpp file:
#include "Point.h"
using namespace std;
int main(void)
{
cout << "\n main has started" << endl;
//Point myPoint;
Point myPoint(1, 1);
myPoint.setPoint(1, 1);
cout << "\n The value for X is : " << myPoint.getX() << endl;
cout << "\n The value for Y is : " << myPoint.getY() << endl;
cout << "\n X scaled by 2 is : " << myPoint.scaleX(2) << endl;
cout << "\n Y scaled by 2 is : " << myPoint.scaleY(2) << endl;
myPoint.translate(2, 3);
cout << "\n main has finished" << endl;
return 0;
}
You need to make your Point::getX() and Point::getY() functions const like so:
inline double Point::getX() const
{
return theX;
}
If they are not const you cannot call them when the parameter is a const reference.
Then the distance is (changed return from void to double):
double distance(const Point & aPoint) const
{
const double x_diff = getX() - aPoint.getX();
const double y_diff = getY() - aPoint.getY();
return std::sqrt(x_diff * x_diff + y_diff * y_diff);
}
I have deliberately not used std::pow since the exponent is 2.
You also need to include <cmath> for std::sqrt.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
So far I've managed to fix my own errors, but this one is keeping me puzzled for a while now. If any of you have any hints, that would be greately appreciated! Thanks.
I'm using Eclipse on a Windows laptop, which gives me the following error after building:
undefined reference to `Point::Point()' circle.cpp
/Assignment4_1/IN4084MSc line 13 C/C++ Problem
All my files (main4_1.cpp, point.h, point.cpp, circle.h, circle.cpp) are part of a project (called Assignment4_1).
#include "point.h"
#include "circle.h"
#include <cmath>
using namespace std;
Circle::Circle(Point ct, double rd):Point(ct)
{
center = ct;
if(rd > 0)
{
radius = rd;
}
radius = -1;
}
Point Circle::get_center(){
return center;
}
double Circle::get_radius(){
return radius;
}
void Circle::print(){
cout << " <Circle(<Point(“"<<center.get_x()<<"”,“"<<center.get_y()<<"”)>,”"<<radius<<"”)>";
}
void Circle::print(string s){
cout << s;
print();
}
void Circle::println(){
print();
cout << endl;
}
void Circle::println(string s){
print(s);
cout << endl;
}
#ifndef CIRCLE_H_
#define CIRCLE_H_
#include <iostream>
#include "point.h"
using namespace std;
class Circle: public Point{
Point center;
double radius;
public:
Circle(Point ct, double rd);
Point get_center();
double get_radius();
void print();
void print(string s);
void println();
void println(string s);
};
#endif /* CIRCLE_H_ */
#include "point.h"
#include <cmath>
using namespace std;
Point::Point(double x, double y){
x_coord = x;
y_coord = y;
}
double Point::get_x(){
return x_coord;
}
double Point::get_y(){
return y_coord;
}
void Point::print(){
//post: has printed the contents of the Point object in the format
cout << "<Point( “ " << x_coord <<" ” , “ " << y_coord << " ” )>";
}
void Point::print(string s){
//post: has printed string s first, then printed the contents of the Point object
cout << s << " ";
print();
}
void Point::println(){
//post: has printed the contents of Point object, then moved the cursor to the next line
print();
cout << endl;
}
void Point::println(string s){
//post: has printed string s first, then printed the contents of the Point object, and moved the cursor to the next line
cout << s << " ";
println();
}
void Point::move(double dx, double dy){
// post: x_coord = x_coord + dx, y_coord = y_coord + dy
x_coord = x_coord + dx;
y_coord = y_coord + dy;
}
double Point::distance(Point that){
//post: returns the distance between this Point and that Point
return sqrt( pow(x_coord - that.get_x(),2) + pow(y_coord - that.get_y(),2) );
}
bool Point::equals(Point that){
//post : returns true if this Point and that Point have the same coordinates
return (x_coord = that.get_x());
}
#ifndef POINT_H_
#define POINT_H_
#include <iostream>
using namespace std;
class Point{
protected:
double x_coord;
double y_coord;
public:
Point(double x, double y);
Point();
double get_x();
double get_y();
void print();
void print(string s);
void println();
void println(string s);
void move(double dx, double dy);
double distance(Point that);
bool equals(Point that);
};
#endif /* POINT_H_ */
#include <iostream>
#include <cstdlib>
using namespace std;
#include "point.h"
#include "circle.h"
void test1(){
cout << "test1:" << endl;
Point p1 = Point(2,3);
cout << "Point p1 is created, the data of p1 = (2,3)" << endl;
cout << "p1.get_x() -> " << p1.get_x() << endl;
cout << "p1.get_y() -> " << p1.get_y() << endl << endl;
p1.println("p1.println() -> ");
cout << endl;
cout << "end test1" << endl << endl;
}
void test2(){
cout << "test2:" << endl;
cout << "Point p1 is created, the data of p1 = (2,3)" << endl;
cout << "Point p2 is created, the data of p2 = (0,0)" << endl;
Point p1 = Point(2,3);p1.println("p1.println() -> ");
Point p2 = Point(0,0);p2.println("p2.println() -> ");
cout << endl;
p1.println("p1 before move -> ");
cout << "p1.move(1,1)" << endl;p1.move(1,1);
p1.println("p1 after move -> ");
cout << endl;
cout << "p1.distance(p2) -> " << p1.distance(p2) << endl << endl;
cout << "p1.equals(p1) -> " << p1.equals(p1) << endl;
cout << "p1.equals(p2) -> " << p1.equals(p2) << endl;
cout << "end test2" << endl << endl;
}
int main(){
test1();
test2();
return 0;
}
The compiler is telling you to define
Point::Point()
which you have only declared in the Point class definition. A possible implementation would be to initialize both coordinates with 0.0:
Point::Point() : x_coord(0.0), y_coord(0.0) {}
You have to explicitly initialize the members in this case because built-in types do not get zero-initialized when default constructed.