程序设计实践_C++程序设计实践

b1f4b4cedfc130d52c4ad68953752260.png《C++ 程序设计》上级考试A卷 2018.11

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

15b4a021318b5522f3906f2e1388e8d5.png

#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;}
为编写实际的应用程序做好准备:无论你是为了进行软件开发还是进行其他领域的工作。《C++程序设计原理与实践(英文版)》假定你的最终目标是学会编写实际有用的程序。以基本概念和基本技术为重点:与传统的C++教材相比,《C++程序设计原理与实践(英文版)》对基本概念和基本技术的介绍更为深入。这会为你编写有用、正确.易维护和有效的代码打下坚实的基础。, 用现代C++语言编程:, 《C++程序设计原理与实践(英文版)》一方面介绍了通用的程序设计方法包括面向对象程序设计和泛型程序设计)。另一方面还对软件开发实践中使用最广泛的程序设计语言——C++进行了很好的介绍。《C++程序设计原理与实践(英文版)》从开篇就开始介绍现代C++程序设计技术,并介绍了大量关于如何使用C++标准库来简化程序设计的内容。, 适用于初学者以及任何希望学习新知识的人:, 《C++程序设计原理与实践(英文版)》主要是为那些从未编写过程序的人编写的。而且已经由超过1000名大学一年级新生试用过。不过,对于专业人员和高年级学生来说,通过观察公认的程序设计大师如何处理编程中的各种问题。同样也会获得新的领悟和指引。, 提供广阔的视野:, 《C++程序设计原理与实践(英文版)》第一部分非常广泛地介绍了基本程序设计技术,包括基本概念、设计和编程技术、语言特性以及标准库。这些内容教你如何编写具有输入、输出、计算以及简单图形显示等功能的程序。《C++程序设计原理与实践(英文版)》第二部分则介绍了一些更专门性的内容(如文本处理和测试),并提供了大量的参考资料。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值