#include<iostream>
usingnamespacestd;
//类的声明
class Student{
private://私有的
char*m_name;
intm_age;
floatm_score;
public://共有的
voidsetname(char*name);
voidsetage(intage);
voidsetscore(floatscore);
voidshow();
};
//成员函数的定义
voidStudent::setname(char*name){
m_name=name;
}
voidStudent::setage(intage){
m_age=age;
}
voidStudent::setscore(floatscore){
m_score=score;
}
voidStudent::show(){
cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
}
intmain(){
//在栈上创建对象
Student stu;
stu.setname("小明");
stu.setage(15);
stu.setscore(92.5f);
stu.show();
//在堆上创建对象
Student *pstu=newStudent;
pstu->setname("李华");
pstu->setage(16);
pstu->setscore(96);
pstu->show();
return0;
}
网友评论