主题:[原创]多文件的结构
//
1. 编写一个学生类,属性包含学生的学号、性别、成绩。其中要求学号用静态成员变量进行处理。方法包括:构造函数,拷贝构造函数,学生信息显示函数,学生信息设置函数等。
2. 编写一个主函数,生产一个学生类的对象,用户可以选择输入该学生信息,显示该学生信息,退出程序等功能。
3. 将上述编程拆分成三个文件。分别为:学生类定义头文件,学生类实现文件,主函数实现文件。
//文件1,类的定义,student.h
#include<iostream>
using namespace std;
class Student //类的定义
{
public:
Student(int news,int newsex);//构造函数
Student(Student &s);//拷贝构造函数
int Getscore(){return score;}
int Getsex(){return sex;}
void setStudent(int,int);
static int GetN(){return Number;}
private:
int score;
int sex;
static int Number;
};
//文件2,类的实现,student.cpp
#include<iostream>
using namespace std;
int Student::Number=1000; //使用类名初始化静态数据成员
Student::Student(int news,int newsex)
{
score=news;
sex=newsex;
Number++;
}
Student::Student(Student &s)
{
score=s.score;
sex=s.sex;
Number++;
}
void Student::setStudent(int news,int newsex)
{
score=news;
sex=newsex;
}
//文件3,主函数,5_04.cpp
#include<iostream>
using namespace std;
void main()
{
int i,j;//用于接收属性
int choice;
Student A(4,5);
Student B(A);
while(choice!=3)
{
cout<<"请选择(1-输入该学生信息,2-显示该学生信息,3-退出):"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"请输入学生成绩:"<<endl;
cin>>i;
cout<<"请输入学生性别:"<<endl;
cin>>j;
break;
case 2: cout<<"学生的成绩是:"<<A.Getscore()<<"性别是:"<<A.Getsex()<<"学生的学号是:"<<A.GetN()<<endl;
A.setStudent(i,j);
cout<<"学生的成绩是:"<<A.Getscore()<<"性别是:"<<A.Getsex()<<"学生的学号是:"<<A.GetN()<<endl;
cout<<"学生的成绩是:"<<B.Getscore()<<"性别是:"<<B.Getsex()<<"学生的学号是:"<<B.GetN()<<endl;
break;
case 3: break;
}
}
}
//程序编译连接都没有错,只是number的初始值不明白怎么变成了2?
//各位高手能告诉我怎么做吗?
//还有最好有例子。谢谢
1. 编写一个学生类,属性包含学生的学号、性别、成绩。其中要求学号用静态成员变量进行处理。方法包括:构造函数,拷贝构造函数,学生信息显示函数,学生信息设置函数等。
2. 编写一个主函数,生产一个学生类的对象,用户可以选择输入该学生信息,显示该学生信息,退出程序等功能。
3. 将上述编程拆分成三个文件。分别为:学生类定义头文件,学生类实现文件,主函数实现文件。
//文件1,类的定义,student.h
#include<iostream>
using namespace std;
class Student //类的定义
{
public:
Student(int news,int newsex);//构造函数
Student(Student &s);//拷贝构造函数
int Getscore(){return score;}
int Getsex(){return sex;}
void setStudent(int,int);
static int GetN(){return Number;}
private:
int score;
int sex;
static int Number;
};
//文件2,类的实现,student.cpp
#include<iostream>
using namespace std;
int Student::Number=1000; //使用类名初始化静态数据成员
Student::Student(int news,int newsex)
{
score=news;
sex=newsex;
Number++;
}
Student::Student(Student &s)
{
score=s.score;
sex=s.sex;
Number++;
}
void Student::setStudent(int news,int newsex)
{
score=news;
sex=newsex;
}
//文件3,主函数,5_04.cpp
#include<iostream>
using namespace std;
void main()
{
int i,j;//用于接收属性
int choice;
Student A(4,5);
Student B(A);
while(choice!=3)
{
cout<<"请选择(1-输入该学生信息,2-显示该学生信息,3-退出):"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"请输入学生成绩:"<<endl;
cin>>i;
cout<<"请输入学生性别:"<<endl;
cin>>j;
break;
case 2: cout<<"学生的成绩是:"<<A.Getscore()<<"性别是:"<<A.Getsex()<<"学生的学号是:"<<A.GetN()<<endl;
A.setStudent(i,j);
cout<<"学生的成绩是:"<<A.Getscore()<<"性别是:"<<A.Getsex()<<"学生的学号是:"<<A.GetN()<<endl;
cout<<"学生的成绩是:"<<B.Getscore()<<"性别是:"<<B.Getsex()<<"学生的学号是:"<<B.GetN()<<endl;
break;
case 3: break;
}
}
}
//程序编译连接都没有错,只是number的初始值不明白怎么变成了2?
//各位高手能告诉我怎么做吗?
//还有最好有例子。谢谢