CPP_第14周
A.元素查找(函数模板) 题目描述 编写一个在数组中进行查找的函数模板,其中数组为具有_n_个元素,类型为T,要查找的元素为key。 注意:必须使用模板函数 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#include<iostream>using namespace std;template<class Type>void search(Type *a,Type key,int n){ for (int i = 0; i < n; i++) { if (a[i] == key) { cout << i + 1 << endl; return ; } } cout << '0' <<...
CPP_第13周
A.三维坐标点的平移(运算符重载) 题目描述 定义一个三维点Point类,利用友元函数重载"++“和”–"运算符,并区分这两种运算符的前置和后置运算。 1234567891011121314151617class point{ int x; int y; int z;public: point(int X=0,int Y=0,int Z=0); {x=X;y=Y;z=Z;} friend point operator ++(point &a); friend point operator ++(point &,int); friend point operator --(point &); friend point operator --(point &,int); void show() { cout<<"x="<<x<<'...
CPP_第12周
A.分数的加减乘除(运算符重载) 题目描述 Fraction类的基本形式如下: 1234567891011121314151617// 定义Fraction类class Fraction{private: int fz, fm; int commonDivisor(); // 计算最大公约数 void contracted(); // 分数化简public: Fraction(int = 0, int = 1); Fraction(Fraction&); Fraction operator+(Fraction); Fraction operator-(Fraction); Fraction operator*(Fraction); Fraction operator/(Fraction); void set(int = 0, int = 1); void...
CPP_第11周
A.在职研究生(多重继承) 题目描述 1、建立如下的类继承结构: 1) 定义一个人员类CPeople,其属性(保护类型)有:姓名、性别、年龄; 2) 从CPeople类派生出学生类CStudent,添加属性:学号和入学成绩; 3) 从CPeople类再派生出教师类CTeacher,添加属性:职务、部门; 4) ...
CPP_第10周
A.圆和圆柱体计算(继承) 题目描述 定义一个CPoint点类,包含数据成员x,y(坐标点)。 以CPoint为基类,派生出一个圆形类CCircle,增加数据成员r(半径)和一个计算圆面积的成员函数。 再以CCircle做为直接基类,派生出一个圆柱体类CCylinder,增加数据成员h(高)和一个计算体积的成员函数。 生成圆和圆柱体对象,调用成员函数计算面积或体积并输出结果。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960#include<iostream>#include<cstring>#include<cmath>using namespace std;class CPoint{protected: double x, y;public: CPoint(double x1, double...
CPP_第9周
...
CPP_第8周
A.距离计算(友元函数) 题目描述 Point类的基本形式如下: 1234567class Point{private: double x, y;public: Point(double xx, double yy); // 构造函数}; 请完成如下要求: 1.实现Point类; 2.为Point类增加一个友元函数double Distance(Point &a, Point &b),用于计算两点之间的距离。直接访问Point对象的私有数据进行计算。 3.编写main函数,输入两点坐标值,计算两点之间的距离。 1234567891011121314151617181920212223242526272829303132333435363738394041424344#include<iostream>#include<cmath>using namespace std;class Point{private: double x, y;public: Point(double x1, double...
CPP_第7周
A.Equation(类与对象+构造) 题目描述 建立一个类Equation,表达方程ax2+bx+c=0。类中至少包含以下方法: 1、无参构造(abc默认值为1.0、1.0、0)与有参构造函数,用于初始化a、b、c的值; 2、set方法,用于修改a、b、c的值 3、getRoot方法,求出方程的根。 一元二次方程的求根公式如下: x=−b±b2−4ac2ax ={-b \pm \sqrt{b^2-4ac}\over...
CPP_第6周
A.Point(类与构造) 题目描述 下面是一个平面上的点的类定义,请在类外实现它的所有方法,并生成点测试它。 123456789101112class Point{ double x,y;public: Point(); //缺省构造函数,给x,y分别赋值为0 Point(double x_value,double y_value);//有参构造函数,给x,y赋参数的值 double getX();//返回x的值 double getY();//返回y的值 void setX(double x_value);//设置x的值 void setY(double y_value);//设置y的值 double distanceToAnotherPoint(Point...
CPP_第5周
A. 对象数组(类和对象) 题目描述 课堂上我们谈到类这个概念,比如第一题我们有学生类这个抽象的概念,成千上万个学生都具有同样的属性,但针对某个具体学生来说,他/她具有自己的鲜明个性,比如计算机专业的王海同学,信息工程学院的李慧同学,那么我们是定义一个该类的变量:Student student; 假设该类包含姓名、学号、性别、所属学院、联系电话等;在程序运行过程,把变量student赋予不同值就可以让它表示是王海还是李慧,尝试定义一个学生数组(比如四个元素大小,请思考此时四个对象是如何初始化的呢?),然后输入四个不同学生各项属性给数组里学生初始化(最好定义一个输入函数),然后输出该学生对象数组的各对象的姓名。 123456789101112131415161718192021222324252627282930313233343536373839#include<iostream>#include<string>using namespace std;class Student {public: void Set(); void...