CPP2022-24-类与对象(下)

简介: CPP2022-24-类与对象

7a0ad386829e44de909e7e38ea5cd0d2.jpg

程序填空题

1. #include <iostream>
2. using namespace std;
3. class CAT
4. {     public:
5. CAT();
6. CAT(const CAT&);
7.           ~CAT();
8. int GetAge() const { return *itsAge; }
9. void SetAge(int age){ *itsAge=age; }
10. protected:
11. int* itsAge;
12. };
13. CAT::CAT()
14. {    itsAge=new int;
15.      *itsAge =5;
16. }
17. CAT::CAT(const CAT& c)
18. {
19. itsAge = new int
20. ; 
21. *itsAge= *(c.itsAge)
22. ; 
23. }
24. CAT::~CAT()
25. {     delete itsAge;   }
1. #include <
2. iostream
3. >
4. using namespace std;
5. 
6. class Point
7. {
8. private:
9. //访问权限设置,私有权限
10. int x;//横坐标
11. int y;//纵坐标
12. public:
13. //访问权限设置,公有权限
14. 
15. //以下为构造函数,用参数a,b分别为横纵坐标进行初始化
16. 
17. Point
18. (int a,int b)
19.     {
20. 
21. x=a
22. ;
23. 
24. y=b
25. ;
26.     }
27. 
28. //以下为拷贝构造函数,借用对象a_point完成初始化
29. Point(
30. Point &
31. a_point)
32.     {
33.         x=a_point.x;
34.         y=a_point.y;
35.     }
36. 
37. //以下为析构函数
38. 
39. ~Point()
40. 
41.     {
42.         cout<<"Deconstructed Point";
43. print();
44.     }
45. 
46. //以下为输出点的信息的函数,要求在一行中输出点的坐标信息,形如:(横坐标,纵坐标)
47. void print()
48.     {
49.         cout<<
50. "("<
51. <<endl;
52.     }
53. };
54. 
55. 
56. int main()
57. {
58. Point b_point(0,0);
59.     b_point.print();
60. int a,b;
61. 
62. cin>>a>>b;
63. //从标准输入流中提取数值给a,b
64. Point c_point(a,b);
65.     c_point.print();
66. 
67. return 0;
68. //主函数的返回语句
69. }
70. /*设输入为10 10,则本程序的运行结果为:
71. (0,0)
72. 
73. (10,10)
74. 
75. Deconstructed Point(10,10)
76. 
77. Deconstructed Point(0,0)
78. 
79. */
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. public:
6. Box(int,int,int);
7. private:
8. int length;
9. int width;
10. int height;
11. };
12. Box::Box(int len,int w,int h)
13. 
14. {
15.     length=len;
16.     width=w;
17.     height=h;
18.     cout<<"The Box is created. Parm:length="<<length<<", width="<<width<<", height="<<height<<"."<<endl;
19. }
20. int main( )
21. {
22. 
23. Box Box(12,10,5);
24. 
25. return 0;
26. }

函数题

 

6-1 创建CPU

分数 10

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

定义一个CPU类,包含等级(Rank)、频率(frequency)、电压(voltage)等属性。其中,rank为枚举类型CPU__Rank,定义为enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7},frequency为单位是MHz的整型数,voltage为浮点型的电压值。

函数接口定义:

根据题目要求写出类。

裁判测试程序样例:

/* 请在这里填写答案 */ int main() { CPU a(P6,3,300); cout<<"cpu a's parameter"<<endl; a.showinfo(); //显示性能参数 CPU b; cout<<"cpu b's parameter"<<endl; b.showinfo(); //显示性能参数 CPU c(a); cout<<"cpu c's parameter"<<endl; c.showinfo(); //显示性能参数 }

输入样例:

输出样例:

1. create a CPU!
2. cpu a's parameter
3. rank:6
4. frequency:3
5. voltage:300
6. create a CPU!
7. cpu b's parameter
8. rank:1
9. frequency:2
10. voltage:100
11. copy create a CPU!
12. cpu c's parameter
13. rank:6
14. frequency:3
15. voltage:300
16. destruct a CPU!
17. destruct a CPU!
18. destruct a CPU!
1. #include<iostream>
2. using namespace std;
3. class CPU
4. {
5. int frequency;
6. float voltage;
7. int rank;
8. public:
9. CPU()
10.   {
11.     rank=1;
12.     frequency=2;
13.     voltage=100;
14.     cout<<"create a CPU!"<<endl;
15.   }
16. CPU(int r,int f,float v)
17.   {
18.     rank=r;
19.     frequency=f;
20.     voltage=v;
21.     cout<<"create a CPU!"<<endl;
22.   }
23. CPU(CPU &a)
24.   {
25.     rank=a.rank;
26.     frequency=a.frequency;
27.     voltage=a.voltage;
28.     cout<<"copy create a CPU!"<<endl;
29.   }
30.   ~CPU()
31.   {
32.     cout<<"destruct a CPU!"<<endl;
33.   }
34. void showinfo()
35.   {
36.     cout<<"rank:"<<rank<<endl;
37.     cout<<"frequency:"<<frequency<<endl;
38.     cout<<"voltage:"<<voltage<<endl;
39.   }
40. };
41. #define PI  1
42. #define P2  2
43. #define P3  3
44. #define P4  4
45. #define P5  5
46. #define P6  6

 

6-2 创建计算机

分数 10

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,定义并实现这个类,为以上的类编写构造和析构函数,注意使用类组合的思想解决该问题,使得给出的主函数代码可以正确运行并得到给出的输出结果。

函数接口定义:

根据要求写出类的实现代码

裁判测试程序样例:

/* 请在这里填写答案 */ 在这里给出函数被调用进行测试的例子。例如: int main() { COMPUTER cpt1(6,4.0,200,60,32); //测试带参数构造 cout<<"cpt1's parameter:"<<endl; cpt1.showinfo(); cout<<"------------------------------"<<endl; COMPUTER cpt2; //测试不带参数构造 cout<<"cpt2's parameter:"<<endl; cpt2.showinfo(); cout<<"------------------------------"<<endl; COMPUTER cpt3(cpt1); //测试复制构造 cout<<"cpt3's parameter:"<<endl; cpt3.showinfo(); cout<<"------------------------------"<<endl; }

输入样例:

输出样例:

在这里给出相应的输出。例如:

1. create a CPU!
2. create a RAM!
3. create a CDROM!
4. create a COMPUTER with para!
5. cpt1's parameter:
6. cpu parameter:
7. rank:6
8. frequency:4
9. voltage:200
10. ram parameter:
11. volumn:60 GB
12. cdrom parameter:
13. speed:32
14. ------------------------------
15. create a CPU!
16. create a RAM!
17. create a CDROM!
18. no para to create a COMPUTER!
19. cpt2's parameter:
20. cpu parameter:
21. rank:1
22. frequency:2
23. voltage:100
24. ram parameter:
25. volumn:1 GB
26. cdrom parameter:
27. speed:16
28. ------------------------------
29. create a CPU by copy!
30. create a RAM by copy!
31. create a CDROM by copy!
32. create a COMPUTER by copy!
33. cpt3's parameter:
34. cpu parameter:
35. rank:6
36. frequency:4
37. voltage:200
38. ram parameter:
39. volumn:60 GB
40. cdrom parameter:
41. speed:32
42. ------------------------------
43. destruct a COMPUTER!
44. destruct a CDROM!
45. desturct a RAM!
46. desturct a CPU!
47. destruct a COMPUTER!
48. destruct a CDROM!
49. desturct a RAM!
50. desturct a CPU!
51. destruct a COMPUTER!
52. destruct a CDROM!
53. desturct a RAM!
54. desturct a CPU!
1. #include<bits/stdc++.h>
2. using namespace std;
3. class CPU
4. {
5.    int rank;
6.    double frequency;
7.    int voltage;
8.  public:
9.    CPU(int R,double F,int V)
10.         {
11.             rank=R;
12.             frequency=F;
13.             voltage=V;
14.       cout<<"create a CPU!"<<endl;
15.     }
16.     CPU(CPU &cpu)
17.         {
18.             rank=cpu.rank;
19.             frequency=cpu.frequency;
20.             voltage=cpu.voltage;
21.       cout<<"create a CPU by copy!"<<endl;
22.     }
23.     void showinfo()
24.         {
25.       cout<<"cpu parameter:"<<endl;
26.       cout<<"rank:"<<rank<<endl;
27.       cout<<"frequency:"<<frequency<<endl;
28.       cout<<"voltage:"<<voltage<<endl;
29.     }
30.     ~CPU()
31.         {
32.       cout<<"desturct a CPU!"<<endl;
33.     }
34. };
35. class RAM
36. {
37.     int volumn;
38.   public:
39.     RAM(int V)
40.         {
41.             volumn=V;
42.       cout<<"create a RAM!"<<endl;
43.     }
44.     RAM(RAM &ram)
45.         {
46.             volumn=ram.volumn;
47.       cout<<"create a RAM by copy!"<<endl;
48.     }
49.     void showinfo()
50.         {
51.       cout<<"ram parameter:"<<endl;
52.       cout<<"volumn:"<<volumn<<" GB"<<endl;
53.     }
54.     ~RAM()
55.         {
56.       cout<<"desturct a RAM!"<<endl;
57.     }
58. };
59. class CDROM
60. {
61.     int speed;
62.   public:
63.     CDROM(int S)
64.         {
65.             speed=S;
66.       cout<<"create a CDROM!"<<endl;
67.     }
68.     CDROM(CDROM &cdrom)
69.         {
70.             speed=cdrom.speed;
71.       cout<<"create a CDROM by copy!"<<endl;
72.     }
73.     void showinfo()
74.         {
75.       cout<<"cdrom parameter:"<<endl;
76.       cout<<"speed:"<<speed<<endl;
77.     }
78.     ~CDROM()
79.         {
80.       cout<<"destruct a CDROM!"<<endl;
81.     }
82. };
83. class COMPUTER
84. {
85.     CPU cpu;
86.     RAM ram;
87.     CDROM cdrom;
88.   public:
89.     COMPUTER():cpu(1,2,100),ram(1),cdrom(16)
90.         {
91.       cout<<"no para to create a COMPUTER!"<<endl;
92.     }
93.     COMPUTER(int rank,double frequency,int voltage,int volumn,int speed):cpu(rank,frequency,voltage),ram(volumn),cdrom(speed)
94.         {
95.       cout<<"create a COMPUTER with para!"<<endl;
96.     }
97.     COMPUTER(COMPUTER &computer):cpu(computer.cpu),ram(computer.ram),cdrom(computer.cdrom)
98.         {
99.       cout<<"create a COMPUTER by copy!"<<endl;
100.    }
101.    void showinfo()
102.         {
103.      cpu.showinfo();
104.      ram.showinfo();
105.      cdrom.showinfo();
106.    }
107.    ~COMPUTER()
108.         {
109.      cout<<"destruct a COMPUTER!"<<endl;
110.    }
111. };

 

6-3 复数类

分数 20

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

  1. 已知一个名为Complex的复数类,这个类包含:
    (1)私有成员:实部、虚部,且均为int 型
    (2)公有的带默认形参值的构造函数、复制构造函数
    (3)公有成员函数Display,其作用为显示复数
    要求:
    (1)实现满足上述属性和行为的Complex类定义;
    (2)设计函数AddComplex,函数AddComplex功能为实现两个复数相加,要求该函数的形参为复数类的常引用;
    (3)保证如下主函数能正确运行。

裁判测试程序样例:

/* 请在这里填写答案 */ int main(){ int x,y; cin>>x>>y; Complex c0(x,y); Complex c1(c0); cout<<"c1 is: "; c1.Display(); cin>>x>>y; Complex c2(x,y); cout<<"c2 is: "; c2.Display(); Complex c3; c3 = AddComplex(c1,c2); cout<<"c3 is: "; c3.Display(); return 0; }

输入样例:

在这里给出一组输入。例如:

1. 2 -3
2. 3 4

输出样例:

在这里给出相应的输出。例如:

1. c1 is: 2-3i
2. c2 is: 3+4i
3. c3 is: 5+1i
1. #include<bits/stdc++.h>
2. using namespace std;
3. class Complex
4. {
5. public:
6. int real;
7. int imag;
8. Complex(int r,int i)
9.     {
10.         real=r;
11.         imag=i;
12.     }
13. Complex(const Complex &c)
14.     {
15.         real=c.real;
16.         imag=c.imag;
17.     }
18. void Display()
19.     {
20. if(imag>0)
21.         {
22.             cout<<real<<"+"<<imag<<"i"<<endl;
23.         }
24. else
25.         {
26.             cout<<real<<imag<<"i"<<endl;
27.         }
28.     }
29. Complex(const Complex& c1,const Complex& c2)
30.     {
31.         real=c1.real+c2.real;
32.         imag=c1.imag+c2.imag;
33.     }
34. Complex()
35.     {
36.         real;
37.         imag;
38.     }
39. };
40. Complex AddComplex(Complex c1,Complex c2)
41. {
42.     Complex c;
43.     c.real=c1.real+c2.real;
44.     c.imag=c1.imag+c2.imag;
45. return c;
46. }

 

6-4 定义Date类

分数 10

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

本题要求实现一个日期类定义,根据所定义的类可以完成相关的类测试。

Date类定义:

根据Date被使用的情况,进行Date类定义,要求通过构造函数进行日期初始化,并通过display()函数进行日期格式显示,显示格式为"月/日/年"

测试程序样例:

main( ) 函数定义如下

int main() { Date d1(3,25,2019); Date d2(3,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; } /* 请在这里填写答案 */

输出样例:

在这里给出相应的输出。例如:

1. 3/25/2019
2. 3/30/2019
3. 10/1/2019
4. 1/1/2019
1. #include<bits/stdc++.h>
2. using namespace std;
3. class Date
4. {
5. private:
6. int year;
7. int month;
8. int day;
9. public:
10. Date(int d=1,int m=1,int y=2019)
11.     {
12.         day=d;
13.         month=m;
14.         year=y;
15.     };
16. void display()
17.     {
18.         cout<<day<<"/"<<month<<"/"<<year<<endl;
19.     }
20. };

 

6-5 学生类的构造与析构

分数 10

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

类定义:

定义一个学生类Student,使得main()函数能够得到指定的输出结果

main()函数如下:

/* 请在这里填写答案 */ int main() {Student stud1(10010,"Wang_li",'f'); stud1.display(); Student stud2(10011,"Zhang_fun",'m'); stud2.display(); return 0; }

输入样例:

输出样例:

在这里给出相应的输出。例如:

1. Constructor called.
2. num:10010
3. name:Wang_li
4. sex:f
5. 
6. Constructor called.
7. num:10011
8. name:Zhang_fun
9. sex:m
10. 
11. Destructor called.
12. Destructor called.
1. #include<bits/stdc++.h>
2. using namespace std;
3. class Student
4. {
5. private:
6. int number;
7.     string name;
8. char sex;
9. public:
10. Student(int num,string n,char s)
11.     {
12.         cout<<"Constructor called."<<endl;
13.         number=num;
14.         name=n;
15.         sex=s;
16.     }
17. void display()
18.     {
19.         cout<<"num:"<<number<<endl;
20.         cout<<"name:"<<name<<endl;
21.         cout<<"sex:"<<sex<<endl;
22.         cout<<endl;
23.     }
24.     ~Student()
25.     {
26.         cout<<"Destructor called."<<endl;
27.     }
28. };



相关文章
|
8天前
|
编译器 C++
CPP的常量引用
CPP的常量引用
12 0
|
10月前
|
机器学习/深度学习 人工智能 网络协议
CPP2022-23-函数进阶-函数指针
CPP2022-23-函数进阶-函数指针
186 0
|
8天前
|
存储 C++
CPP的类和对象
CPP的类和对象
16 1
|
8天前
|
C++
CPP的继承和多态
CPP的继承和多态
17 0
|
8天前
|
编译器 C++
CPP的函数重载
CPP的函数重载
27 0
|
8天前
|
C++
win32编程 -- 动态库中声明类
win32编程 -- 动态库中声明类
13 0
|
8天前
|
存储 编译器 程序员
【新手解答2】深入探索 C 语言:变量名、变量 + 函数声明 vs 函数定义 + main的声明 + 头文件和源文件的关系
【新手解答2】深入探索 C 语言:变量名、变量 + 函数声明 vs 函数定义 + main的声明 + 头文件和源文件的关系
59 0
|
10月前
|
10月前
CPP2022-26-多态专题(上)
CPP2022-26-多态专题
32 0
|
10月前
|
测试技术
CPP2022-26-多态专题(下)
CPP2022-26-多态专题(下)
81 0

热门文章

最新文章