(1)构造函数 —— 学生信息类
#include<iostream>
#include<string>
using namespace std;
class Student
{
/********* Begin *********/
public:
Student();
Student(int sid,string name);
~Student();
int SID;
string Name;
//在此处声明所需的成员
/********* End *********/
};
/********* Begin *********/
Student::Student()
{
SID=0;
Name="王小明";
}
//在此处定义成员函数
Student::Student(int sid,string name)
{
SID=sid;
Name=name;
}
Student::~Student()
{
cout<<SID<<" "<<Name<<" "<<"退出程序"<<endl;
}
(2)对象数组 —— 学生信息表
#include <string>
#include <iostream>
using namespace std;
/********* Begin *********/
class Student
{
//在此处声明所需的成员
public:
int SID;
string Name;
float Sco;
Student();
Student(int sid,string name,float sco)
{
SID=sid;
Name=name;
Sco=sco;
}
};
Student ss[5];
int ssCount;
Student::Student()
{}
/********* End *********/
void Add(int sid,string name,float sco)
{
/********* Begin *********/
ss[ssCount]=Student(sid,name,sco);
ssCount++;
/********* End *********/
}
void PrintAll()
{
/********* Begin *********/
//打印出学生表中所有记录
for(int i=0;i<ssCount;i++)
{
cout<<ss[i].SID<<" "<<ss[i].Name<<" "<<ss[i].Sco<<endl;
}
/********* End *********/
}
void Average()
{
/********* Begin *********/
//计算并打印出学生表中的平均成绩
float sum=0.00;
float ave;
for(int i=0;i<ssCount;i++)
{
sum+=ss[i].Sco;
}
ave=sum/ssCount;
cout<<"平均成绩"<<" "<<ave<<endl;
/********* End *********/
}
(3)静态成员 —— 模拟共享书店
#include <string>
#include <iostream>
using namespace std;
/********* Begin *********/
class User
{
//在此处声明所需的成员
public:
static int UserCount;
static int BookCount;
string Name;
int Books;
User(string name,int books);
~User();
static void GetState();
};
int User::UserCount=0;
int User::BookCount=0;
User::User(string name,int books)
{
UserCount++;
Name=name;
Books=books;
cout<<Name<<" "<<Books<<" "<<"进入"<<endl;
BookCount+=Books;
}
User::~User()
{
UserCount--;
cout<<Name<<" "<<Books<<" "<<"离开"<<endl;
BookCount-=Books;
}
void User::GetState()
{
cout<<"书店人数:"<<User::UserCount<<","<<"书店共享书数量:"<<User::BookCount<<","<<"人均共享数量:"<<User::BookCount/User::UserCount<<endl;
}