C++私有继承(第十四章)

C++还有另一种实现has-a关系的途径——私有继承。(上一个是包含)

  1. 使用私有继承,基类的公有方法将成为派生类的私有方法。派生类不继承基类接口。
  2. 使用私有继承,类将继承实现。比如从string类派生Student类,Student类可以使用string组件,可用于保存字符串。Student类可以用string方法访问string组件
  3. 包含将对象作为一个命名成员添加到类中,私有继承将对象作为未命名的继承对象添加到类中。
  4. 用关键字private来声明类
类声明
#include 
#include
#include
using std::string;
class Student:private string, private std::valarray
{
    private:
        typedef std::valarray AR;
        std::ostream& arr_out(std::ostream& os) const;
    public:
        Student() : string("No"),AR(){}
        explicit Student(const string& s):string(s),AR() {}
        explicit Student(int n): string("Jeff"),AR(n){}
        Student(const string& s,int n):string(s),AR(n){}
        Student(const char* str,const double* pd, int n)
        :string(str),AR(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& s);
        friend std::istream& getline(std::istream& is,Student& s);
        friend std::ostream& operator<<(std::ostream& os,const Student& s);
};
  1. 包含中private部分有string命名对象和valarray命名对象,私有继承不用这些对象,而是用类组件代替。
  2. 包含中构造函数用对象名来初始化,私有继承使用类名来初始化。(内联构造函数)
#include"studentc.h"
using std::cout; 
std::ostream& Student::arr_out(std::ostream& os) const
{
    int i;
    int lim=AR::size();
    if(lim>0)
    {
        for(i=0;i>(std::istream& is,Student& s)
{
    is>>(string& )s;
    return is;
}

std::istream& getline(std::istream& is,Student& s)
{

    for(int i=0;i>s.AR::operator[](i);
    return is;
}

std::ostream& operator<<(std::ostream& os,const Student& s)
{
    os<<"Scores for "<<(const string&)s<<": ";
    s.arr_out(os);
    return os;
}
  1. 访问基类方法:
    AD::size()这样,用类名和作用域解析运算符来调用基类方法.
    s.AD::size()该基类方法变成Student私有方法
    AD::operator[](i) 调用函数
  2. 访问基类对象
    用强制类型转换
    (const string&) *this
  3. 访问友元函数
    显示类型转换

使用包含还是私有继承?

  1. 大部分都会倾向于使用包含,易于理解,因为可以使用显式命名对象。使用继承关系会更抽象。
  2. 继承会引起很多问题,尤其是从多个基类继承时(叫多重继承)
  3. 包含能够包括多个同类的子对象,而继承则只能使用一个这样的对象。
  4. 私有继承提供的特性会比包含多,比如包含是不能访问保护对象的,但是私有继承可以,因为私有继承派生出类,而包含不是派生类。(需要使用私有继承)
  5. 另一种需要使用私有继承的是需要重新定义虚函数。

保护继承

保护继承是私有继承的变体,在列出基类时使用protected

  1. 在使用保护继承时,基类的公有成员和保护成员将成为派生类的保护成员。且基类接口也可以在派生类中使用,跟私有继承一样。
  2. 当派生类派生出另一个类。使用私有继承时,第三代类将不能使用基类的接口,因为基类的公有方法会变成派生类的私有方法。但是使用保护继承就可以使用。

using重新定义访问权限

如果要使基类的方法在派生类外使用:

  1. 定义一个使用该基类的派生类方法
double Student::sum() const
{  return std::valarray::sum() }
  1. 将函数调用包装到另一个函数调用中,使用using声明
public:
   using std::valarray::max;
    using std::valarray::operator[];

using声明只使用成员名,且只适合继承。

cout<

你可能感兴趣的:(C++私有继承(第十四章))