- 281
- 0
- 约 13页
- 2016-10-16 发布于广东
- 举报
面向对象C题参考解答
4-8 定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。实现并测试这个类。
#include iostream
using namespace std;
class Dog
{
public:
void setAge(int a)
{
age=a;
}
int getAge()
{
return age;
}
void setWeight(float w)
{
weight=w;
}
float getWeight()
{
return weight;
}
private:
int age;
float weight;
};
void main()
{
Dog d;
d.setAge(3);
d.setWeight(30);
cout小狗:d.getAge()岁,重d.getWeight()斤。endl;
}
4-9 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。
#include iostream
#include math.h
using namespace std;
class Rectangle
{
public:
Rectangle(int xx1,int yy1,int xx2,int yy2)
{
原创力文档

文档评论(0)