?? student.cpp
字號:
#include "student.h"
#include <iostream>
using namespace std;
//default constructor
Student::Student()
{
name = new char[3];
strcpy(name,"no");
snumber = new char[3];
strcpy(snumber,"no");
age = 0;
sex = 'n';
chinese = 0;
math = 0;
english = 0;
set_tot();
set_ava();
}
Student::Student(char *n,char *sn, int a, char s,int y,int m ,int e )
{
name = new char[strlen(n) + 1];
strcpy(name , n);
snumber = new char[strlen(sn) +1];
strcpy(snumber , sn);
age = a;
sex = s;
chinese = y;
math = m;
english = e;
set_tot();
set_ava();
}
Student::Student(const Student & st)
{
name = new char[strlen(st.name) + 1];
strcpy(name, st.name);
snumber = new char[strlen(st.snumber) +1];
strcpy(snumber, st.snumber);
age = st.age;
sex = st.sex;
chinese = st.chinese;
math = st.math;
english = st.english;
set_tot();
set_ava();
}
Student::~Student()
{
delete [] name;
delete [] snumber;
}
//set the value of members of the class
void Student::set_name(const char *n)
{
delete [] name;
name = new char[strlen(n) + 1];
strcpy(name , n);
}
void Student::set_sn(char *sn)
{
delete [] snumber;
snumber = new char[strlen(sn) +1];
strcpy(snumber , sn);
}
void Student::set_age(int a)
{
age = a;
}
void Student::set_sex(char s)
{
sex = s;
}
void Student::set_chinese(int yw)
{
chinese = yw;
set_tot();
set_ava();
}
void Student::set_math(int m)
{
math = m;
set_tot();
set_ava();
}
void Student::set_english(int e)
{
english = e;
set_tot();
set_ava();
}
void Student::set_pfm(int y, int m, int e)
{
chinese = y;
math = m;
english = e;
set_tot();
set_ava();
}
//get the vaule of the members of the class
char * Student::get_n()const
{
return name;
}
char * Student::get_sn()const
{
return snumber;
}
int Student::get_age()const
{
return age;
}
char Student::get_sex()const
{
return sex;
}
int Student::get_chinese()const
{
return chinese;
}
int Student::get_math()const
{
return math;
}
int Student::get_english()const
{
return english;
}
int Student::get_tot()const
{
return total;
}
double Student::get_ava()const
{
return avag;
}
//show methods
void Student::showall()const
{
cout<<snumber<<"\t"<<name<<" \t"
<<age<<" \t "<< sex <<"\t" << chinese
<< "\t" << math << "\t" << english
<< "\t" << total << "\t" << avag <<endl;
}
void Student::showpfm()const
{
cout << "\t" << chinese
<< "\t" << math << "\t" << english
<< "\t" << total << "\t" << avag <<endl;
}
/*istream & operator >>(istream & is, Student & stu)
{
char n[40];
char sn[15];
cout << "請輸入學號:";
cin.getline(sn,15);
cout << "請輸入姓名:";
cin.getline(n,40);
cout << "請輸入年齡:";
cin >> stu.age;
}*/
Student & Student::operator =(const Student & stu)
{
if(this == &stu)
return *this;
delete [] name;
delete [] snumber;
name = new char[strlen(stu.name) + 1];
strcpy(name , stu.name);
snumber = new char[strlen(stu.snumber) + 1];
strcpy(snumber,stu.snumber);
age = stu.age;
sex = stu.sex;
chinese = stu.chinese;
math = stu.math;
english = stu.english;
set_tot();
set_ava();
return *this;
}
int Student::get_key(int choice)
{
if(choice==1)
return get_chinese();
else if(choice==2)
return get_math();
else if(choice==3)
return get_english();
else
return get_tot();
}
void Student::outfilestream(ofstream &ofs)
{
ofs<<age<<"\t"<<avag<<"\t"<<chinese<<"\t"<<english<<"\t"<<math<<"\t"<<name<<"\t"<<sex<<"\t"<<snumber<<"\t"<<total<<endl;
}
void Student::infilestream(ifstream &ifs)
{
ifs>>age>>avag>>chinese>>english>>math>>name>>sex>>snumber>>total;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -