bupt c++慕课测试与作业7章
1设计学生类,数据成员包括学号、姓名、年龄、成绩;成员函数有构造函数、析构函数。定义带默认参数值的构造函数,默认值为:2021001,“Lili”,19,89.5。定义析构函数,析构时输出:学号,姓名,以及提示字符串“~~~”。编写主程序测试代码,定义一个不带参数的对象,读取用户输入信息作为构造函数的参数再定义一个对象。
#include <iostream>
using namespace std;
class student
{ public:
student(int l=2021001,string n="Lili",int a=19,float s=89.5)
{
id=l;
name=n;
age=a;
score=s;
}
~student()
{
cout<<id<<','<<name<<','<<"~~~"<<endl;
}
private:
int id;
string name;
int age;
float score;
};
int main()
{ student s1;
int id;
string name;
int age;
float score;
cin>>id>>name>>age>>score;
student s2(id,name,age,score);
return 0;
}
1设计学生类,数据成员包括学号、姓名、年龄、成绩;成员函数有构造函数、学生信息输出函数。定义带默认参数值的构造函数,默认值为:2021001,“Lili”,19,89.5。编写一个普通函数,以学生对象作为函数参数,实现学生信息输出。编写主程序测试代码,定义一个不带参数的对象,调用类成员函数输出学生信息,再调用普通函数输出学生信息,对比两种函数形式的区别。
#include <iostream>
using namespace std;
class student
{ public:
student(int l=2021001,string n="Lili",int a=19,float s=89.5)
{
id=l;
name=n;
age=a;
score=s;
}
int show_stu()
{
cout<<id<<','<<name<<','<<age<<','<<score<<endl;
}
int get_id()
{
return id;
}
string get_name()
{
return name;
}
int get_age()
{
return age;
}
float get_score()
{
return score;
}
~student()
{
}
private:
int id;
string name;
int age;
float score;
};
int show_student(student s)
{
cout<<s.get_id()<<','<<s.get_name()<<','<<s.get_age()<<','<<s.get_score()<<endl;
}
int main()
{ student s1;
s1.show_stu();
show_student(s1);
return 0;
}
更多推荐



所有评论(0)