#include<iostream>
#include<cstring>
using namespace std;
struct Student
{
public:
Student(char *name1,char *stu_no1,float score1);
~Student();
void modify(float score);
void show();
private:
char *name;
char *stu_no;
float score;
};
Student::Student(char *name1,char *stu_no1,float score1)
{ //因为strlen返回的是字符串所占的个数 并不包括‘/0’;
name=new char[strlen(name1)+1];//new为具有name1+1个char型元素分配了内存空间,并将首地址赋给了指针name,后面加上的1是‘/0’所占空间,即结束符
strcpy(name,name1);//将后者所指空间中的内容复制到前者所指的空间中
stu_no=new char[strlen(stu_no1)+1];
strcpy(stu_no,stu_no1);
score=score1;
}
Student::~Student()
{
delete []name;
delete []stu_no;
}
void Student::show()
{
cout<<"name: "<<name<<endl;//cout 对于运算符<<重载,表示输出从char*所指向的首地址到结束符前面的内容
cout<<"stu_no: "<<stu_no<<endl;
cout<<"score: "<<score<<endl;
}
void Student::modify(float score1)
{
score=score1;
}
int main()
{
Student stu1("liming","20080201",90);
stu1.show();
stu1.modify(80);
stu1.show();
return 0;
}