面向对象程序设计 实验二 C++基础练习

3.22 建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并储存在一个double变量中。在类cylinder中包含vol,用来显示每个cylinder对象的体 。

#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;
 }

3.23 构建一个类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&#
  • 7
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值