两道c++编程题,关于类的
1.设计用静态成员实现学生-班费管理的程序:用静态数据成员money存放班费;用函数getmoney()实现学生缴纳班费;用函数spendmoney()实现班费支出;用静...
1.设计用静态成员实现学生-班费管理的程序:
用静态数据成员money存放班费;
用函数getmoney()实现学生缴纳班费;
用函数spendmoney()实现班费支出;
用静态成员函数display()显示余额。
2.定义学生类Student,包括学号、姓名、三门课的成绩、平均成绩。设计一个友元函数grade(),输出按平均成绩对应的等级:
大于等于90为优
80~89为良
70~79为中
60~69为及格
小于60为不及格
不要写得太复杂~
在下是c++菜鸟 展开
用静态数据成员money存放班费;
用函数getmoney()实现学生缴纳班费;
用函数spendmoney()实现班费支出;
用静态成员函数display()显示余额。
2.定义学生类Student,包括学号、姓名、三门课的成绩、平均成绩。设计一个友元函数grade(),输出按平均成绩对应的等级:
大于等于90为优
80~89为良
70~79为中
60~69为及格
小于60为不及格
不要写得太复杂~
在下是c++菜鸟 展开
9个回答
展开全部
第一题:
#include<iostream>
using namespace std;
class Fee
{
public:
void getmoney(int n);
void spendmoney(int n);
static void display();
private:
static int money;
};
int Fee::money=0;
void Fee::getmoney(int n)
{
money=money+n;
}
void Fee::spendmoney(int n)
{
money=money-n;
}
void Fee::display()
{
cout<<money<<endl;
}
int main() //测试程序
{
Fee f;
int n,m;
cout<<"缴纳金额"<<endl;
cin>>n;
f.getmoney(n);
cout<<"花费金额"<<endl;
cin>>m;
f.spendmoney(m);
cout<<"剩余班费"<<endl;
Fee::display(); //或者f.display()
return 0;
}
第二题:
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int,string,int,int, int);
friend void grade(const student&);
private:
int ID;
string Name;
int grade1;
int grade2;
int grade3;
int avr;
};
student::student(int n, string m, int a1, int a2, int a3)
{
ID=n;
Name=m;
grade1=a1;
grade2=a2;
grade3=a3;
avr=(a1+a2+a3)/3;
}
void grade(const student& s)
{
if(s.avr>=90)cout<<"优"<<endl;
else if(s.avr>=80)cout<<"良"<<endl;
else if(s.avr>=70)cout<<"中"<<endl;
else if(s.avr>=60)cout<<"及格"<<endl;
else cout<<"不及格"<<endl;
}
int main() //测试程序
{
int n, a1,a2, a3;
string m;
cout<<"请依次输入学号,姓名,三门成绩"<<endl;
cin>>n>>m>>a1>>a2>>a3;
student s(n,m,a1,a2,a3);
grade(s);
return 0;
}
#include<iostream>
using namespace std;
class Fee
{
public:
void getmoney(int n);
void spendmoney(int n);
static void display();
private:
static int money;
};
int Fee::money=0;
void Fee::getmoney(int n)
{
money=money+n;
}
void Fee::spendmoney(int n)
{
money=money-n;
}
void Fee::display()
{
cout<<money<<endl;
}
int main() //测试程序
{
Fee f;
int n,m;
cout<<"缴纳金额"<<endl;
cin>>n;
f.getmoney(n);
cout<<"花费金额"<<endl;
cin>>m;
f.spendmoney(m);
cout<<"剩余班费"<<endl;
Fee::display(); //或者f.display()
return 0;
}
第二题:
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int,string,int,int, int);
friend void grade(const student&);
private:
int ID;
string Name;
int grade1;
int grade2;
int grade3;
int avr;
};
student::student(int n, string m, int a1, int a2, int a3)
{
ID=n;
Name=m;
grade1=a1;
grade2=a2;
grade3=a3;
avr=(a1+a2+a3)/3;
}
void grade(const student& s)
{
if(s.avr>=90)cout<<"优"<<endl;
else if(s.avr>=80)cout<<"良"<<endl;
else if(s.avr>=70)cout<<"中"<<endl;
else if(s.avr>=60)cout<<"及格"<<endl;
else cout<<"不及格"<<endl;
}
int main() //测试程序
{
int n, a1,a2, a3;
string m;
cout<<"请依次输入学号,姓名,三门成绩"<<endl;
cin>>n>>m>>a1>>a2>>a3;
student s(n,m,a1,a2,a3);
grade(s);
return 0;
}
展开全部
小伏飞刀 回答的很好。
程序满足题目要求,也很简单,正式楼主所要的。
1、
#include<iostream>
using namespace std;
class Fee
{
public:
void getmoney(int n);
void spendmoney(int n);
static void display();
private:
static int money;
};
int Fee::money=0;
void Fee::getmoney(int n)
{
money=money+n;
}
void Fee::spendmoney(int n)
{
money=money-n;
}
void Fee::display()
{
cout<<money<<endl;
}
int main() //测试程序
{
Fee f;
int n,m;
cout<<"缴纳金额"<<endl;
cin>>n;
f.getmoney(n);
cout<<"花费金额"<<endl;
cin>>m;
f.spendmoney(m);
cout<<"剩余班费"<<endl;
Fee::display(); //或者f.display();
return 0;
}
2、
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int,string,int,int, int);
friend void grade(const student&);
private:
int ID;
string Name;
int grade1;
int grade2;
int grade3;
int avr;
};
student::student(int n, string m, int a1, int a2, int a3)
{
ID=n;
Name=m;
grade1=a1;
grade2=a2;
grade3=a3;
avr=(a1+a2+a3)/3;
}
void grade(const student& s)
{
if(s.avr>=90)cout<<"优"<<endl;
else if(s.avr>=80)cout<<"良"<<endl;
else if(s.avr>=70)cout<<"中"<<endl;
else if(s.avr>=60)cout<<"及格"<<endl;
else cout<<"不及格"<<endl;
}
int main() //测试程序
{
int n, a1,a2, a3;
string m;
cout<<"请依次输入学号,姓名,三门成绩"<<endl;
cin>>n>>m>>a1>>a2>>a3;
student s(n,m,a1,a2,a3);
grade(s);
return 0;
}
程序满足题目要求,也很简单,正式楼主所要的。
1、
#include<iostream>
using namespace std;
class Fee
{
public:
void getmoney(int n);
void spendmoney(int n);
static void display();
private:
static int money;
};
int Fee::money=0;
void Fee::getmoney(int n)
{
money=money+n;
}
void Fee::spendmoney(int n)
{
money=money-n;
}
void Fee::display()
{
cout<<money<<endl;
}
int main() //测试程序
{
Fee f;
int n,m;
cout<<"缴纳金额"<<endl;
cin>>n;
f.getmoney(n);
cout<<"花费金额"<<endl;
cin>>m;
f.spendmoney(m);
cout<<"剩余班费"<<endl;
Fee::display(); //或者f.display();
return 0;
}
2、
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int,string,int,int, int);
friend void grade(const student&);
private:
int ID;
string Name;
int grade1;
int grade2;
int grade3;
int avr;
};
student::student(int n, string m, int a1, int a2, int a3)
{
ID=n;
Name=m;
grade1=a1;
grade2=a2;
grade3=a3;
avr=(a1+a2+a3)/3;
}
void grade(const student& s)
{
if(s.avr>=90)cout<<"优"<<endl;
else if(s.avr>=80)cout<<"良"<<endl;
else if(s.avr>=70)cout<<"中"<<endl;
else if(s.avr>=60)cout<<"及格"<<endl;
else cout<<"不及格"<<endl;
}
int main() //测试程序
{
int n, a1,a2, a3;
string m;
cout<<"请依次输入学号,姓名,三门成绩"<<endl;
cin>>n>>m>>a1>>a2>>a3;
student s(n,m,a1,a2,a3);
grade(s);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我帮你写了个.
1:
#include <iostream>
using std::istream ;
using std::cout;
using std::cin;
class banfei
{
private:
static int money; //班费
friend istream& operator >>(istream& in ,banfei& a);
public:
bool getmoney(int a) //交钱
{
money+=a;
return 1;
}
bool spendmoney(int a=0) //支出
{
if(money != 0 )
{
money-=a;
return 1;
}
return 0;
}
static int display() //余款
{
return money;
}
};
int banfei::money = 0;
istream& operator >> (istream& in ,banfei& a)
{
in >> a.money;
return in;
}
int main()
{
banfei a;
cout << "输入班费 : ";
cin >> a; // 接收班费
a.spendmoney(21); // 支出;
cout << banfei::display() << "元"<<std::endl; //输出班费
system("pause");
}
2:
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::istream;
using std::string;
class kemu
{
protected:
double yuwen; // 语文
double shuxue; //数学
double waiyu ; //外语
};
class student : public kemu
{
private:
string xm; //姓名;
friend void garde(student& a);
double pj();
friend istream& operator >> (istream& in , student&);
};
double student::pj()
{
return double((yuwen + shuxue + waiyu )/3);
}
void garde(student& a)
{
double x = a.pj();
if( x >= 90 )
cout <<a.xm<< "成绩为: 优" << std::endl;
else
if( x >= 80)
cout <<a.xm<< "成绩为: 良" << std::endl;
else
if( x >= 70 )
cout <<a.xm << "成绩为: 中" << std::endl;
else
if(x >= 60 )
cout <<a.xm <<"成绩为: 及格"<<std::endl;
else
cout <<a.xm <<"成绩为: 不及格"<<std::endl;
}
istream& operator >> (istream& in , student& a)
{
in >> a.xm >>a.yuwen >> a.shuxue >> a.waiyu;
return in;
}
int main()
{
student a;
cout << "请输入成绩 :姓名 语文, 数学, 外语"<<std::endl;
cin >> a;
garde(a);
system("pause");
}
1:
#include <iostream>
using std::istream ;
using std::cout;
using std::cin;
class banfei
{
private:
static int money; //班费
friend istream& operator >>(istream& in ,banfei& a);
public:
bool getmoney(int a) //交钱
{
money+=a;
return 1;
}
bool spendmoney(int a=0) //支出
{
if(money != 0 )
{
money-=a;
return 1;
}
return 0;
}
static int display() //余款
{
return money;
}
};
int banfei::money = 0;
istream& operator >> (istream& in ,banfei& a)
{
in >> a.money;
return in;
}
int main()
{
banfei a;
cout << "输入班费 : ";
cin >> a; // 接收班费
a.spendmoney(21); // 支出;
cout << banfei::display() << "元"<<std::endl; //输出班费
system("pause");
}
2:
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::istream;
using std::string;
class kemu
{
protected:
double yuwen; // 语文
double shuxue; //数学
double waiyu ; //外语
};
class student : public kemu
{
private:
string xm; //姓名;
friend void garde(student& a);
double pj();
friend istream& operator >> (istream& in , student&);
};
double student::pj()
{
return double((yuwen + shuxue + waiyu )/3);
}
void garde(student& a)
{
double x = a.pj();
if( x >= 90 )
cout <<a.xm<< "成绩为: 优" << std::endl;
else
if( x >= 80)
cout <<a.xm<< "成绩为: 良" << std::endl;
else
if( x >= 70 )
cout <<a.xm << "成绩为: 中" << std::endl;
else
if(x >= 60 )
cout <<a.xm <<"成绩为: 及格"<<std::endl;
else
cout <<a.xm <<"成绩为: 不及格"<<std::endl;
}
istream& operator >> (istream& in , student& a)
{
in >> a.xm >>a.yuwen >> a.shuxue >> a.waiyu;
return in;
}
int main()
{
student a;
cout << "请输入成绩 :姓名 语文, 数学, 外语"<<std::endl;
cin >> a;
garde(a);
system("pause");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不要主函数么?
第一个
#include<iostream.h>
class cost
{
private:
static int money=0;
public:
void getmoney(int x)
{ money+=x; }
void spendmoney(int x)
{ money-=x; }
static void display()
{ cout<<"余额为:"<<money<<endl; }
};
第二个
#include<iostream.h>
#include<string.h>
class stu
{
public:
int ID;
int math=0,chi=0,eng=0;
double evl=0;
char name[20];
void setmath(int x)
{ math=x;evl=(math+chi+eng)/3 }
void setchi(int x)
{ chi=x;evl=(math+chi+eng)/3 }
void seteng(int x)
{ eng=x;evl=(math+chi+eng)/3 }
int getmath()
{ return math; }
int getchi()
{ return chi; }
int geteng()
{ return eng; }
void getevl()
{ return evl; }
void stu()
{ strcpy(name,""); }
void stu(stu s)
{ strcpy(name,s.name); ID=s.ID; math=s.math; chi=s.chi; eng=s.eng;
evl=s.evl; }
void stu(int sid, char* sn, int sm=0, int sc=0, int se=0)
{ strcpy(name,sn); ID=sid; math=sm; chi=sc; eng=sn; }
friend void grade();
};
void grade(stu s)
{
if(s.evl>=90)cout<<"优"<<endl;
else if(s.evl>=80)cout<<"良"<<endl;
else if(s.evl>=70)cout<<"中"<<endl;
else if(s.evl>=60)cout<<"及格"<<endl;
else cout<<"不及格"<<endl;
}
第一个
#include<iostream.h>
class cost
{
private:
static int money=0;
public:
void getmoney(int x)
{ money+=x; }
void spendmoney(int x)
{ money-=x; }
static void display()
{ cout<<"余额为:"<<money<<endl; }
};
第二个
#include<iostream.h>
#include<string.h>
class stu
{
public:
int ID;
int math=0,chi=0,eng=0;
double evl=0;
char name[20];
void setmath(int x)
{ math=x;evl=(math+chi+eng)/3 }
void setchi(int x)
{ chi=x;evl=(math+chi+eng)/3 }
void seteng(int x)
{ eng=x;evl=(math+chi+eng)/3 }
int getmath()
{ return math; }
int getchi()
{ return chi; }
int geteng()
{ return eng; }
void getevl()
{ return evl; }
void stu()
{ strcpy(name,""); }
void stu(stu s)
{ strcpy(name,s.name); ID=s.ID; math=s.math; chi=s.chi; eng=s.eng;
evl=s.evl; }
void stu(int sid, char* sn, int sm=0, int sc=0, int se=0)
{ strcpy(name,sn); ID=sid; math=sm; chi=sc; eng=sn; }
friend void grade();
};
void grade(stu s)
{
if(s.evl>=90)cout<<"优"<<endl;
else if(s.evl>=80)cout<<"良"<<endl;
else if(s.evl>=70)cout<<"中"<<endl;
else if(s.evl>=60)cout<<"及格"<<endl;
else cout<<"不及格"<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
哎呀,我上了几次课都晓得了,看教材中的例题一对照就行了,这个真的较简单的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询