职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 6632|回复: 2

【C++程序设计实验】类的定义和使用2

[复制链接]
已经来了吗 发表于 2011-9-25 11:34 | 显示全部楼层 |阅读模式
0e655ca7a0b193cfd0435879.jpg
定义一个学生类,其中有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;
}
显示结果: 4e83cb6292a8be87e6113a5c.jpg 使用构造函数和析构函数实现对数据的输入、输出
#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);
显示结果: 95afee1f1c8d76afe1fe0b5b.jpg 定义日期类型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;
}
显示结果: e78c6589ed9552e90e24443b.jpg

本经验的程序、文字以及图片均为乔布斯的同学原创
奔跑着 发表于 2012-7-23 18:36 | 显示全部楼层
写给即将入行的程序员的一封信http://blog.sina.com.cn/s/blog_5ce5700e0101669p.html
奔跑着 发表于 2012-9-11 15:03 | 显示全部楼层
写给即将入行的程序员的一封信
http://blog.sina.com.cn/s/blog_5ce5700e0101669p.html
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-20 05:56 , Processed in 0.162277 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表