你好,可不可帮我看一下这道程序,怎么改?说一下理由,谢谢!
#include<iostream>#include<sstream>usingnamespacestd;classStudent{char*name;charthe_c...
#include<iostream>
#include<sstream>
using namespace std;
class Student
{
char *name;
char the_class[12];
public:
Student():name(NULL){the_class[0]='\0';}
Student(char *name,char the_class[]);
~Student(){delete []name;}
const char *getName()const {return name;}
const char *getClass()const{return the_class;}
void toUpper(){strdup(name);}
string toString()const;
Student& operator=(const Student &st);
};
Student::Student(char *name,char the_class[])
{
this->name=strdup(name);
strcpy(this->the_class,the_class);
}
string Student::toString()const
{
ostringstream ost;
ost<<"Name:"<<name<<"Class:"<<the_class<<" ";
return ost.str();
}
Student & Student::operator=(const Student &st)
{
if(&st==this) return *this;
if(name!=NULL) delete []name;
name=strdup(st.name);
strcpy(the_class,st.the_class);
return *this;
}
int main()
{
Student a("Jim","071103"),b("Mary","051208");
cout<<"赋值前:"<<endl;
cout<<a.toString()<<b.toString();
a=b;
cout<<"赋值后:"<<endl;
cout<<a.toString()<<b.toString();
a.toUpper();
cout<<"第一个学生的姓名改为大写后:"<<endl;
cout<<a.toString()<<b.toString();
return 0;
} 展开
#include<sstream>
using namespace std;
class Student
{
char *name;
char the_class[12];
public:
Student():name(NULL){the_class[0]='\0';}
Student(char *name,char the_class[]);
~Student(){delete []name;}
const char *getName()const {return name;}
const char *getClass()const{return the_class;}
void toUpper(){strdup(name);}
string toString()const;
Student& operator=(const Student &st);
};
Student::Student(char *name,char the_class[])
{
this->name=strdup(name);
strcpy(this->the_class,the_class);
}
string Student::toString()const
{
ostringstream ost;
ost<<"Name:"<<name<<"Class:"<<the_class<<" ";
return ost.str();
}
Student & Student::operator=(const Student &st)
{
if(&st==this) return *this;
if(name!=NULL) delete []name;
name=strdup(st.name);
strcpy(the_class,st.the_class);
return *this;
}
int main()
{
Student a("Jim","071103"),b("Mary","051208");
cout<<"赋值前:"<<endl;
cout<<a.toString()<<b.toString();
a=b;
cout<<"赋值后:"<<endl;
cout<<a.toString()<<b.toString();
a.toUpper();
cout<<"第一个学生的姓名改为大写后:"<<endl;
cout<<a.toString()<<b.toString();
return 0;
} 展开
2个回答
中智咨询
2024-08-28 广告
2024-08-28 广告
在当今竞争激烈的商业环境中,企业需要不断提高自身的竞争力,以保持市场份额和增加利润。通过人效提升,企业可以更有效地利用有限的资源,提高生产力和效益,从而实现盈利目标。中智咨询提供全方位的组织人效评价与诊断、人效提升方案等数据和管理咨询服务。...
点击进入详情页
本回答由中智咨询提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询