C 上机实验报告
- 格式:pdf
- 大小:170.42 KB
- 文档页数:10
Square(int a = 0); void setRadius(int a = 0); int getBian() {
return bian; } virtual double area() const; virtuaห้องสมุดไป่ตู้ void printName() const {
cout<<"Square(正方形):"; } }; Square::Square(int a) { bian = a; } double Square::area() const { return bian*bian; } void vpf(const TDshape *bptr) { bptr->printName(); cout<<"\narea:"<<bptr->area()<<"\n\n"; } void vrf(const TDshape &bref) { bref.printName(); cout<<"\narea:"<<bref.area()<<"\n\n"; } int main() { Circle c1(2); Square s1(4); TDshape *arrayOfTDshapes[2]; arrayOfTDshapes[0] = &c1; arrayOfTDshapes[1] = &s1; cout<<"******通过基类指针访问虚函数******\n"; for(int i=0;i<2;i++)
上机考试题 1
定义盒子 Box 类,要求具有以下成员变量:长、宽、高分别为 length,
with,high;具有一个静态成员变量:盒子的个数 number;具有以
下成员函数:1)构造函数和析构函数;2)计算盒子体积的函数;3)
计算盒子的表面积的函数;4)显示长、宽、高、体积和表面积的函
数;5)显示盒子个数的静态成员函数。编写 main 函数进行测试。
数的函数。编写 main 函数进行测试。
解答:
程序编写:
#include <iostream> using namespace std; class Vehicle { private:
int speed,weight; public:
Vehicle(int s,int w) {
speed = s; weight = w; } void setSpeed(int s) { speed = s; } void setWeight(int w) { weight = w; } int getSpeed() { return speed; } int getWeight() { return weight; } void display() { cout<<"速度:"<<speed<<endl; cout<<"重量:"<<weight<<endl; } }; class Bicycle:public Vehicle { private: int high; public: Bicycle(int s,int w,int h); void setHigh(int h) { high = h; } int getHigh() { return high; } void display2() { display();
调试结果:
上机考试题 2
定义一个车基类 Vehicle,含私有成员 speed、weight。派生出自行
车类 Bicycle,增加 high 成员;派生出汽车类 Car,增加 seatnum(座
位数)成员。每一个类都有:各自的构造函数、设置(如 setSpeed
等)和返回每个成员变量(如 getSpeed 等)的函数、显示名称和参
b1.display2(); cout<<"*********************"<<endl; Car c1(150,4500,12); c1.display3(); c1.setSpeed(135); c1.setWeight(8700); c1.setSeatnum(25); c1.display3(); }
解答:
程序编写:
#include <iostream> using namespace std; class Box { private:
int length,with,high; static int number; public: Box(int L,int W,int H) {
length = L; with = W; high = H; number++; } ~Box() {} int getLength() { return length; } int getWith() { return with; }
vpf(arrayOfTDshapes[i]); cout<<"******通过基类引用访问虚函数******\n"; for(int j=0;j<2;j++)
vrf(*arrayOfTDshapes[j]); return 0;
}
调试结果:
心得总结
这次上机考试的题目虽然不多,也就仅仅 3 个,而且还和老师上 课讲解的题目有很多相似之处,但是我在做的时候依然会磕磕绊绊, 在自认为对的情况下运行程序依旧会有很多的错误,尤其是其中有许 多不应该犯的低级错误,这让我感到汗颜;但是也有很大一部分是自 己一直以来就根本没有理解懂的,也让我对自己有了一个重新的认 识,原来自己的功底是如此之差,不过这也给我敲响了警钟,让我再 也不敢有轻慢之心。C++编程虽然比较难以学习与理解,但是慢慢贯 通也是会有不小的收获,我从一开始的完全不懂到现在的磕磕绊绊, 也算是小有收获吧。相信只要继续坚持下去,只要不断的运用它,总 会在 C++上取得不俗的成绩。
函数进行测试。
解答:
程序编写:
#include <iostream> using namespace std; class TDshape { public:
virtual double area() const {return 0.0;} virtual void printName() const = 0; }; class Circle:public TDshape { private: int radius; public: Circle(int r = 0); void setRadius(int r = 0); int getRadius() {
cout<<"高度:"<<high<<endl; } }; Bicycle::Bicycle(int s,int w,int h):Vehicle(s,w) { high = h; } class Car:public Vehicle { private: int seatnum; public: Car(int s,int w,int se); void setSeatnum(int se) {
调试结果:
上机考试题 3
写一个程序,定义抽象类 TDshape,由它派生两个类:Circle(圆形), Square(正方形)。各自需要定义其独有的数据成员和成员函数。用 虚函数 area()和 printName()分别计算几种图形的面积和显示图 形名称。要求编写以 TDshape 为接口的函数,借以访问具体类如 Circle 和 Square 类的成员函数 area()和 printName()。编写 main
int getHigh() {
return high; } static int getNumber() {
return number; } int tiji(); int mianji(); }; int Box::tiji() { int T = length*with*high; return T; } int Box::mianji() { int M = 2*(length*with+length*high+with*high); return M; } int Box::number = 0; void main() { Box b1(2,5,10); Box b2(4,5,10); cout<<"长:"<<b1.getLength()<<endl; cout<<"宽:"<<b1.getWith()<<endl; cout<<"高:"<<b1.getHigh()<<endl; cout<<"体积:"<<b1.tiji()<<endl; cout<<"表面积:"<<b1.mianji()<<endl; cout<<"*************************"<<endl; cout<<"长:"<<b2.getLength()<<endl; cout<<"宽:"<<b2.getWith()<<endl; cout<<"高:"<<b2.getHigh()<<endl; cout<<"体积:"<<b2.tiji()<<endl; cout<<"表面积:"<<b2.mianji()<<endl; cout<<"*************************"<<endl; cout<<"总共:"<<b2.getNumber()<<"个盒子"<<endl; }