-5
#include <iostream>
#include <iomanip>
#include "cylinderType.h"  

using namespace std;

int main()
{
    cylinderType cylinder1(4.5, 6.75);
    cylinderType cylinder2;

    cout << fixed << showpoint;
    cout << setprecision(2);

    cout << "***** Cylinder 1 *****" << endl;
    cylinder1.print();
    cout << endl;

    cylinder2.setRadius(3.75);
    cylinder2.setHeight(8.25);

    cout << "***** Cylinder 2 *****" << endl;
    cylinder2.print();
    cout << endl;

    return 0;
}

气缸类型实施

#include <iostream>
#include "circleType.h"
#include "cylinderType.h"
#include <iostream>

using namespace std;

cylinderType::cylinderType(double r, double h):circleType(r)
{
    setHeight(h);
}

void cylinderType::print()
{
     cout << "Radius = " << getRadius()
     << ", height = " << height
     << ", surface area = " << area()
     << ", volume = " << volume();
}

void cylinderType::setHeight(double h)
{
    if (h >= 0)
        height = h;
    else
        height = 0;
}

double cylinderType::getHeight()
{
    return height;
}

double cylinderType::area()
{
    return 2 * 3.1416 * getRadius() * (getRadius() + height);
}

double cylinderType::volume()
{
    return 3.1416 * getRadius() * getRadius() * height;
}

circleType 实现

#include <iostream>
#include "circleType.h"

using namespace std;

void circleType::print()
{
     cout << "Radius = " << radius
     << ", area = " << area()
     << ", circumference = " << circumference();
}

void circleType::setRadius(double r)
{
    if (r >= 0)
        radius = r;
    else
        radius = 0;
}

double circleType::getRadius()
{
    return radius;
}

double circleType::area()
{
    return 3.1416 * radius * radius;
}

double circleType::circumference()
{
    return 2 * 3.1416 * radius;
}

circleType::circleType(double r)
{
    setRadius(r);
}

我不明白问题出在哪里。Cylinder1 将评估得非常好,但是当两个参数都在气缸 2 的打印函数被调用之前传递时,clyinder2 不会......

4

1 回答 1

1

好吧,请注意,您没有针对 cylinderType 的默认构造函数,而且我也没有看到它的 setRadius 方法(当然,鉴于您提供了整个代码)。

于 2013-04-28T19:28:14.250 回答