c++ 字符数组
创建一个employee类,该类中用字符数组表示姓名、街道地址、市、省和邮政编码。把构造函数、changname()、display()的函数的原型放在类定义中,函数实现放在类外。构造函数初始化每个成员,changname()可以让用户修改姓名,display()函数把完整的对象数据打印出来。主函数中建立两个对象测试一下
追问:我在你的代码上做了修改,为什么postnum出来一堆“烫”呢?求解释#include<iostream>
using namespace std;
class Employee
{
private:
char name[20];
char address[100];
char city[15];
char province[15];
char postnum[20];
public:
Employee(char *name,char *address,char *city,char *province,char *postname);
void changename();
void display();
};
Employee::Employee(char *name,char *address,char *city,char *province,char *postname)
{
strcpy(this->name,name);
strcpy(this->address,address);
strcpy(this->city,city);
strcpy(this->province,province);
strcpy(this->postnum,postnum);
}
void Employee::changename()
{char name1[20];
cout<<"修改姓名:"<<endl;
cin>>*name1;
strcpy(this->name,name1);
}
void Employee::display()
{
cout<<name<<endl;
cout<<address<<endl;
cout<<city<<endl;
cout<<province<<endl;
cout<<postnum<<endl;
}
int main()
{ Employee a("ssss","aa","cc","ddd","dsd");
a.display();
a.changename();
a.display();
return 0;
}
答案:
#include<iostream>
using namespace std;
class Employee
{
private:
char name[20];
char address[100];
char city[15];
char province[15];
char postnum[10];
public:
Employee(char *name,char *address,char *city,char *province,char *postname);
void changename();
void display();
};
Employee::Employee(char *name,char *address,char *city,char *province,char *postname)
{
strcpy(this->name,name);
strcpy(this->address,address);
strcpy(this->city,city);
strcpy(this->province,province);
strcpy(this->postnum,postnum);
}
void Employee::changename()
{
char name1[20];
strcpy(name1,name);
}
void Employee::display()
{
cout<<name<<endl;
cout<<address<<endl;
cout<<city<<endl;
cout<<province<<endl;
cout<<postnum<<endl;
}
int main()
{
return 0;
}
#define STR_LEN 256
#include <stdio.h>
#include <string.h>
class employee
{
public:
employee();
void display();
void changename();
protected:
char szName[STR_LEN];
char szStreet[STR_LEN];
char szCity[STR_LEN];
char szProvince[STR_LEN];
char szPostCode[STR_LEN];
};
employee::employee()
{
memset (this, 0, sizeof(*this));
}
void employee::display()
{
printf ("EmployDiskplay:\n");
printf ("Name=%s\n", szName);
printf ("Street=%s\n", szStreet);
printf ("City=%s\n", szCity);
printf ("Province=%s\n", szProvince);
printf ("Postcode=%s\n", szPostCode);
}
void employee::changename()
{
char buf[STR_LEN];
printf ("Please input Name, # to skip:\n");
scanf("%s", buf);
if (buf[0] != '#')
strcpy (szName, buf);
printf ("Please input Street, # to skip:\n");
scanf("%s", buf);
if (buf[0] != '#')
strcpy (szStreet, buf);
printf ("Please input City, # to skip:\n");
scanf("%s", buf);
if (buf[0] != '#')
strcpy (szCity, buf);
printf ("Please input Province, # to skip:\n");
scanf("%s", buf);
if (buf[0] != '#')
strcpy (szProvince, buf);
printf ("Please input Postcode, # to skip:\n");
scanf("%s", buf);
if (buf[0] != '#')
strcpy (szPostCode, buf);
}
int main()
{
employee a;
a.display();
a.changename();
a.display();
}
上一个:c与c++有什么区别?
下一个:C++怎么读?