1.公有继承可以实现
2.包含、私有继承、保护继承用于实现has-a关系,即新的类将包含另一个类的对象。
(使用这样类成员:本身是另外一个类对象称为包含 (组合或层次化)。)
3.函数模板、类模板
valarray类构造函数举例
- double gap[5] = { 3.1,3.5,3.8,2.9,3.3 };
- valarray<double> v1; //创建1个double类型的空数组
- valarray<int> v2(8); //创建8个int类型数组
- valarray<int> v3(10,8); //创建8个int类型数组,数组中每个数都是10
- valarray<double> v4(gap,4);//取出gap数组的前四个元素用于填充v4数组
- valarray<int> v5 = { 20,32,17,9 };//C++ 11中
类方法举例:
- operator[]() : 访问各个元素
- size() : 返回包含的元素数
- sum() : 返回所有元素的总和
- max() : 返回最大的元素
- min(): 返回最小的元素
举例:每个学生的录入考试成绩 (has_a关系,学生有姓名,也有一组考试成绩)
Valarray类 包含了string类和valarray
用string对象表示学生的名字,valarray
将其声明为私有,意味着Valarray类的成员函数可以使用string和valarray
valarray.h
- #ifndef VALARRAY_H_
- #define VALARRAY_H_
- #include
- #include
- #include
- using namespace std;
-
- //14章 14.1 valarray包含成员对象的类
- class Student
- {
- private:
- typedef valarray<double> ArrayDb;
- string name;
- ArrayDb scores;//valarray
ArrayDb - public:
- Student():name("Null student"),scores(){} //成员初始化列表
- explicit Student(const string&s):name(s), scores() {} //explicit关闭隐式转换,使其只能显调用
- explicit Student(int n) :name("Nully"), scores(n) {}
- Student(const string&s,int n) :name(s), scores(n) {}
- Student(const string&s, const ArrayDb &a) :name(s), scores(a) {}
- Student(const string&s, const double *pd,int n) :name(s), scores(pd,n) {}
- ~Student(){}
-
- double Average() const; //平均成绩 不可修改
- const string &Name() const;
- double &operator[](int n); //stu[0]=100;
- double operator[](int n) const;//a=stu[0]
-
- friend istream &operator >>(istream &is, Student &stu);//友元函数重载输入输出运算符
- friend istream &getline(istream &is, Student &stu);
- friend ostream &operator<<(ostream &os, Student &stu);
- };
-
-
- #endif // !VALARRAY_H_
-
valarray.cpp
- #include "valarray.h"
-
- double Student::Average() const
- {
- if (scores.size() > 0)
- return scores.sum() / scores.size();
- else
- return 0.0;
- }
-
- const string & Student::Name() const
- {
- return name;
- }
-
- double & Student::operator[](int n)
- {
- return scores[n];
- }
-
- double Student::operator[](int n) const
- {
- return scores[n];
- }
-
- 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 & os, Student & stu)
- {
- os << "Scores for" << stu.name << ":" << endl;//显示学生的姓名和各科分数
- int i;
- int lim = stu.scores.size();
- if (lim > 0)
- {
- for ( i = 0; i < lim; i++)
- {
- os << stu.scores[i] << " ";
- if (i % 5 == 4)
- os << endl;
- }
- if (i % 5 != 0)
- os << endl;
- }
- else
- os << "Empty array" << endl;
- return os;
-
- }
-
-
main.cpp
- #include
- #include "valarray.h"
-
- using namespace std;
- const int pupils = 3; //人数
- const int quizzes = 5;//每个人都有5门成绩
- void set(Student &sa, int n);
- int main()
- {
- Student ada[pupils] = { Student(quizzes),Student(quizzes) ,Student(quizzes) };
- int i;
- for (i = 0; i < pupils; i++)
- set(ada[i], quizzes);
-
- cout << "\n Student List:" << endl;
- for (i = 0; i < pupils; i++)
- cout << ada[i].Name() << endl;//显示每个学生的姓名
-
- cout << "\n Result List:" << endl;
- for (i = 0; i < pupils; i++)
- cout << ada[i];
- cout << "Average:" << ada[i].Average() << endl;
- return 0;
- }
-
- void set(Student &sa, int n)
- {
- cout << "Please enter the student's name:";
- getline(cin, sa);
- cout << "Please enter:" << n << "quiz scores:" << endl;
- for (int i = 0; i < n; i++)
- cin >> sa[i];
- while (cin.get() != '\n');
- }
-
-
-
运行结果:

使用私有继承,基类的公有方法将成为派生类的私有方法。
包含和私有继承的不同:
1.包含提供了2个被显示命名的对象成员(name和scores),而私有继承提供了2个无名的子对象成员。
2.私有继承的构造函数使用初始化成员列表语法,它使用类名而不是成员名来标识构造函数。
包含:使用成员名name和scores
Student(const string&s, const double *pd,int n) :name(s), scores(pd,n) {}
私有继承:使用string和ArrayDb
Student(const string&s, const double *pd,int n) :string(s), ArrayDb(pd,n) {}
修改包含代码使其变为私有继承的方式:
1.访问基类的方法
包含:使用valarray的 方法,如size()和sum(),使用对象名调用方法
- double Student::Average() const
- {
- if (scores.size() > 0)
- return scores.sum() / scores.size();
- else
- return 0.0;
- }
私有继承(只能在派生类的方法中使用基类的方法):使用类名和作用域与作用域运算符来调用基类的方法
- double Student::Average() const
- {
- if (ArrayDb::size() > 0) //访问基类的方法 使用类名+作用域解析运算符
- return ArrayDb::sum() / ArrayDb::size();
- else
- return 0.0;
- }
2.访问基类对象
包含:name为string类对象
- const string & Student::Name() const
- {
- return name;
- }
私有继承:string对象没有名称。Student类的代码如何访问string对象?使用强制类型转换
- const string & Student::Name() const
- {
- return (const string &)*this; //访问基类对象 通过强制类型转换
- }
*this为调用方法的对象。返回一个引用,该引用指向用于该方法的Student对象中的继承而来的string对象。
3.访问基类的友元函数
类名显示限定函数名不适用于友元函数,因为友元函数不是成员函数,不属于类
包含:
- ostream & operator<<(ostream & os, Student & stu)
- {
- os << "Scores for" << stu.name << ":" << endl;//显示学生的姓名和各科分数
- ....
- }
私有继承:使用显示转换 为基类调用正确的函数
- ostream & operator<<(ostream & os, const Student & stu)
- {
- os << "Scores for" << (string &)stu << ":" << endl;//显示学生的姓名和各科分数
- .....
- }
整体代码:
valarray.h
- #ifndef VALARRAY_H_
- #define VALARRAY_H_
- #include
- #include
- #include
- using namespace std;
-
- //14章 14.2私有继承
- class Student:private string,private valarray<double> //私有继承
- {
- private:
- typedef valarray<double> ArrayDb;
- // string name;
- // ArrayDb scores;//valarray
ArrayDb - public:
- Student():string("Null student"), ArrayDb(){} //成员初始化列表
- explicit Student(const string&s):string(s), ArrayDb() {}
- explicit Student(int n) :string("Nully"), 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 string&s, const double *pd,int n) :string(s), ArrayDb(pd,n) {}
- ~Student(){}
-
- double Average() const; //平均成绩 不可修改
- const string &Name() const;
- double &operator[](int n); //stu[0]=100;
- double operator[](int n) const;//a=stu[0]
-
- friend istream &operator >>(istream &is, Student &stu);
- friend istream &getline(istream &is, Student &stu);
- friend ostream &operator<<(ostream &os,const Student &stu);
- };
-
-
- #endif // !VALARRAY_H_
-
valarray.cpp
- #include "valarray.h"
-
- double Student::Average() const
- {
- if (ArrayDb::size() > 0) //访问基类的方法 使用类名+作用域解析运算符
- return ArrayDb::sum() / ArrayDb::size();
- else
- return 0.0;
- }
-
- const string & Student::Name() const
- {
- return (const string &)*this; //访问基类对象 通过强制类型转换
- }
-
- double & Student::operator[](int n)
- {
- return ArrayDb::operator[](n);
- }
-
- double Student::operator[](int n) const
- {
- return ArrayDb::operator[](n); //访问基类的方法 使用类名+作用域解析运算符
- }
-
- 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" << (string &)stu << ":" << endl;//显示学生的姓名和各科分数
- int i;
- int lim = stu.ArrayDb::size();
- if (lim > 0)
- {
- for ( i = 0; i < lim; i++)
- {
- os << stu.ArrayDb::operator[](i) << " ";
- if (i % 5 == 4)
- os << endl;
- }
- if (i % 5 != 0)
- os << endl;
- }
- else
- os << "Empty array" << endl;
- return os;
-
- }
-
-
main.cpp
- #include
- #include "valarray.h"
-
- using namespace std;
- const int pupils = 3; //人数
- const int quizzes = 5;//每个人都有5门成绩
- void set(Student &sa, int n);
- int main()
- {
- Student ada[pupils] = { Student(quizzes),Student(quizzes) ,Student(quizzes) };
- int i;
- for (i = 0; i < pupils; i++)
- set(ada[i], quizzes);
-
- cout << "\n Student List:" << endl;
- for (i = 0; i < pupils; i++)
- cout << ada[i].Name() << endl;//显示每个学生的姓名
-
- cout << "\n Result List:" << endl;
- for (i = 0; i < pupils; i++)
- cout << ada[i];
- cout << "Average:" << ada[i].Average() << endl;
- return 0;
- }
-
- void set(Student &sa, int n)
- {
- cout << "Please enter the student's name:";
- getline(cin, sa);
- cout << "Please enter:" << n << "quiz scores:" << endl;
- for (int i = 0; i < n; i++)
- cin >> sa[i];
- while (cin.get() != '\n');
- }
-
-
-
结果:

1.假设类包含保护成员(可以是数据成员也可以是成员函数),则这样的成员在派生类中是可用的,但在继承层次外是不可用的。使用组合将这样的类包含在另一个类中,则后者将不是派生类,而是继承层次之外,因此不能访问保护成员。 但是通过私有继承得到的将是派生类,因此能够访问保护成员。
2.当需要重新定义虚函数。派生类可以重新定义虚函数,但包含不可以,使用私有继承,重新定义的函数将只能在类中使用,而不是公有的。
通常:应使用包含来建立has-a关系;如果新类需要访问原有类的保护成员,或需要重定义一个虚函数,则使用私有继承。