Cylinder1工作正常,但是cylinder2给出错误?

时间:2013-04-28 19:06:04

标签: c++

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

cylinderType实现

#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 implementmecation

#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都不会...

1 个答案:

答案 0 :(得分:1)

好吧,请注意你没有cylinderType的默认构造函数,我也没有看到它的setRadius方法(假设你提供了整个代码)。