|
#include<iostream>
using namespace std;
#include<string>
#include<cstring>
class Student
{
public:
Student(const char* name, const int score)
{
m_name = new char[strlen(name) + 1];
m_score = score;
memcpy(m_name, name, strlen(name));
}
Student(const Student& s)
{
m_name = new char[strlen(s.m_name) + 1];
memcpy(m_name, s.m_name, strlen(s.m_name));
m_score = s.m_score;
}
void setName(const char* name)
{
m_name = new char[strlen(name) + 1];
memcpy(m_name, name, strlen(name));
}
~Student()
{
if (m_name != NULL)
{
delete[]m_name;
m_name = NULL;
}
}
void print_info()
{
cout << "name:" << m_name << ", score:" << m_score << endl;
}
private:
char* m_name;
int m_score;
};
int main()
{
Student stu1("Join", 98);
Student stu2(stu1);
stu2.setName("Tom");
stu1.print_info();
stu2.print_info();
system("pause");
return 0;
}
|
上一篇: 请教下cin>>输入问题下一篇: 番茄助手
|