(3) Cylinder c3(c1);//用一个已有的圆柱体构造一个新的圆柱体 在主函数中进行测试。
Input
输入数据有多行,每行有两个数据,代表圆柱体的底面半径和高。
Output
输出有多行,每行有两个数据,代表圆柱体的体积和表面积,最后两行分别表示调用默认构造函数和拷贝构造函数的圆柱体对象的体积和表面积。
Sample Input
20 100 4 5 2 7
Sample Output
125600 15072 251.2 226.08 87.92 113.04 3140 1256 3140 1256
#include
private: float h,r; public: Cylinder() { r=10;h=10; } Cylinder(float r1,float h1) { r=r1;h=h1; } float VV() { float v; v=3.14*r*r*h;
return v; } float SS() { float s; s=2*3.14*r*h+2*3.14*r*r; return s; } void show() { cout< int main() { Cylinder c2(20,100); Cylinder c1; Cylinder c3(c1); float h2,r2; while(cin>>r2>>h2) { Cylinder c2(r2,h2); c2.show(); } c1.show();c3.show(); return 0; } 15:构造函数、拷贝构造和析构函数——定义 学生类 Time/Memory Limit:1000 MS/32768 K Submitted: 57 Accepted: 40 Problem Description 定义学生类,该类包含的数据成员有:学生编号,姓名,年龄,成绩;成员函数有:构造函数,拷贝构造函数,析构函数,输出学生信息函数。其中构造函数要输出\提示;拷贝构造函数要输出\提示;析构函数要输出\提示。在主函数中进行测试:主函数中先定义一个学生对象s1,数据从键盘中输入,再定义一个对象s2,用对象s1对s2进行初始化。 Input 输入数据只有一行,分别代表学生的编号,姓名,年龄,成绩。 Output 输出两个学生对象的信息,具体如下所示。 Sample Input 10 wang 19 88 Sample Output Constructing... Copy Constructing... Id:10 Name:wang Age:19 Score:88 Id:10 Name:wang Age:19 Score:88 Destructing... Destructing... #include private: int num,age,score; char *name; public: student(int n1=0,char *na=\ { num=n1; name=new char[strlen(na)+1]; strcpy(name,na); age=a1;score=s1; cout<<\ } student(student &p) { num=p.num; name=new char[strlen(p.name)+1]; strcpy(name,p.name);age=p.age;score=p.score; cout<<\ } ~student() { delete []name; cout<<\ } void show() { cout<<\ } }; int main() { int n,a,s; char na[20]; cin>>n>>na>>a>>s; student s1(n,na,a,s); student s2(s1); s1.show(); s2.show(); return 0; } 16:统计学生人数和成绩 Time/Memory Limit:1000 MS/32768 K Submitted: 57 Accepted: 47 Problem Description 定义一个Student类记录学生的学号和C++课程的成绩。要求使用静态成员变量和静态成员函数计算全班学生C++课程的总成绩和平均成绩。 Input 输入每个学生的学号和成绩,每个学生的信息占一行,直到文件结束。 Output 输出包括两行,第一行全班人数和总成绩,用空格隔开; 第二行平均成绩。 Sample Input 101 30 102 50 103 90 104 60 105 70 Sample Output 5 300 60 #include private: double score; string num; static int count; static double sum; static double ave; public: student(string num,double score) { this->num=num;this->score=score; count++; sum=sum+score; ave=double(sum/count); } static void show() { cout<