3.cpp

来自「平常学习c++的一些小程序」· C++ 代码 · 共 54 行

CPP
54
字号
/*
Please fulfill the definition of the following class and add 
an assignment operator overloaded function which means 
when we declared person p1, p2, we can realize p2=p1.
 Then you can test it in the main function.
 */

#include <iostream>
#include <string>
using namespace std;

class person{
	char *Name;
	int  Age;
	char Sex;
public:
	person(char *name="JACK",int age=10,char sex='F');
	~person(){}
	person operator=(const person& p);
	char *GetName(){return Name;}
	int  GetAge(){return Age;}
	char GetSex(){return Sex;}
        void show();
};

person::person(char *name,int age,char sex)
{
	Name=new char[strlen(name)+1];
	strcpy(Name,name);
	Age=age;
	Sex=sex;
}
person person::operator=(const person& p)
{
	Name=new char[strlen(p.Name)+1];
	strcpy(Name,p.Name);
	Age=p.Age;
	Sex=p.Sex;
	return person(Name,Age,Sex);
}
void person::show()
{
	cout<<Name<<'\t'<<Age<<'\t'<<Sex<<endl;
}
void main()
{
	person p1;
	person p2("hewei",19,'F');
	p1.show();
	p1=p2;
	p1.show();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?