1
一、按下列要求,定义类MyClass 中的数据成员和成员函数:
1)具有两个私有的数据成员x,y;
2) 具有无参的构造函数,将数据成员初始化为零,并输出表示信息:"Default Constructor!";
3) 具有带两个参数的构造函数,初始化数据成员;
4)为该类定义友元函数Display,该函数计算数据成员x和y的乘积;
5)定义该类的派生类YourClass,新增数据成员z,成员函数显示YourClass类中的所有数据成员的和。
成员编写完成后,将本题答案文件保存为1.cpp,然后通过考试系统上传答案文件,若后续修改了答案可以再次提交,会覆盖原有文件。
#include using namespace std;class MyClass{private: int x, y;public: MyClass(){ x = 0; y = 0; cout << "Default Constructor" << endl; } MyClass(int a, int b){ x = a; y = b; } int getX(){return x;} int getY(){return y;} friend void Display(MyClass&);};class YourClass: public MyClass{private: int z;public: YourClass(int a, int b, int c): MyClass(a, b){ z = c; } void Display(){ cout << getX() + getY() + z << endl; }};void Display(MyClass& my){ cout << my.x * my.y << endl;}void main(){ //测试无参数构造方法 MyClass my; Display(my); //测试友元函数输出 MyClass my1(3, 6); Display(my1); //测试成员函数输出 YourClass you(2, 4, 5); you.Display();}
2
二、设计函数CoverFun,要求:将用户输入的字符串中的大写字母都改为对应的小写字母,其他字符不变;同时设计主函数测试该函数。
例如:若输入“this23ThiS”,则输出“this23this”。
程序编写完后,将本题答案文件保存为2.cpp,通过考试系统上传答案文件,若后续修改答案可再次提交,会覆盖原有文件。
#include #include using namespace std;//ASCII码中同字母大小写编码差值:a=97,A=65const int DIFF = 'a' - 'A';//题目的convert单词写错了,定义应该按题目的函数名定义//此题参数建议穿字符串的值,而不是引用//string类型这个关键字是小写stringstring CovertFun(string str){ int size = str.length(); for(int i=0; i //控制变更范围 if(str[i] > 'A' && str[i] < 'Z'){ str[i] = str[i] + DIFF; } } return str;}void main(){ string input; cout << "请输入需要转换的内容:" << endl; cin >> input; string output = CovertFun(input); cout << "转换后的内容:" << output << endl;}
《C++ 程序设计》上级考试B卷 2018.11
#include using namespace std;const double PI = 3.1415926;class Point{private: int a, b;public: Point(int x=10, int y=20){ a = x; b = y; } ~Point(){ cout << "Destructor!" << endl; } friend void show(Point&);};class Circle: public Point{private: int r;public: Circle(int x, int y, int z): Point(x, y){ r = z; } void Area(){ cout << "The area is " << PI*r*r << endl; }};void show(Point& p){ cout << "The value of a is " << p.a << ", The value of b is " << p.b << endl;}void main1(){ Circle c1(8, 5, 10); c1.Area(); //多余的测试代码 Point& p = c1; show(p);}
#include #include using namespace std;//交大老师写错单词,应该是MulDatavoid MulDate(int a[], int b[], int length){ for(int i=0; i b[i] = b[i] * a[i]; }}void main(){ int a[]={3, 4, 5, 6}; int b[]={1, 2, 3, 4}; cout << "原始:a[]="; copy(a, a+4, ostream_iterator<int>(cout, " ")); cout << endl << "原始:b[]="; copy(b, b+4, ostream_iterator<int>(cout, " ")); MulDate(a, b, 4); cout << endl << "转换:b[]="; copy(b, b+4, ostream_iterator<int>(cout, " "));}
《C++ 程序设计》上级试验 2017.10
1
第一题:(文件存为1.cpp)
定义类满足以下条件:
(1)类Point:
有私有属性a, b
有带2个参数的构造方法
有友元函数show 功能是可以显示 value of a is+ a的值, value of b is +b的值
(2)类Circle:
继承Point类
自己的属性r
自己的求面积方法
#include using namespace std;const double PI = 3.1415926;class Point{private: double a, b;public: Point(double a, double b){ // 如果不使用this指针,则需将形参设置为其他名字, this->a = a; this->b = b; } friend void show(Point&);};void show(Point& p){ cout << "value of a is " << p.a << ", value of b is " << p.b << endl;}class Circle: public Point{private: double r;public: Circle(double a, double b, double r): Point(a, b){this->r = r;} double area(){ return PI * r * r;}};void main(){ Circle c(2.5, 1.4, 5); show(c); double area = c.area(); cout << "the area is " << area << endl;}
2
第二题:(文件存为2.cpp)
数组 int a[] = {3,4,5,6,7};
数组 int b[] = {1,2,3,4,5,6,7};
函数 v1 ,传入数组a,b 让数组b的值为:
b[i] = b[i] * a[i]
最后输出b为:b[]={3,8,15,24,35,6,7}
#include #include using namespace std;//数组传递的是首地址,丢失了长度信息//必须在数组定义的地方将长度信息传递到函数void v1(int a[], int b[], int size){ for(int i=0; i b[i] = b[i] * a[i]; }}void main(){ int a[] = {3, 4, 5, 6, 7}; int b[] = {1, 2, 3, 4, 5, 6, 7}; v1(a, b, sizeof(a)/sizeof(int)); int size = sizeof(b)/sizeof(int); cout << "b[]={"; //copy方法输出数组的最后一个元素后多了一个逗号 //copy(b, b+size, ostream_iterator(cout, ", ")); for(int i=0; i cout << b[i]; if(i != size-1){ cout << ", "; } } cout << "}" << endl;}