收藏 分销(赏)

C--程序设计实验4-5.doc

上传人:仙人****88 文档编号:7859629 上传时间:2025-01-23 格式:DOC 页数:9 大小:18.94KB 下载积分:10 金币
下载 相关 举报
C--程序设计实验4-5.doc_第1页
第1页 / 共9页
C--程序设计实验4-5.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
内蒙古工业大C++实验源代码答案 仅供参考 实验四 实现Employee类 (一) 实验目的 1.学习字符串数据的组织和处理 2.掌握指针的使用方法 3.使用字符数组和标准C++库练习处理字符串的方法 (二) 实验内容 1.声明一个Employee类,其中包括表示姓名、街道地址、城市和邮政编码等属性,包括change_name( )和display( )等函数。 2.成员函数display( )使用cout语句显示姓名、街道地址、城市和邮政编码等属性,成员函数change_name( )改变对象的姓名属性,实现并测试这个类。 (三) 实验要求 1.掌握用字符数组和标准C++库处理字符串的方法。 2.下课前完成实验内容,提交给教师检查。 实验五 实现由Object类派生出的Box类 (一) 实验目的 1.学习定义和使用类的继承关系、定义派生类。 2.熟悉不同继承方式下对基类成员的访问控制。 3.学习派生类的构造和析构函数的调用顺序。 (二) 实验内容 1.声明一个Object类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及相应的操作函数。 2.声明一个Box对象,观察构造函数与析构函数的调用顺序。 (三) 实验要求 1.分析程序运行结果。 2.下课前完成实验内容,提交给教师检查。 实验四 #include<iostream> #include<cstring> using namespace std; #define len 20 class Employee { public: Employee() { strcpy(name,"000"); strcpy(urban,"000"); strcpy(postcode,"000"); strcpy(streetaddress,"000"); } void input(); void changename(char changename[]); void display(); ~Employee(){} private: char name[len]; char urban[len]; char postcode[len]; char streetaddress[len]; }; void Employee::input() { char n[len],u[len],sa[len],p[len]; cout<<"Please input employee's information"<<endl; cout<<"name\t"<<"urban\t"<<"streetaddress\t"<<"postcode"<<endl; cin>>n; cin>>u; cin>>sa; cin>>p; strcpy(name,n); strcpy(urban,u); strcpy(streetaddress,sa); strcpy(postcode,p); } void Employee::changename(char changename[]) { strcpy(name,changename); } void Employee::display() { cout<<"\tEmployee's information is :"<<endl; cout<<"\tname: \t"<<name<<endl; cout<<"\tstreetaddress:\t"<<streetaddress<<endl; cout<<"\turban: \t"<<urban<<endl; cout<<"\tpostcode: \t"<<postcode<<endl; } int main() { Employee people; people.display(); char ch,changename[len]; people.input(); people.display(); cout<<"are you changename ?"<<endl; cin>>ch; if(ch=='Y') { cout<<"input changename:"<<endl; cin>>changename; people.changename(changename); } people.display(); return 0; } 实验五 #include<iostream> using namespace std; class Object { public: Object(); Object(float x); ~Object() { cout<<"object of destructor."<<endl; } void output(); void put(float x); private: float weight; }; Object::Object() { cout<<"object of constructor."<<endl; weight=0.0; } Object::Object(float x) { cout<<"object of constructor."<<endl; weight=x; } void Object::output() { cout<<"\tweight:\t"<<weight<<endl; } void Object::put(float x) { weight=x; } class Box:public Object { public: Box(); Box(float x,float y,float z); void input(float x,float y,float z); void show(); ~Box() { cout<<"box of destructor."<<endl; } private: float height; float width; }; Box::Box():Object() { cout<<"box of constructor."<<endl; height=0.0; width=0.0; } Box::Box(float x,float y,float z):Object(x) { cout<<"box of constructor."<<endl; height=y; width=z; } void Box::input(float x,float y,float z) { put(x); height=y; width=z; } void Box::show() { cout<<"\tBox of informations is:"<<endl; output(); cout<<"\theight:\t"<<height<<endl; cout<<"\twidth: \t"<<width<<endl; } int main() { float x,y,z; Box boxes; boxes.show(); cout<<"\tPlease input weight,height and width:"<<endl; cin>>x>>y>>z; boxes.input(x,y,z); boxes.show(); return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服