2-29 编写一个完整的程序,运行时向用户提问"你考试考了多少分?(0~100)",接收输入后判断其等级,显示出来。规则如下:
解:
#include <iostream.h>
void main()
{
int i,score;
cout << "你考试考了多少分?(0~100):";
cin >> score;
if (score>100 || score<0)
cout << "分数值必须在0到100之间!";
else
{
i = score/10;
switch (i)
{
case 10:
case 9:
cout << "你的成绩为优!";
break;
case 8:
cout << "你的成绩为良!";
break;
case 7:
case 6:
cout << "你的成绩为中!";
break;
default:
cout << "你的成绩为差!";
}
}
}
程序运行输出:
你考试考了多少分?(0~100):85
你的成绩为良!
4-8 定义一个Dog 类,包含的age、weight等属性,以及对这些属性操作的方法。实现并测试这个类。
解:
源程序:
#include <iostream.h>
class Dog
{
public:
Dog (int initialAge = 0, int initialWeight = 5);
~Dog();
int GetAge() { return itsAge;} // inline!
void SetAge (int age) { itsAge = age;} // inline!
int GetWeight() { return itsWeight;} // inline!
void SetWeight (int weight) { itsAge = weight;} // inline!
private:
int itsAge, itsWeight;
};
Dog::Dog(int initialAge, int initialWeight)
{
itsAge = initialAge;
itsWeight = initialWeight;
}
Dog::~Dog() //destructor, takes no action
{
}
int main()
{
Dog Jack(2,10);
cout << "Jack is a Dog who is " ;
cout << Jack.GetAge() << " years old and";
cout << Jack.GetWeight() << " pounds weight.\n";
Jack.SetAge(7);
Jack.SetWeight(20);
cout << "Now Jack is " ;
cout << Jack.GetAge() << " years old and";
cout << Jack.GetWeight() << " pounds weight.";
return 0;
}
程序运行输出:
Jack is a Dog who is 2 years old and 10 pounds weight.
Now Jack is 7 years old 20 pounds weight.
4-9 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。
解:
源程序:
#include <iostream.h>
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle () {}
int GetTop() const { return itsTop; }
int GetLeft() const { return itsLeft; }
int GetBottom() const { return itsBottom; }
int GetRight() const { return itsRight; }
void SetTop(int top) { itsTop = top; }
void SetLeft (int left) { itsLeft = left; }
void SetBottom (int bottom) { itsBottom = bottom; }
void SetRight (int right) { itsRight = right; }
int GetArea() const;
private:
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
}
int Rectangle::GetArea() const
{
int Width = itsRight-itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}
int main()
{
Rectangle MyRectangle (100, 20, 50, 80 );
int Area = MyRectangle.GetArea();
cout << "Area: " <
安工大工商学院首届好菜... 暂无评价 9页 免费 电路电子实验复习总结(安.....C++语言是目前应用最广的面向对象的编程语言。 二、什么是面向对象方法?这种方法...
安工大2008工商学院期末考... 暂无评价 2页 免费 安工大美学原理期末考试答.... 安工大企业管理期末考试知识点 隐藏>> 01、管理有哪些职能。 计划职能 组织...
安工大工商学院C++期末考试... 3页 5财富值 安工大2011-2012实变与泛函.....答案要点: 实证经济学不讲价值判断。回答“是什么”的问题。 (只描述不评价) ...
电子商务实验报告安工大 10页 2财富值 安工大工商学院C++期末考试... 3页 ... 实验二 门电路特性 3页 免费 C语言实验报告-安徽工业大... 26页 2财富值...
湖南工业大学 2010 届计算机与通信学院 C++期末考试之问答题重点这里面是从 06 ...面向:它吸取了面向过程的程序设计的优点,同时又考虑了现实世界和计算机处理的关系...
安工大工商学院 2010_2011学年第一学期汇编语言期末...学年第一学期期末考试《汇编语言》试卷(A)一、填空...(4) INT 21H CODE ENDS END START 五、编程题(...
C++面向对象程序设计 2010期末考试 编程题_工学_高等教育_教育专区。C++面向对象程序设计 2010期末考试 编程题1、编写一个程序根据输入的三角形的三条边判断是否...
C语言题库(编程)河南工业大学 河工大 c语言期末考试题库_其它_高等教育_教育专区。二、编程题 1.有一函数:当x<0时,y=-1;当x=0时,y=0;当x>0时,y=...
5 页第 3 页 学号 姓名 装 订 线 沈阳工业大学班级得分 五、简单编程题...函数中使用 int 型、 float 型和 char 型数据通过调用 Swap 函数来测试该函数...
安工大操作系统期末考试绝对重点已经考完检验过_IT认证_资格考试/认证_教育专区...多道程序设计技术有什么特点:答:多道 程序设计是允许多个作业(程序)同时进入...
热门文档