如何建立类cylinder,cylinder的构造函数

 时间:2024-10-14 18:59:41

1、#include<iostream>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:"<<volume<<endl; }private:double volume,radius,high;};int main(){int x,y; cout<<"请输入圆柱的半径:"; cin>>x; cout<<"请输入圆柱的高:"; cin>>y; cylinder A(x,y); return 0;}

2、构建一个类book,其中含有两个私有数据成员qu和price,建立一个有5个元素的数组对象,将初始化为1~5,将price初始化为qu的10倍1.显示每个对象的qu*price。#include<iostream>using namespace std;class book{public: book(int n) { qu=n;price=10*n;cout<<"qu*price="<<qu*price<<"\n"; }private:int qu,price;};int main(){book array[5]={1,2,3,4,5};return 0;}

3、修改3.33,通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的qu*price值#include<iostream>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[5]={1,2,3,4,5};book *parray;parray=&array[0];for(i=4;i>=0;i--){cout<<"qu*price="<<parray[i].show()<<"\n";}return 0;}

4、构建一个类Stock,含字符数组stockcode[]及整型数组成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为8.98.成员函数print没有形参,需使用this指针,显示对象数据成员的内容。编写程序显示对象数据成员的值。#include<iostream>#include<string>using namespace std;class Stock{public: Stock(string a,int b=1000, double c=8.98); void printf(void) { cout<<"stockcode:"<<stockcode<<" qu:"<<qu<<" price:"<<price<<"\n"; }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;}

5、编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据敛财醣沁并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)#include<i泠贾高框ostream>#include<string>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<<" 计算机成绩"<<Computer<<" 英语成绩"<<English<<" 数学成绩"<<Mathematics<<"\n";}/**********************学生类******************/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姓名:"<<name;cout<<"\n学号:"<<number;score1.show();cout<<"\n学生人数:"<<count;cout<<" 平均成绩:"<<ave;}int Student::count=0;float Student::sum=0;float Student::ave=0;int main(){ Student stu1("张三","161011020",90,85,78);stu1.show();cout<<endl;Student stu2("李四","161011021",90,98,98);stu2.show();cout<<endl;return 0;}

  • C#如何打开模式对话框窗体
  • 边坡固土网垫植草防护施工控制要点有哪些?
  • 电脑自带的计算器怎么使用温度转换
  • proe装配入门基础:[3]匹配关系
  • 生源地助学贷款第二年该怎么办理呢?
  • 热门搜索
    小学保护环境手抄报 建军节手抄报内容20字 五一英语手抄报 手抄报花边又漂亮 春的手抄报 关于校园安全的手抄报 好读书读好书手抄报 小手牵大手手抄报 节约用水手抄报大全 我的家庭手抄报