您的位置:首页 > 编程语言 > C语言/C++

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

2019-04-12 12:40 288 查看

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

[code]#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。

[code]#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.34修改3.33,通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的qu*price值。

[code]#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;
}

3.35 构建一个类Stock,含字符数组stockcode[]及整型数组成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为8.98.成员函数print没有形参,需使用this指针,显示对象数据成员的内容。编写程序显示对象数据成员的值。

[code]#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;
}

3.36 写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。

[code]#include<iostream>
#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;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: