构造函数与析构函数

(1)创建一个Employee类,该类中用字符数组存放Employee的信息,如姓名、地址、市、省、及邮政编码;每个成员函数的定义放在类定义之外;成员函数包括改变姓名数据成员等;构造函数完成成员数据的初始化;用Display()函数将完整的对象数据打印出来;其中数据成员是保护的,成员函数是公共的。

/*main.cpp*/
#include"iostream.h"
#include"string.h"
#include"stdio.h"
#include"stdlib.h"
#include"Employee.h"
void main()
{	int num;
	char n[20],a[20],s[20],sh[20],p[20];
	cout<<"姓名"<<endl;
	gets(n);
	cout<<"地址"<<endl;
	gets(a);
	cout<<"市"<<endl;
	gets(s);
	cout<<"省"<<endl;
	gets(sh);
	cout<<"邮政编码"<<endl;
	gets(p);
    Employee em(n,a,s,sh,p);
	em.Display();	
	cout<<"1.修改姓名"<<endl<<"2.修改地址"<<endl<<"3.市"<<endl<<"4.省"<<endl<<"5.邮政编码"<<endl<<"0.退出"<<endl;
	cout<<"请输入你想进行的操作"<<endl;
	while(cin>>num,num!=0)
	{
	switch(num)
	{
	case 1:
		char name[10];
	    gets(name);
	    em.ChangeName(name);
        break;
	case 2:
		char address[20];
		gets(address);
		em.ChangeAddress(address);
		break;
	case 3:
		char shi[20];
		gets(shi);
		em.ChangeShi(shi);
		break;
	case 4:
		char sheng[20];
		gets(sheng);
		em.ChangeSheng(sheng);
		break;
	case 5:
		char post[10];
		gets(post);
		em.ChangePost(post);
		break;
	}
	}
    em.Display();
}
/*employee.h*/
class Employee
{
public:
	Employee(char n[20],char a[50],char s[20],char sh[20],char p[6]);
	void ChangeName(char ch_name[20]);
	void ChangeAddress(char ch_address[50]);
	void ChangeShi(char ch_shi[20]);
	void ChangeSheng(char ch_sheng[20]);
	void ChangePost(char ch_post[6]);
	void Display();
private:
	char name[20];
	char address[50];
	char shi[20];
	char sheng[20];
	char post[6];
};
/*employee.cpp*/
#include"iostream.h"
#include"string.h"
#include"stdio.h"
#include"stdlib.h"
#include"Employee.h"

Employee::Employee(char n[20],char a[50],char s[20],char sh[20],char p[6])
{
	strcpy(name,n);
	strcpy(address,a);
	strcpy(shi,s);
	strcpy(sheng,sh);
	strcpy(post,p);
}
void Employee::ChangeName(char ch_name[20])
{
	strcpy(name,ch_name);
}
void Employee::ChangeAddress(char ch_address[20])
{
	strcpy(address,ch_address);
}
void Employee::ChangeShi(char ch_shi[20])
{
	strcpy(shi,ch_shi);
}
void Employee::ChangeSheng(char ch_sheng[20])
{
	strcpy(sheng,ch_sheng);
}
void Employee::ChangePost(char ch_post[6])
{
	strcpy(post,ch_post);
}
void Employee::Display()
{
	cout<<"姓名:"<<name<<endl;
	cout<<"地址:"<<address<<endl;
	cout<<"市:"<<shi<<endl;
	cout<<"省:"<<sheng<<endl;
	cout<<"邮政编码:"<<post<<endl;
}

(2) 设计一个表示矩形的类Rect,其矩形成员长float * Length ,宽float * Width为指针变量,设计相应成员函数,实现下列功能:
① 构造函数设置长和宽(默认为1)。
② 复制构造函数用老对象生成新对象。
③ set()函数设置长和宽(默认为0)。
④ 计算并返回长方形的周长。
⑤ 计算并返回长方形的面积。
⑥ 析构函数释放动态分配的长和宽。
编制主程序应用指针建立对象测试类。

/*main.cpp*/
#include"iostream.h"
#include"Rect.h"
void main()
{	float l,w;
	cout<<"请输入你想计算的长和宽"<<endl;
	cin>>l>>w;
	Rect r1(l,w);
	cout<<"周长为:"<<r1.peri()<<endl;
	cout<<"面积为:"<<r1.area()<<endl;
	Rect *p=new Rect(r1);
	cout<<"周长为:"<<p->peri()<<endl;
	cout<<"面积为"<<p->area()<<endl;
	cout<<"请输入重新设置的长和宽"<<endl;
	cin>>l>>w;
	p->set(l,w);
	cout<<"周长为:"<<p->peri()<<endl;
	cout<<"面积为"<<p->area()<<endl;
}
/*.h*/
#include"iostream.h"
class Rect
{private:
	float *Length;
	float *Width;
public:
	Rect(float a=1,float b=1);
	Rect(Rect &);
	void set(float a=0,float b=0);
	float peri();
	float area();
	~Rect(){delete Length;delete Width;}

};
/*Rect.cpp*/
#include"iostream.h"
#include"Rect.h"
void Rect::set(float a,float b)
{
	*Length=a;
	*Width=b;
}
Rect::Rect(float a,float b)
{
    Length=new float;
	Width=new float;
	*Length=a;
	*Width=b;
}
Rect::Rect(Rect &r)
{   
	Length=new float;
	Width=new float;
	Length=r.Length;
	Width=r.Width;
}
float Rect::peri()
{
	return 2*(*Length)+2*(*Width);
}
float Rect::area()
{
	return (*Length)* (*Width);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值