main.cpp
#include"Stud.h"
#include"Stud.cpp"
using namespace std ;
int main()
{
Stud s1,s2(1101 ,"xiaoming" ,20 ,'m' ,99.8 );
s1.set_age(21 );
s1.display();
s2.display();
return 0 ;
}
Stud.h
#ifndef _STUD_H
#define _STUD_H
#include<iostream>
using namespace std;
class Stud{
int id;
string name;
int age;
char sex;
float grade;
public :
Stud ();
Stud(int i,string na,int ag,char se,float gr);
void set_age(int a);
void display();
};
#endif
Stud.cpp
#include"Stud.h"
using namespace std ;
Stud::Stud(){
id=0 ;
name='0' ;
age=0 ;
sex='f' ;
grade=0 ;
}
Stud::Stud(int i,string na,int ag,char se,float gr){
id=i;
name=na;
age=ag;
sex=se;
grade=gr;
}
void Stud::set_age(int a){
age=a;
}
void Stud::display(){
cout <<"ID:" <<id<<endl<<"Name:" <<name<<endl<<"Age:" <<age<<endl<<"Sex:" <<sex<<endl<<"Grade:" <<grade<<endl<<endl;
}
运行结果
ID :0
Name :0
Age :21
Sex :f
Grade :0
ID :1101
Name :xiaoming
Age :20
Sex :m
Grade :99 .8