一、組合
(1)組合是has-a的關係,即創建一個包含其他類對象的類。
(2)使用公有繼承時,類可以獲得接口,但使用組合時,類可以獲得實現,但不能獲得接口,一般通過實現來調用類的方法。
(3)將typedef放在類定義的私有部分意味可以在類中使用,explicit可以關閉隱式轉換,但可以顯示調用。用const可以限制數據的修改。
關於組合的初始化和約束:
1、初始化被包含的對象
對於繼承的對象,構造函數在成員初始化列表使用類名調用特定的構造函數。初始化成員對象,在初始化列表中使用對象名。
不使用初始化列表,將調用相應的默認的構造函數。
2、使用被包含的對象的接口
被包含對象的接口不是公有的,但可以在類方法中使用它們。
關於組合的例子:
//student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <iostream>
#include <string>
#include <valarray>
using namespace std;
class Student
{
private:
typedef valarray<double> ArrayDb;
string name;
ArrayDb scores;
ostream & arr_out(ostream &os) const;
public:
Student():name("Null Student"),scores(){}
explicit Student(const string &s):name(s),scores() {}
explicit Student(int n):name("NUll"),scores(n) {}
Student(string &s,const ArrayDb &a):name(s),scores(a){ }
Student(const string &s,int n):name(s),scores(n) {}
Student(const char *str,const double *pd,int n):name(str),scores(pd,n) {}
~Student() { }
double Average() const;
const string &Name() const;
double &operator[](int i);
double operator[](int i) const;
friend istream &getline(istream &in,Student &stu);
friend istream &operator>>(istream &in,Student &stu);
friend ostream &operator<<(ostream &out,const Student &stu);
};
#endif
//student.cpp
#include "student.h"
#include <iostream>
using namespace std;
double Student::Average() const
{
if(scores.size() > 0)
{
return scores.sum() /scores.size(); //包含類對象時,在類方法中可以調用類對象的方法。
}
else
{
return 0;
}
}
const string &Student::Name() const
{
return name;
}
double &Student::operator[](int i)
{
return scores[i];
}
double Student::operator[](int i) const
{
return scores[i];
}
ostream &Student::arr_out(ostream &os) const
{
int i;
int lim = scores.size();
if(lim > 0)
{
for(i = 0; i < lim; i++)
{
os << scores[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 >> stu.name;
return is;
}
istream &getline(istream &is,Student &stu)
{
getline(is,stu.name);
return is;
}
ostream &operator<<(ostream &out,const Student &stu)
{
out << "Scores for " << stu.name << endl;
stu.arr_out(out);
return out;
}
//main.cpp
#include "student.h"
#include <iostream>
using namespace std;
void set(Student &s,int n);
const int pup = 3;
const int qui = 5;
int main()
{
Student ada[3] = {Student(5),Student(5),Student(5)};
int i;
for(int i= 0; i < 3; ++i)
{
set(ada[i],5);
}
cout << "\nStudent List : \n";
for(i = 0 ; i < 3; i++)
{
cout << endl << ada[i];
cout << "average: " << ada[i].Average() << endl;
}
cout << "Done.\n";
return 0;
}
void set(Student &s,int n)
{
cout << "Please enter ther student's name:";
getline(cin,s);
cout << "Please enter " << n << "quiz scores" << endl;
for(int i = 0; i < n; i++)
{
cin >> s[i];
}
while(cin.get() != '\n')
continue;
}
二、私有繼承(實現has-a的關係)
使用私有繼承時,基類的公有成員或者保護成員將成爲派生類的私有成員。
關於私有繼承實現has-a關係的語法說明:
1、初始化基類組件
使用初始化列表,使用類名而不是使用對象名來表示構造函數。
2、只能在派生類的方法中使用基類的方法,使用類名和作用域限定符來調用基類的方法。
3、訪問基類對象
由於基類的對象沒有名稱,但this指針指向用來調用的對象,因此使用強制類型轉換轉換,將派生類指針轉換爲基類指針。
4、訪問基類的友元函數
顯示的將派生類對象轉換爲基類的引用或者指針,進而調用友元函數。
在多重繼承中,顯示調用可以防止二義性的產生,導致的遞歸調用。
問題:關於使用包含還是私有繼承?
通常,應使用包含來建立has-a的關係,如果新類需要訪問原有類的保護成員,或者重新定義許函數,則應使用私有繼承。
//使用私有繼承的Student的類
//Student.h
ifndef _STUDENT_H_
#define _STUDENT_H_
#include <iostream>
#include <valarray>
#include <string>
using namespace std;
class Student:private string,private valarray<double>
{
private:
typedef valarray<double>ArrayDb;
ostream &arr_out(ostream &os) const;
public:
Student():string("NUll Student"),ArrayDb()
{
}
explicit Student(const string &s):string(s),ArrayDb()
{
}
explicit Student(int n):string("Null Student"),ArrayDb(n) // 關閉隱式轉換
{
}
Student(const string &s,int n):string(s),ArrayDb(n)
{
}
Student(const string &s,const ArrayDb &a):string(s),ArrayDb(a)
{
}
Student(const char *str,const double *pd,int n):string(str),ArrayDb(pd,n)
{
}
~Student()
{
}
double Average() const;
double &operator[](int i);
double operator[](int i)const;
const string &Name() const;
friend istream &operator>>(istream &is,Student &stu);
friend istream &getline(istream &is,Student &stu);
friend ostream &operator<<(ostream &out,const Student &stu);
};
#endif
//student.cpp
#include "student.h"
#include <iostream>
using namespace std;
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;
}
//main.cpp
#include <iostream>
#include "student.h"
using namespace std;
void set(Student &sa,int n)
{
cout << "Please enter the student's name: ";
getline(cin,sa);
cout << "Please enter" << n << "quzi scores:" << endl;
for(int i = 0; i < n ; i++)
{
cin >> sa[i];
}
while(cin.get() != '\n')
continue;
}
int main()
{
Student ada[3] = {Student(5),Student(5),Student(5)};
int i;
for(i = 0; i < 3; i++)
{
set(ada[i],5);
}
cout << "\nStudent List:" << endl;
for(i = 0; i < 3;i++)
{
cout << ada[i].Name() << endl;
}
cout << "\nResults:";
for(i = 0; i < 3;i++)
{
cout << endl << ada[i];
cout << "average: " << ada[i].Average() << endl;
}
cout << "Done.\n";
return 0;
}