/*定义一个描述学生通讯录的类,数据成员包括:姓名、学校、电话号码和邮编;成
员函数包括:输出各个数据成员的值,分别设置和获取各个数据成员的值*/
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
char *Name;
char *School;
char Tel[15];
char PC[6];
public:
void Assign(char *name, char *shcool, char tel[], char pc[]);
void SetName(char *name);
void SetSchool(char *school);
void SetPhone(char *tei);
void SetPC(char *pc);
char GetName();
char GetSchool();
char GetPhone();
char GetPC();// 设置数据和获得数据
void ShowStudent();//输出数据
};
void Student::Assign(char *name, char *school, char tel[], char pc[])
{
SetName(name);
SetSchool(school);
SetPhone(tel);
SetPC(pc);
}
void Student::SetName(char *name)
{
Name = new char[strlen(name) + 1];
strcpy(Name, name);
}
void Student::SetSchool(char *school)
{
School = new char[strlen(school) + 1];
strcpy(School, school);
}
void Student::SetPhone(char *tel)
{
strcpy(Tel, tel);
}
void Student::SetPC(char *pc)
{
strcpy(PC, pc);
}
char Student::GetName()
{
return *Name;
}
char Student::GetSchool()
{
return *School;
}
char Student::GetPhone()
{
return *Tel;
}
char Student::GetPC()
{
return *PC;
}
void Student::ShowStudent()
{
cout << Name << '\t' << School << '\t' << Tel << '\t' << PC << endl;
}//学生通讯类
int main()
{
int n;
char *name="qiaoxu";
char *school="jiangsudaxue";
char tel[15];
char pc[6];
cout << "请输入学生人数n:" << endl;
cin >> n;
Student *s;
s = new Student[n];
cout << "name" << '\t' << "school" << '\t' << "telphone" << '\t' << "post code:" << endl;
for (int i=0;i<n;i++)
{
cin >> name>> school>> tel >> pc;
s[i].Assign(name, school, tel, pc);
}
for (int j = 0; j < n; j++)
{
s[j].ShowStudent();
}
delete[]s;
system("pause");
return 0;
}
问题是 会提示写入权限冲突
_Str 是 0x132DC74
相关分类