注册 登录
编程论坛 C++教室

帮忙解决一个问题。。。。

昨夜的亡灵 发布于 2010-06-07 21:34, 422 次点击
#include <iostream.h>
class CMyRectangle
{
public:
    CMyRectangle(double w,double h)
    {
        width=w;
        heigh=h;
        cout<<"The object is created!"<<endl;
    }
    ~CMyRectangle()
    {
        cout<<"The object is destroyed"<<endl;
    }
    double CalArea();
    double CalPerimeter();
private:
    double width;
    double heigh;
};
double CMyRectangle::CalArea()
{
    return width*heigh;
}
double CMyRectangle::CalPerimeter()
{
    return (width+heigh)*2;
}
int main()
{
    CMyRectangle myerctangle(12.2,9.8);
    cout<<"the mianji is:"<<myerctangle.CalArea()<<endl;
    cout<<"the zhouchang is"<<myerctangle.CalPerimeter()<<endl;
    return 0;
}


这里的w和h已经固定了,如果要手动输入m和h的值,即cin>>该怎么改?
类与对象我一直没弄好懂,谁有详细的教程,顺便给给,谢谢了。。。

3 回复
#2
南国利剑2010-06-07 23:09
那你就在主函数里输入m和h的值,然后调用的时候传递给构造函数就可以。

至于教程,你百度一下应该有。
#3
marenshi2010-06-09 00:11
#include <iostream.h>
class CMyRectangle
{
public:
    CMyRectangle(double w,double h)
    {
        width=w;
        heigh=h;
        cout<<"The object is created!"<<endl;
    }
    ~CMyRectangle()
    {
        cout<<"The object is destroyed"<<endl;
    }
    void get_data()//在此加输入函数
    {cin>>width>>heigh;
    }
    double CalArea();
    double CalPerimeter();
private:
    double width;
    double heigh;
};
double CMyRectangle::CalArea()
{
    return width*heigh;
}
double CMyRectangle::CalPerimeter()
{
    return (width+heigh)*2;
}
int main()
{
    CMyRectangle myerctangle(12.2,9.8);
    cout<<"the mianji is:"<<myerctangle.CalArea()<<endl;
    cout<<"the zhouchang is"<<myerctangle.CalPerimeter()<<endl;
    myerctangle.get_data();//在此调用
        cout<<"the mianji is:"<<myerctangle.CalArea()<<endl;//再次调用
    cout<<"the zhouchang is"<<myerctangle.CalPerimeter()<<endl;//再次调用
    return 0;
}
#4
2010-06-09 12:58
int main()
{
    double w,h;
    cin>>w>>h;
    CMyRectangle myerctangle(w,h);
    cout<<"the mianji is:"<<myerctangle.CalArea()<<endl;
    cout<<"the zhouchang is"<<myerctangle.CalPerimeter()<<endl;
    return 0;
}
1