C++ HW10-1 for WallPower

若有長方體物件分別為,一號長方體c1(2,3,3),二號長方體c2(5,5,5),求兩者體積。
參考輸出如下。




#include <iostream>
using namespace std;

class Couboid
{
public:
 Couboid(int x, int y, int z);
 ~Couboid();
 int volume() { return (*x)*(*y)*(*z); }
private:
 int *x, *y, *z;
};

Couboid::Couboid(int a, int  b, int c)
{
 x = new int;
 y = new int;
 z = new int;
 *x = a;
 *y = b;
 *z = c;
}

Couboid::~Couboid()
{
 delete x;
 delete y;
 delete z;
}

int main()
{
 Couboid c1(2, 3, 3), c2(5, 5, 5);
 cout << "一號長方體體積:" << c1.volume() << endl;
 cout << "二號長方體體積:" << c2.volume() << endl;
 system("pause");
  return 0;
}

留言

這個網誌中的熱門文章

UVA 11321 Java