//正方体 classCube : public Cuboid { public: Cube(int a) : Cuboid(a, a, a) {} Cube(int a, int b, int c) : Cuboid(a, b, c) {} intarea(){ return6 * _wid * _hei; } intvolume(){ return _wid * _hei * _len; } };
intmain(void){ Line *p = newCuboid(10, 20, 30); cout << "The area of Cuboid is " << p->area() << endl; cout << "The volume of Cuboid is " << p->volume() << endl;
p = newCube(15); cout << "The area of Cube is " << p->area() << endl; cout << "The volume of Cube is " << p->volume() << endl;
return0; }
/* ============================== The area of Cuboid is 2200 The volume of Cuboid is 6000 The area of Cube is 1350 The volume of Cube is 3375 ============================== */