《北邮信通C++上机实验 第七章》由会员分享,可在线阅读,更多相关《北邮信通C++上机实验 第七章(28页珍藏版)》请在金锄头文库上搜索。
1、第七章实验题一基础题1.#include using namespace std; class CSample double a; CSample()a=2.5; public: CSample(int val); CSample(); ;2.#include using namespace std; class cubepublic: cube();/构造函数cube();/析构函数int volume(); /定义整形函数private:int height, width, depth;/定义私有数据成员; cube:cube() height = 100; depth = 4; widt
2、h = 2; coutConstructor called.endl; cube:cube() cout Destructor called. endl; system(pause); int cube:volume() return height*width*depth; void main() cube cubeone; /定义对象cout立方体的体积为:cubeone.volume()endl; system(pause);3.#include using namespace std;class CCounterint value;public:CCounter()cout CCount
3、er Constructor1 endl;value = 0; CCounter(int val)cout CCounter Constructor2 endl;value = val;CCounter() cout CCounter Destructor endl; system(pause);class CExampleint value;public:CCounter car;CExample()cout CExample Constructor1 endl;value = 0;CExample(int val)cout CExample Constructor2 endl;value
4、= val;CExample() cout CExample Destructor endl; system(pause);void Display() cout value= value endl; ;void main()CExample obj(350);obj.Display();system(pause);/调用顺序:CCounter()-CExample(int val)-CExample()-CCounter()4.#include using namespace std;class Pointprotected:int x;public:int Getx() return x;
5、 int Y;void init(int a, int b) x = a; Y = b; int Gety() return Y; void Setx(int a) x = a; void Sety(int b) Y = b; ;void main()Point A, B;A.init(25, 27);B.init(29, 31);cout A.x= A.Getx() endl;cout B.x= B.Getx() endl;cout A.Y= A.Y endl;cout A.y= A.Gety() endl;cout B.y= B.Y endl;cout B.y= B.Gety() endl
6、;system(pause);5.#include using namespace std;class baseprivate:int i;static int k; /定义静态变量k public:base() i = 0; k+; /定义构造函数void Display();void base:Display()k = k + 10;i+;cout i= i ,k= k 0)cout Its a positive number endl;positive+;else if (x = 0)zero+;cout Its zero endl;elsenegative+;cout Its a ne
7、gative number n)num test(n);test.decision();test.print();system(pause);2.#include using namespace std;class dianqipublic:void getprice(int x , int y, int z )colorcast = x;ridge = y;washer = z;void total(int a , int b , int c )int t = a*colorcast + b*ridge + c*washer;cout t endl;private:int colorcast
8、;int ridge;int washer;void main()dianqi dian;dian.getprice(4200, 2800, 1500);cout a;cout b;cout c;cout endl;cout 总营业额为:;dian.total(a, b, c);system(pause);3.#include#includeusing namespace std;class carpublic:void disp_parameters()cout 型号: type endl;cout 颜色: colour endl;cout 车门数: door endl;cout 座位数:
9、seat endl;cout 轴距: distance endl;void change(string a, char b, int c, int d, int e)type = a;colour = b;door = c;seat = d;distance = e;private:string type;char colour;int door;int seat;int distance;void main()car gundam;gundam.change(Double O GUNDAM, b, 1, 5, 6);gundam.disp_parameters();system(pause)
10、;4.#include using namespace std;class studentpublic:int num;char name20;char sex;int age;void main()student stu5;student temp;for (int i = 0; i5; i+)cout 输入第 i + 1 stui.num;cout 输入第 i + 1 stui.name;cout 输入第 i + 1 stui.sex;cout 输入第 i + 1 stui.age;for (int a = 1; a 5; a+)for (int b =0 ; b 5-a; b+)if (stub.agestub + 1.age)temp.age = stub + 1.age;stub + 1.age = stub.age;stub.age = temp.age;