![]() |
#2
rjsp2012-02-15 14:29
![]() #define _CRT_SECURE_NO_WARNINGS #include <iostream> class CStudent { friend std::ostream& operator<<( std::ostream&, const CStudent& ); public: CStudent( const char* name, unsigned int age ); CStudent( const CStudent& rhs ); CStudent& operator=( const CStudent& rhs ); ~CStudent(); void show(void); private: char* name_; unsigned int age_; }; using namespace std; CStudent::CStudent( const char* name, unsigned int age ) { name_ = new char[strlen(name)+1]; strcpy( name_, name ); age_ = age; } CStudent::CStudent( const CStudent& rhs ) { name_ = new char[strlen(rhs.name_)+1]; strcpy( name_, rhs.name_ ); age_ = rhs.age_; } CStudent& CStudent::operator=( const CStudent& rhs ) { if( this != &rhs ) { delete[] name_; name_ = new char[strlen(rhs.name_)+1]; strcpy( name_, rhs.name_ ); age_ = rhs.age_; } return *this; } CStudent::~CStudent() { delete[] name_; cout<<"destructor of class"<<endl; } ostream& operator<<( ostream& os, const CStudent& st ) { return os << st.name_ << ' ' << st.age_; } int main() { CStudent* pst1 = new CStudent("张三",20); CStudent st2("李四",50); CStudent st3(st2); cout << *pst1 << endl; cout << st2 << endl; cout << st3 << endl; delete pst1; return 0; } |

#include "stdafx.h"
#include <iostream>
using namespace std;
class CStudent
{
private:
char *p_name;
int age;
public:
CStudent(char *,int);
~CStudent() {
if(p_name!=NULL)
delete p_name;
cout<<"destructor of class"<<endl;
}
void show(void);
};
CStudent::CStudent(char *source,int a)
{
p_name=new(char[strlen(source)+1]);
strcpy(p_name,source);
age=a;
}
void CStudent::show()
{
cout<<p_name<<" "<<age<<endl;
}
void main()
{
CStudent *p_student=new CStudent("张三",20);
CStudent stud1("李四",50);
CStudent stud3(stud1);
p_student->show();
stud1.show();
stud3.show();
delete p_student;
}
#include <iostream>
using namespace std;
class CStudent
{
private:
char *p_name;
int age;
public:
CStudent(char *,int);
~CStudent() {
if(p_name!=NULL)
delete p_name;
cout<<"destructor of class"<<endl;
}
void show(void);
};
CStudent::CStudent(char *source,int a)
{
p_name=new(char[strlen(source)+1]);
strcpy(p_name,source);
age=a;
}
void CStudent::show()
{
cout<<p_name<<" "<<age<<endl;
}
void main()
{
CStudent *p_student=new CStudent("张三",20);
CStudent stud1("李四",50);
CStudent stud3(stud1);
p_student->show();
stud1.show();
stud3.show();
delete p_student;
}
出现这个状况 那位帅哥帮忙解释下。
只有本站会员才能查看附件,请 登录