编写程序,已有若干学生的基本信息,包括学号、姓名、成绩,要求输出学生基本信息并计算显示学生总人数和平均成绩。
提示:设计Student类,记录学生的基本信息,并将学生人数和总成绩声明为静态数据成员;在主函数中定义Student对象数组,存储和汇总学生信息。
/*3.编写程序,已有若干学生的基本信息,
包括学号、姓名、成绩,
要求输出学生基本信息并计算显示学生总人数和平均成绩。
提示:设计Student类,记录学生的基本信息,
并将学生人数和总成绩声明为静态数据成员;
在主函数中定义Student对象数组,存储和汇总学生信息。
*/
#include<iostream>
#include<string>
using namespace std;
class Student{
private:
int date;
string name;
double score;
public:
static double toscore;
static int tostudents;
Student(){
tostudents++;
}
Student(int d,string n,double s=0)
:date(d),name(n),score(s){
toscore+=get_score();
tostudents++;
}
void set(int d,string n,double s=0){
date=d;
name=n;
score=s;
toscore+=get_score();
}
void output()const{
cout<<"姓名:"<<name<<"\t学号:"<<date<<"\t分数:"<<score<<endl;
}
double get_score()const{return score;}
};
int Student::tostudents=0;
double Student::toscore=0;
int main(){
Student a[3]={Student(10086,"final",150),Student(10010,"张三",120)
,Student(10000,"李四",100)};
a[0].output();
a[1].output();
a[2].output();
cout<<"总人数:"<<Student::tostudents<<
"\t平均分:"<<(Student::toscore)/(Student::tostudents)<<endl;
system("pause");
return 0;
}
4.定义一个CPoint类,包含私有数据成员x、y(均为整型),提供构造函数、读取值成员函数(getX、getY)、显示输出成员函数(show)等;在CPoint类的基础上,采用组合类的方法定义一个矩形类CRect,包含私有数据成员leftup、rightdown(均为CPoint对象,表示矩形左上和右下点坐标),提供构造函数、求周长、求面积、显示输出矩形基础信息(两点坐标值)等成员函数。编写主函数,创建两个CPoint类对象并初始化,创建一个CRect类对象并使用前面定义的两个CPoint类对象进行初始化,输出矩形对角线两点的坐标值信息,计算输出矩形的周长和面积。
/*4.定义一个CPoint类,包含‘私有’数据成员x、y(均为整型),
提供构造函数、读取值成员函数(getX、getY)、显示输出成员函数(show)等;
在CPoint类的基础上,采用组合类的方法定义一个矩形类CRect,
包含‘私有’数据成员leftup、rightdown(均为CPoint对象,表示矩形左上和右下点坐标)
提供构造函数、求周长、求面积、显示输出矩形基础信息(两点坐标值)等成员函数。
编写主函数,
创建两个CPoint类对象并初始化,
创建一个CRect类对象并使用前面定义的两个CPoint类对象进行初始化,
输出矩形对角线两点的坐标值信息,计算输出矩形的周长和面积。
*/
#include<iostream>
using namespace std;
/*
4.定义一个CPoint类,包含‘私有’数据成员x、y(均为整型),
提供构造函数、读取值成员函数(getX、getY)、显示输出成员函数(show)等;
*/
class CPoint{
private:
int x,y;
public:
CPoint(int a=0,int b=0)
:x(a),y(b){}
int set(int a=0,int b=0){x=a,y=b;}
int getX()const{return x;}
int getY()const{return y;}
void show()const{
cout<<'('<<x<<','<<y<<')'<<endl;
}
};
/*在CPoint类的基础上,采用组合类的方法定义一个矩形类CRect,
包含‘私有’数据成员leftup、rightdown(均为CPoint对象,表示矩形左上和右下点坐标)
提供构造函数、求周长、求面积、显示输出矩形基础信息(两点坐标值)等成员函数。
*/
class CRect{
private:
CPoint leftup,rightdown;
public:
CRect(int x1 ,int y1,int x2 ,int y2):leftup(x1,y1),rightdown(x2,y2){}
int Rect_c(){
int c;
c=(rightdown.getX()-leftup.getX())+(leftup.getY()-rightdown.getY());
return 2*c;
}
int Rect_s(){
int s;
s=(rightdown.getX()-leftup.getX())*(leftup.getY()-rightdown.getY());
return s;
}
void output(){
cout<<"矩形两点的坐标是:"<<endl;
leftup.show();
rightdown.show();
cout<<"矩形周长:"<<endl;
cout<<Rect_c()<<endl;
cout<<"矩形面积:"<<endl;
cout<<Rect_s()<<endl;
}
};
int main(){
int z,x,c,v;
cout<<"请输入矩形左上点坐标:";
cin>>z>>x;
cout<<"请输入矩形右下点坐标:";
cin>>c>>v;
CRect c1(z,x,c,v);
c1.output();
system("pause");
return 0;
}
c++基础思维导图(转载)