使用类输出时遇到问题

时间:2013-04-12 03:08:18

标签: c++ class gcc

我遇到问题的输出部分时出现问题,我在右下角,左上角和尺寸的行上出现错误。我究竟做错了什么? 我尝试了很多东西,我只是不知道如何让它正常工作,我们没有在课堂上看到类似这样的输出:

#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);
}

1 个答案:

答案 0 :(得分:0)

您正在使用这些函数,因为它们是您需要添加()以正确调用它们的变量:

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;
                                        ^^                                             ^^   

此外,read_triangle没有返回语句,但您声明它返回 double 。流回值返回函数的末尾是undefined behavior,因此您不能依赖结果。它看起来不像您正在使用结果,所以您可能只想更改函数以返回void,这将解决它。