如何建立类cylinder,cylinder的构造函数
的有关信息介绍如下:
建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并储存在一个double变量中。在类cylinder中包含vol,用来显示每个cylinder对象的体积。
#include
using namespace std;
class cylinder{
public:
cylinder(double r,double h)
{
radius=r;high=h;
volume=3.14*radius*radius*high;
cout<<"the volume is:"< } private: double volume,radius,high; }; int main() { int x,y; cout<<"请输入圆柱的半径:"; cin>>x; cout<<"请输入圆柱的高:"; cin>>y; cylinder A(x,y); return 0; } 构建一个类book,其中含有两个私有数据成员qu和price,建立一个有5个元素的数组对象,将初始化为1~5,将price初始化为qu的10倍1.显示每个对象的qu*price。 #include using namespace std; class book{ public: book(int n) { qu=n;price=10*n; cout<<"qu*price="< } private: int qu,price; }; int main() { book array={1,2,3,4,5}; return 0; } 修改3.33,通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的qu*price值 #include using namespace std; class book{ public: book(int n) { qu=n;price=10*n; } int show() { return qu*price; } private: int qu,price; }; int main() { int i; book array={1,2,3,4,5}; book *parray; parray=&array; for(i=4;i>=0;i--) { cout<<"qu*price="< } return 0; } 构建一个类Stock,含字符数组stockcode[]及整型数组成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为8.98.成员函数print没有形参,需使用this指针,显示对象数据成员的内容。编写程序显示对象数据成员的值。 #include #include using namespace std; class Stock{ public: Stock(string a,int b=1000, double c=8.98); void printf(void) { cout<<"stockcode:"< } private: string stockcode; int qu; double price; }; Stock::Stock(string a,int b, double c) { stockcode=a; qu=b; price=c; } int main() { Stock a("600001",3000,5.67),b("600001"); a.printf(); b.printf(); return 0; } 编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示) #include #include using namespace std; /************************成绩类***************/// class Score{ public: Score(float c,float e,float m); void show(); private: float Computer,English,Mathematics; }; Score::Score(float c,float e,float m) { Computer=c; English=e; Mathematics=m; } void Score::show() { cout<<" 计算机成绩"< } /**********************学生类******************/ class Student{ private: string name; string number; static int count; static float ave; static float sum; Score score1; public: Student(string name1,string number1,float s1,float s2,float s3);//构造函数 void show(); }; Student::Student(string name1,string number1,float s1,float s2,float s3):score1(s1,s2,s3)// { name=name1; ++count; sum=sum+s1+s2+s3; ave=sum/count/3; number=number1; } void Student::show() { cout<<"\n姓名:"< cout<<"\n学号:"< score1.show(); cout<<"\n学生人数:"< cout<<" 平均成绩:"< } int Student::count=0; float Student::sum=0; float Student::ave=0; int main() { Student stu1("张三","161011020",90,85,78); stu1.show(); cout< Student stu2("李四","161011021",90,98,98); stu2.show(); cout< return 0; }



![论文引用怎么标注[1]](/upload/jingyan/2024/0807/71e33bcb.jpg)