音响网站模板,网页加速器免费永久,网站绑定公众号,深圳石岩建网站#x1f383;个人专栏#xff1a; #x1f42c; 算法设计与分析#xff1a;算法设计与分析_IT闫的博客-CSDN博客 #x1f433;Java基础#xff1a;Java基础_IT闫的博客-CSDN博客 #x1f40b;c语言#xff1a;c语言_IT闫的博客-CSDN博客 #x1f41f;MySQL#xff1a… 个人专栏 算法设计与分析算法设计与分析_IT闫的博客-CSDN博客 Java基础Java基础_IT闫的博客-CSDN博客 c语言c语言_IT闫的博客-CSDN博客 MySQL数据结构_IT闫的博客-CSDN博客 数据结构数据结构_IT闫的博客-CSDN博客 CC_IT闫的博客-CSDN博客 C51单片机C51单片机STC89C516_IT闫的博客-CSDN博客 基于HTML5的网页设计及应用基于HTML5的网页设计及应用_IT闫的博客-CSDN博客 pythonpython_IT闫的博客-CSDN博客 离散数学离散数学_IT闫的博客-CSDN博客 欢迎收看希望对大家有用 问题一 给出下面的抽象基类container要求建立2个继承container的派生类Sphere与Cylinder,每一个派生类都包含虚函数surface_area()和 volume(),分别用来计算球体和圆柱体的表面积及体积。写出主程序应用C的多态性分别计算半径为5.0的球体以及半径为5.0和高为6.0的圆柱体的表面积和体积。问题一 问题二 已知一个有若干元素的数组arr使用函数模板求该数组的的最大值主函数定义数组使用模板函数求最大值并输出。 问题三 编写一个类模板对数组元素进行遍历输出、数组求和等主函数创建数组类对象调用函数遍历输出及求和。 答案一
#include iostreamusing namespace std;class Container { //声明抽象类containerprotected:double radius;public:Container(double _radius) { //抽象类container的构造函数radius_radius;}virtual double surface_area()0; //纯虚函数surface_areavirtual double volume()0; //纯虚函数volume};/****************************************************///派生类sphere pi3.14159 s4*pi*r*r v4/3*pi*r*r*rclass Sphere:public Container {public:Sphere(double _radius);double surface_area();double volume();};Sphere::Sphere(double _radius):Container(_radius) {}double Sphere::surface_area() {//s4*pi*r*rreturn 4*3.14159*radius*radius;}double Sphere::volume() {//v4/3*pi*r*r*rreturn 4.0/3*3.14159*radius*radius*radius;}/****************************************************///派生类Cylinderclass Cylinder:public Container {public:Cylinder(double _radius,double _height);double surface_area();double volume();private:double height;};Cylinder::Cylinder(double _radius,double _height):Container(_radius) {height_height;}double Cylinder::surface_area() {return 2*3.14159*radius*height2*3.14159*radius*radius;}double Cylinder::volume() {return 3.14159*radius*radius*height;;}int main() {Sphere sphere(2);cout球体表面积:sphere.surface_area()endl;cout球体体积:sphere.volume()endl;Cylinder Cylinder(2,3);cout圆柱体表面积:Cylinder.surface_area()endl;cout圆柱体体积:Cylinder.volume()endl;}
答案二
#include iostreamusing namespace std;/****************************************************///函数模板max_arr template class TT max_arr(T arr[],int n) {T maxarr[0];for(int i1; in; i)if(maxarr[i])maxarr[i];return max;}/****************************************************/int main() {int a[5],k;for(k0;k5;k)cina[k];coutmaxmax_arrint(a,5)endl;}
答案三
#include iostreamusing namespace std;templateclass T //类模板class Array {private:int _size;T* _ptr;public:Array(T arr[], int s);void show();T sum();};//构造函数templateclass TArrayT::Array(T arr[], int s) {_ptr new T[s];_size s;for (int i0; i_size; i) {_ptr[i]arr[i];}}//遍历输出函数templateclass Tvoid ArrayT::show() {for (int i0; i_size; i)cout*(_ptr i) ;coutendl;}/****************************************************///求和函数templateclass TT ArrayT::sum() {T s0;for (int i0; i_size; i)ss*(_ptr i);return s;}/****************************************************/int main() {int iArr[6] { 2, 1, 3, 5, 4, 6 };Arrayint a1(iArr, 6);a1.show();coutsuma1.sum()endl;return 0;}