📄 实验2.cpp
字号:
//lab 2 constructor overloading and distructor
#include<iostream.h>
#include<iomanip.h>/*stew()*/
#include<string.h>/*strcpy()*/
class Student/*定义一个类*/
{
private:/*以下为私有变量*/
char Name[15];
float GPA;
int MAN;
public: /*以下为公有变量*/
Student(); /*构造函数的重载*/
Student(char*);
Student(char*,float,int);
~Student(); /*析构函数*/
void Showdata(); /*定义类的一个Showdata()函数*/
};
void FormatOutput(); /*定义一个格式化输出的函数*/
main()
{
Student pupil_1;/*主函数中对构造函数的调用*/
Student pupil_2("aaa");
Student pupil_3("bbb",3.5,8.0);
FormatOutput();/*主函数对格式化输出函数的调用*/
pupil_1.Showdata();/*对类中定义的Showdata()这个函数的调用*/
pupil_2.Showdata();
pupil_3.Showdata();
return 0;
}
void FormatOutput()/*对格式化输出函数的定义*/
{cout.setf(ios::showpoint);
cout.precision(2);
}
Student::Student()/*对类的构造函数重载的定义*/
{cout<<"\nConstructor initializing student record...";
strcpy(Name,"*******");
GPA=0.0;
MAN=0;
}
Student::Student(char *name)/*对类的构造函数重载的定义*/
{cout<<"\nConstructor initializing student record...";
strcpy(Name,name);
GPA=0.0;
MAN=0;
}
Student::Student(char *name,float grade,int man)/*对类的构造函数重载的定义*/
{cout<<"\nConstructor initializing student record...";
strcpy(Name,name);
GPA=grade;
MAN=man;
}
Student::~Student()/*析构函数的定义*/
{cout<<"\n\nThe destructor function has run."<<endl;
}
void Student::Showdata()/*对Showdata()函数的定义*/
{cout<<"\n\nShow data...";
cout<<"\nStudent name:"<<Name;
cout<<"\nStudent GPA:"<<GPA;
cout<<"\nStudent MAN:"<<MAN;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -