定义一个学生类,包括姓名,姓名长度,年龄,性别。写出该类的构造函数、拷贝构造函数、析构函数;并重载=
包括数据成员char*name(用于存放姓名印文字符串),intlen;存放姓名的长度,intAge用于存放年龄,boolsex;存放性别。急急急!!!...
包括 数据成员 char* name (用于存放姓名印文字符串),int len;存放姓名的长度,int Age 用于存放年龄,bool sex;存放性别。
急急急!!! 展开
急急急!!! 展开
展开全部
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
public:
Student(char*, int, bool);
Student(const Student&);
~Student();
Student& operator =(const Student&);
void print();
private:
char* name;
int age;
bool sex;
int len;
};
Student::Student(char* name, int age, bool sex)
{
len = strlen(name);
this->name = new char[len+1];
strcpy(this->name, name);
this->age = age;
this->sex = sex;
}
Student::Student(const Student& stu)
{
len = strlen(stu.name);
this->name = new char[len+1];
strcpy(this->name, stu.name);
this->age = stu.age;
this->sex = stu.sex;
}
Student::~Student()
{
len = 0;
delete[] name;
}
Student& Student::operator =(const Student& stu)
{
len = strlen(stu.name);
this->name = new char[len+1];
strcpy(this->name, stu.name);
this->age = stu.age;
this->sex = stu.sex;
return *this;
}
void Student::print()
{
cout << name << '\t';
if (sex) cout << "男";
else cout << "女";
cout << '\t' << age << endl;
}
int main()
{
Student s1("张三", 20, true);
Student s2(s1);
Student s3 = s1;
s1.print();
s2.print();
s3.print();
return 0;
}
#include <cstring>
using namespace std;
class Student
{
public:
Student(char*, int, bool);
Student(const Student&);
~Student();
Student& operator =(const Student&);
void print();
private:
char* name;
int age;
bool sex;
int len;
};
Student::Student(char* name, int age, bool sex)
{
len = strlen(name);
this->name = new char[len+1];
strcpy(this->name, name);
this->age = age;
this->sex = sex;
}
Student::Student(const Student& stu)
{
len = strlen(stu.name);
this->name = new char[len+1];
strcpy(this->name, stu.name);
this->age = stu.age;
this->sex = stu.sex;
}
Student::~Student()
{
len = 0;
delete[] name;
}
Student& Student::operator =(const Student& stu)
{
len = strlen(stu.name);
this->name = new char[len+1];
strcpy(this->name, stu.name);
this->age = stu.age;
this->sex = stu.sex;
return *this;
}
void Student::print()
{
cout << name << '\t';
if (sex) cout << "男";
else cout << "女";
cout << '\t' << age << endl;
}
int main()
{
Student s1("张三", 20, true);
Student s2(s1);
Student s3 = s1;
s1.print();
s2.print();
s3.print();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上面的回答很好,我就不答了,谢谢关注
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询