一道C++题目
#include<iostream>usingnamespacestd;classstudent{private:intsid;char*name;intage;floa...
#include<iostream>
using namespace std;
class student
{
private:
int sid;
char *name;
int age;
float score;
static int num;
public:
student();
student(int si,char *pname,int a,float s);
~student();
void display();
};
int student::num=0;
student::student()
{
++num;
cout<<"constructor1"<<endl;
cout<<"num="<<num<<endl;
sid=0;
name=new char[sizeof("zhangmeng")];
name="zhangmeng";
score=0;
}
student::student(int si, char *pname, int a, float s)
{
++num;
cout<<"constructor2"<<endl;
cout<<"num="<<num<<endl;
sid=si;
name=new char[strlen(pname)+1];
strcpy(name,pname);
age=a;
score=s;
}
student::~student()
{
--num;
cout<<"destructor"<<endl;
cout<<"num="<<num<<endl;
delete []name;
}
void student::display()
{
cout<<"id:"<<sid<<endl;
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"score:"<<score<<endl;
}
int main()
{
student x1(1,"zhangmeng",20,90.2);
x1.display();
getchar();
return 0;
}
这个题目有什么问题啊 。。。。 问题忘记打了 展开
using namespace std;
class student
{
private:
int sid;
char *name;
int age;
float score;
static int num;
public:
student();
student(int si,char *pname,int a,float s);
~student();
void display();
};
int student::num=0;
student::student()
{
++num;
cout<<"constructor1"<<endl;
cout<<"num="<<num<<endl;
sid=0;
name=new char[sizeof("zhangmeng")];
name="zhangmeng";
score=0;
}
student::student(int si, char *pname, int a, float s)
{
++num;
cout<<"constructor2"<<endl;
cout<<"num="<<num<<endl;
sid=si;
name=new char[strlen(pname)+1];
strcpy(name,pname);
age=a;
score=s;
}
student::~student()
{
--num;
cout<<"destructor"<<endl;
cout<<"num="<<num<<endl;
delete []name;
}
void student::display()
{
cout<<"id:"<<sid<<endl;
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"score:"<<score<<endl;
}
int main()
{
student x1(1,"zhangmeng",20,90.2);
x1.display();
getchar();
return 0;
}
这个题目有什么问题啊 。。。。 问题忘记打了 展开
3个回答
2012-03-26
展开全部
#include<iostream> using namespace std; class Ex { public: Ex(char *s) { len=strlen(s); p=new char[len+1]; strcpy(p,s); } Ex() { p=new char[8]; cout<<"****"<<endl; } Ex(const Ex& st) { len=sizeof(st); p=new char[len+1]; strcpy(p,st.p); } ~Ex() { delete []p; } void outdata(void) { cout<<&len<<","<<len<<&p<<","<<p<<endl; } private: int len; char *p; }; int main() { Ex x("first"); Ex y=x,z; x.outdata(); y.outdata(); return 0; }
~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询