簡體   English   中英

使用類無法輸出

[英]Having trouble outputting using 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);
}

您正在使用這些函數,就像它們是變量一樣,您需要添加()才能正確調用它們:

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沒有return語句,但是您聲明它返回double 值返回函數的末尾流出是未定義的行為 ,因此您不能依賴結果。 看起來您沒有使用結果,因此您可能只想更改函數以返回void解決該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM