定义Box(盒子)类,包括:长、宽、高及盒子体积的计算函数。要求在main函数中,创建盒子,计算盒子的体积。
#include <iostream>
using namespace std;
class Box
{
private:
double length,wide,high;
public:
void v()
{
cout<<"The result is:"<<length*wide*high<<endl;
}
void setM(double len,double wid,double hig)
{
length = len;
wide = wid;
high = hig;
}
};
int main ()
{
Box box;
double a,b,c;
cout << "请输入盒子的长、宽、高" <<endl;
cin >> a;
cin >> b;
cin >> c;
box.setM(a,b,c);
box.v();
return 0;
}
定义学生类,包括:学号、姓名、英语分数、工数分数、政治分数以及总分和平均分统计函数。在main函数中,计算单个学生的总分及平均分。
#include <iostream>
#include <string>
using namespace std;
class St{private:double nu,e,ma,p;string name;
public:void v(){cout<<"The sum is:"<<e+ma+p<<endl;cout<<"The average is:"<<(e+ma+p)/3<<endl;}
void s(double num,string n,double en,double m,double po){nu = num;name = n;e = en; ma = m;p = po;}
};int main (){St s;double a,b,c,d;string n;cout << "请输入学生学号、姓名、英语分数、工数分数、政治分数" <<endl;cin >> a;
cin >> n;cin >> b;cin >> c; cin >> d; s.s(a,n,b,c,d);s.v();return 0;}
编写 Account 类 1. 包括一个类型为 int 的数据成员,表示账户余额 2. 提供一个构造函数,接收初始余额并用它初始化数据成员 构造函数应确认初始余额的有效性,保证其大于等于0,否则,余额应设置为0,并显示一条错误信息,指示初始余额无效 3. 成员函数credit将一笔金额加到当前余额中 4. 成员函数debit从Account中取钱,并保证取出金额不超过此Account的余额,否则,余额不变,打印一条信息:“Debit amount exceeded account balance.” 5. 成员函数getBalance返回当前余额 6. 编写一个测试程序,创建两个Account对象,测试该类的成员函数
#include <iostream>
using namespace std;
class Account
{
private:
int ba;
public:
Account(int b){
if(b>=0){
ba=b;
}else{
ba=0;
cout<<"false"<<endl;
}
}
void credit(int a){
ba+=a;
}
void debit(int d){
if(d>ba){
cout<<"Debit amount exceeded account balance."<<endl;
}else{
ba-=d;
}
}
int getBalance(){
return ba;
}
};
int main ()
{
Account x1(1);
Account x2(2);
x1.credit(2);
x1.debit(3);
cout<<x1.getBalance();
return 0;
}s/