C++练习1

#include <iostream>
#include
<cstring>

using namespace std;

class NameString{
private:
char *str;
public:
NameString(
char *s="...")
{
str
= new char[strlen(s)+1];
strcpy(str,s);
// cout<<"构造 NameString OK!"<<endl;
}

void print()
{
cout
<<str;
}

~NameString()
{
// cout<<"析构 NameString OK!"<<endl;
delete str;
}
};

class Student{
private:
int StudentNO;
NameString Name;
int Scores;
static int total_Scores;
static int total_Num;
public:
Student(
int a,char *b,int c):StudentNO(a),Name(b),Scores(c)
{
total_Scores
+= c;
total_Num
++;
// cout<<"构造 Student OK!"<<endl;
}

void print()
{
cout
<<StudentNO<<'\t';
Name.print();
cout
<<'\t'<<Scores<<endl;
}

void average()
{
cout
<<"Average:"<<total_Scores/total_Num<<endl;
}

static void total_disp()
{
cout
<<"Total_Scores:"<<total_Scores<<endl;
cout
<<"Total_Num:"<<total_Num<<endl;
}

};

int Student::total_Scores = 0;
int Student::total_Num = 0;

int main()
{
Student st1(
1,"wen",94);
Student st2(
2,"hao",100);
Student st3(
3,"lin",100);

cout
<<"学号"<<'\t'<<"姓名"<<'\t'<<"成绩"<<endl;
st1.print();
st2.print();
st3.print();

Student::total_disp();

st1.average();

return 0;

}

posted @ 2011-05-17 19:17  hnrainll  阅读(223)  评论(0编辑  收藏  举报