《C++Primer PLus 第五版》读书笔记3(二)

2014-11-24 10:39:59 · 作者: · 浏览: 2
( int n ) : sz_Name( "Nully" ), m_ArrScores( n )
{
}
CStudent( const string& name, int n ) : sz_Name( name ), m_ArrScores( n )
{
}
CStudent( const string& name, const ArrayDb& a ) : sz_Name( name ), m_ArrScores( a )
{
}
CStudent( const char* name, const double* pd, int n ) : sz_Name( name ), m_ArrScores( pd, n )
{
}
double Average() const
{
if ( m_ArrScores.size() > 0 )
{
return ( m_ArrScores.sum() / m_ArrScores.size() );
}
else
{
return 0;
}
}
const string& GetName() const
{
return sz_Name;
}
double& operator[]( int i)
{
return m_ArrScores[ i ];
}
double operator[]( int i ) const
{
return m_ArrScores[ i ];
}
ostream& CStudent::arr_out( ostream& os ) const
{
int i;
int lim = m_ArrScores.size();
if ( lim > 0 )
{
for ( i = 0; i < lim; i++ )
{
os << m_ArrScores[ i ] << " ";
if ( 4 == i % 5 )
{
os << endl;
}
}
if ( 0 != i % 5 )
{
os << endl;
}
}
else
{
os << "empty array";
}
return os;
}
friend istream& operator >>( istream& is, CStudent& stu );
friend istream& operator <<( istream& os, const CStudent& stu );
friend istream& getline( istream& is, CStudent& stu );
~CStudent(){};
private:
string sz_Name;
ArrayDb m_ArrScores;
};

istream& operator >>( istream& is, CStudent& stu )
{
is >> stu.sz_Name;
return is;
}

ostream& operator <<( ostream& os, const CStudent& stu )
{
os << "this student name is:" << stu.GetName() << endl;
os << "this student scores is:" << endl;
stu.arr_out( os );
return os;
}
istream& getline( istream& is, CStudent& stu )
{
getline( is, stu.sz_Name );
return is;
}

const int puplis = 3;
const int quizzes = 5;
void set( CStudent& sa, int n );

int _tmain(int argc, _TCHAR* argv[])
{
CStudent ada[ puplis ] = { CStudent( quizzes ), CStudent( quizzes ), CStudent( quizzes ) };
int i;
for ( i = 0; i < puplis; ++i )
{
set( ada[ i ], quizzes );
}
cout << "\nStudent List:" << endl;
for ( i = 0; i < puplis; ++i )
{
cout << ada[ i ].GetName() << endl;
}
cout << "\nResults:" << endl;
for ( i = 0; i < puplis; i++ )
{
cout << endl << ada[ i ];
cout << "average" << ada[ i ].Average() << endl;
}
cout << "Done." << endl;

return 0;
}

void set( CStudent& sa, int n )
{
cout << "Please enter the student name:";
getline( cin, sa );
cout << "Please enter " << n << "quiz scores:" << endl;
for ( int i = 0; i < n; i++ )
{
cin >> sa[ i ];
}
while( '\n' != cin.get() )
{
continue;
}
}

私有继承
c++还有另一种实现has-a关系的途径----私有继承。使用私有继承,基类的公有成员和保护成员都将成为派生类的私有成员。这意味着基类方法将不会成为派生对象公有接口的一部分,但可以在派生类的成员函数中使用它们。
使用公有继承,基类的公有方法将成为派生类的公有方法。简而言之,派生类将继承基类的接口,这是is-a关系的一部分。使用私有继承,基类的公有方法将成为派生类的私有方法。简而言之,派生类不能继承基类的接口。正如从被包含对象中看到的,这种不完全继承是has-a关系的一部分。
因此私有继承提供的特性与包含相同:获得实现,但不获得接口。所以,私有继承也可以用来实现has-a关系。
[cpp]
#include "stdafx.h"
#include "iostream"
using namespace std;
#include "valarray"
#include "string"

class CStudent : private valarray, private string
{
private:
typedef std::valarray ArrayDb;
public:
CStudent() : string( "Null Student" ), ArrayDb()
{
}
CStudent( const string& name ) : string( name ), ArrayDb()
{
}
explicit CStudent( int n ) : string( "Nully" ), ArrayDb( n )
{
}
CStudent( const string& name, int n ) : string( name ), ArrayDb( n )
{
}
CStudent( const string& name, const ArrayDb& a ) : string( name ), ArrayDb( a )
{
}
CStudent( const char* name, const double* pd, int n ) : string( name ), ArrayDb( pd, n )
{
}
~CStudent(){};
double Average() const