资源描述
内蒙古工业大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;
}
展开阅读全文