这是我的代码,我对C 是相对较新的。我曾经写过的唯一其他C 程序是ATM应用程序。对于这个项目,我正在尝试找到一个盒子的区域,任何建议n为什么不起作用?
无论如何,我的代码
/*
* c.cpp
*
* Created on: Jan 31, 2014
* Author: University of Denver. Yeah, I smoke weed.
*/
class Box
{
public:
// pure virtual function
virtual double getVolume() = 0;
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
#include <iostream>
using namespace std;
// Base class
class Shape
{
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}
代码是有效编译的,输出35和17。我认为问题是您的控制台窗口被系统迅速关闭,而您看不到结果。您可以插入任何将等到输入内容的命令,例如
int i;
std::cin >> i;
或返回之前类似的东西
如果使用MS VC ,则可以使用键组合CTRL F5
运行控制台应用程序还考虑到您的类框未使用并且可以删除。