|
定义一个学生类,其中有3个数据成员有学号、姓名、年龄,以及若干成员函数。同时编写主函数使用这个类,实现对学生数据的赋值和输出。定义日期类型Date。
实验内容使用成员函数实现对输出的输入、输出; 使用构造函数和析构函数实现对数据的输入、输出。 定义日期类型Date。要求有以下面成员:
1、 可以设置日期;
2、 日期加一天操作;
3、 输入函数,输入格式为“XXXX年XX月XX日”。 程序/结果使用成员函数实现对输出的输入、输出
#include <iostream>
using namespace std;
class MyClassmate
{public:
void set_information( ); //公用成员函数
void show_information( ); //公用成员函数
private: //数据成员为私有
int number,year,month,day;
char name[30];
};
int main( )
{
MyClassmate t1; //定义对象t1
t1.set_information( ); //调用对象t1的成员函数set_time,向t1的数据成员输入数据
t1.show_information( );
cout<<endl;
cout<<"请输入下一位学生信息: "<<endl; //调用对象t1的成员函数show_time,输出t1的数据成员的值
MyClassmate t2; //定义对象t2
t2.set_information( ); //调用对象t2的成员函数set_time,向t2的数据成员输入数据
t2.show_information( );
cout<<endl;
cout<<"请输入下一位学生信息: "<<endl;
MyClassmate t3; //定义对象t2
t3.set_information( ); //调用对象t2的成员函数set_time,向t2的数据成员输入数据
t3.show_information( );
cout<<"输入学生信息完毕"<<endl; //调用对象t2的成员函数show_time,输出t2的数据成员的值
return 0;
}
void MyClassmate::set_information( ) //在类外定义set_time函数
{
cout<<endl;
cout<<"请输入学生信息: "<<endl;
cout<<endl;
cout<<" "<<"姓名:";
cin>>name;
cout<<" "<<"学号:";
cin>>number;
cout<<" "<<"出生年月:"<<endl;
cout<<"输入年:";
cin>>year;
cout<<"输入月:";
cin>>month;
cout<<"输入日:";
cin>>day;
}
void MyClassmate::show_information( ) //在类外定义show_time函数
{
cout<<endl;
cout<<"学生信息: "<<endl;
cout<<endl;
cout<<"姓名:"<<name<<" "<<"学号: "<<number<<" "<<"年龄: "<<2010-year<<" "<<"出生年月: "<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
显示结果:
使用构造函数和析构函数实现对数据的输入、输出
#include <iostream.h>
#include <string.h>
class MyClassmates
{
public:
MyClassmates(char *,int,int,int,int); //构造函数声明
~MyClassmates(); //析构函数声明
protected:
int number,year,month,day;
char name[30];
};
MyClassmates::MyClassmates(char * pName,int x,int y,int m,int d) //构造函数实现
{
strcpy(name,pName);
number=x;
year=y;
month=m;
day=d;
cout<<endl;
cout<<"姓名:"<<name<<endl;
cout<<endl;
cout<<"学号: "<<number<<endl;
cout<<endl;
cout<<"年龄: "<<2010-year<<endl;
cout<<"出生年月: "<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
cout<<endl;
}
MyClassmates::~MyClassmates()
{
}
void main()
{
MyClassmates hefei("hefei",9,1989,8,1);
MyClassmates Rose("Rose",15,1988,3,15);
MyClassmates Jack("Jack",3,1989,5,8);
显示结果:
定义日期类型Date
#include <iostream>
using namespace std;
class Time
{public:
void set_time( ); //公用成员函数
void show_time( );
void show_nexttime( ); //公用成员函数
private: //数据成员为私有
int hour;
int minute;
int sec;
};
int main( )
{
Time t1; //定义对象t1
t1.set_time( ); //调用对象t1的成员函数set_time,向t1的数据成员输入数据
t1.show_time( );
t1.show_nexttime( ); //调用对象t1的成员函数show_time,输出t1的数据成员的值
return 0;
};
void Time::set_time( ) //在类外定义set_time函数
{
cout<<"请设置新的时间:"<<endl;
cout<<endl;
cout<<"设置新的年:"<<endl;
cin>>hour;
cout<<"设置新的月:"<<endl;
cin>>minute;
cout<<"设置新的日:"<<endl;
cin>>sec;
}
void Time::show_time( ) //在类外定义show_time函数
{
cout<<endl;
cout<<"现在的日期是:"<<endl;
cout<<endl;
cout<<hour<<"年"<<minute<<"月"<<sec<<"日"<<endl;
}
void Time::show_nexttime( ) //公用成员函数
{
cout<<endl;
cout<<"下一日的日期是:"<<endl;
cout<<endl;
cout<<hour<<"年"<<minute<<"月"<<sec+1<<"日"<<endl;
cout<<endl;
}
显示结果:
本经验的程序、文字以及图片均为乔布斯的同学原创 |
|