|
//studenti.h
#ifndef STUDENTI_H_
#define STUDENTI_H_
#include<iostream>
#include<string>
#include<valarray>
class Student:private std::string,private std::valarray<double>
{
private:
typedef std::valarray<double> ArrayDb;
std::ostream & arr_out(std::ostream & os)const;
public:
Student():std::string("Null Student"),ArrayDb() {}
Student(const std::string & s)
:std::string(s),ArrayDb() {}
explicit Student(int n)
:std::string("Nully"),ArrayDb(n) {}
Student(const std::string & s,int n)
:std::string(s),ArrayDb(n) {}
Student(const std::string & s,const ArrayDb & a)
:std::string(s),ArrayDb(a) {}
Student(const char * str,const double * pd,int n)
:std::string(str),ArrayDb(pd,n) {}
~Student() {}
double Average()const;
const std::string & Name()const;
double & operator[](int i);
double operator[](int i)const;
friend std::istream & operator>>(std::istream & is,
Student & stu);
friend std::istream & getline(std::istream & is,
Student & stu);
friend std::ostream & operator<<(std::ostream & os,
const Student & stu);
};
#endif
//student.cpp
#include"studenti.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
double Student::Average()const
{
if(ArrayDb::size()>0)
return ArrayDb::sum()/ArrayDb::size();
else
return 0;
}
const string & Student::Name()const
{
return (const string &)*this;
}
double & Student::operator [](int i)
{
return ArrayDb::operator[](i);
}
double Student::operator [](int i)const
{
return ArrayDb::operator[](i);
}
ostream & Student::arr_out(ostream & os)const
{
int i;
int lim=ArrayDb::size();
if(lim>0)
{
for(i=0;i<lim;i++)
{
os<<ArrayDb::operator[](i)<<" ";
if(i%5==4)
os<<endl;
}
if(i%5!=0)
os<<endl;
}
else
os<<" empty array ";
return os;
}
istream & operator>>(istream & is,Student & stu)
{
is>>(string &)stu;
return is;
}
istream & getline(istream & is,Student & stu)
{
getline(is,(string &)stu);
return is;
}
ostream & operator<<(ostream & os,const Student & stu)
{
os<<"Scores for "<<(const string &)stu<<":\n";
stu.arr_out(os);
return os;
}
//usestu.cpp
#include<iostream>
#include"studenti.h"
using std::cin;
using std::cout;
using std::endl;
void set(Student & sa, int n);
const int pupils=3;
const int quizzes=5;
int main()
{
Student ada[pupils]={Student(quizzes),Student(quizzes),Student(quizzes)};
int i;
for(i=0;i<pupils;i++)
set(ada[i],quizzes);
cout<<"\nStudent List:\n";
for(i=0;i<pupils;i++)
cout<<ada[i].Name()<<endl;
cout<<"\nResults:";
for(i=0;i<pupils;i++)
{
cout<<endl<<ada[i];
cout<<"average: "<<ada[i].Average()<<endl;
}
cout<<"Done.\n";
return 0;
}
void set(Student & sa,int n)
{
cout<<"Please enter the student's name: ";
getline(cin,sa);
cout<<"Please enter "<<n<<" quiz scores:\n";
for(int i=0;i<n;i++)
cin>>sa[i];
while(cin.get()!='\n')
continue;
}
这个程序和我前两天求教的一个程序基本类似,只是前面的用的是包含的类继承,这次是用的私有继承,还是完成输入3个学生的各5门功课成绩,求其各自平均分后输出,我在VC++6.0上编译出现如下错误:
Compiling...
use_stui.cpp
d:\vc源程序练习\use_stui\studenti.h(14) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(15) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(17) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(19) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(21) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(23) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
Error executing cl.exe.
use_stui.obj - 6 error(s), 0 warning(s)
难道编译器不支持私有继承?请教各位了 |
|