曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 2026|回复: 0
打印 上一主题 下一主题

私有继承

[复制链接]

716

主题

734

帖子

2946

积分

超级版主

Rank: 9Rank: 9Rank: 9

积分
2946
跳转到指定楼层
楼主
发表于 2013-12-12 13:37:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
私有继承也是实现 has-a 关系的一种途径。使用私有继承,基类的公有成员和保护成员都将成为派生类的私有成员。也就是说,基类的方法不会成为派生类对象的公有接口的一部分,但可以在派生类的成员函数中使用它们

使用私有继承,类将继承实现。

包含将对象作为一个命名的成员添加到类中,而私有继承是将对象作为一个未命名的继承对象添加到类中。

私有继承和包含具有相同的特性:获得实现,但不获得接口。

使用 private 来使用私有继承:
class Student : private std::string, private std::valarray<double>
{
  public:
       ...
};

实际上,private 是默认值,如果省略,那么也会实现私有继承。这里同时继承了两个基类,此行为称为多重继承(multiple inheritance, MI) 。

初始化基类组件

在“包含”关系的构造函数里,在成员初始化列表中直接使用被包含的对象并对其初始化:
  1. Student(const char *str, const double *pd, int n) : name(str), scores(pd, n) {}
复制代码
其中,name 和 scores 是被包含的对。

对于继承类的构造函数,在成员初始化列表中,使用的是类名,而不是成员名:
  1. Student(const char *str, const double *pd, int n) : std::string(str), std::valarray<double>(pd, n) {}
复制代码
下面给出了完整的 Student 类的定义(studenti.h):
[C++] 纯文本查看 复制代码
// studenti.h -- defining a Student class using private inheritance
#ifndef STUDENTC_H_
#define STUDENTC_H_

#include <iostream>
#include <valarray>
#include <string>   
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() {}
    explicit 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;
    double & operator[](int i);
    double operator[](int i) const;
    const std::string & Name() const;
// 友元
    // 输入
    friend std::istream & operator>>(std::istream & is,
                                     Student & stu);  // 1 word
    friend std::istream & getline(std::istream & is,
                                  Student & stu);     // 1 line
    // 输出
    friend std::ostream & operator<<(std::ostream & os,
                                     const Student & stu);
};

#endif



访问基类的方法
使用私有继承时,只能在派生类的方法中使用基类的方法,它能够使用类名和作用域解析运算符来调用基类的方法:
  1. double Student::Average() const
  2. {
  3.     if (ArrayDb::size() > 0)
  4.         return ArrayDb::sum()/ArrayDb::size();  
  5.     else
  6.         return 0;
  7. }
复制代码
其中,ArrayDB 是由 typedef std::valarray<double> ArrayDb; 来定义的。

总之,在“包含”时使用对象名来调用方法,在使用私有继承时使用类名和作用域解析运算符来调用方法。

访问基类对象
使用作用域解析运算符可以访问基类的方法,但如果要使用基类对象本身,那又该如何呢?

上面说过,在私有继承时,string 对象没有名称,那么 Student 类的代码如何访问内部的 string 对象呢?答案是,使用强制类型转换

由于 Student 类是从 string 类派生而来,因此可以通过强制类型转换,将 Student 对象转换为 string 对象。

指针 this 指向用来调用方法的对象,因此 *this 就表示用来调用方法的对象。看下面 Studnet 类中的 Name() 方法的定义:
  1. const string & Student::Name() const
  2. {
  3.     return (const string &) *this;
  4. }
复制代码
该方法返回一个引用,该引用指向用于调用该方法的 Student 对象中继承而来的的 string 对象。

访问基类的友元函数
用类名显式地限定函数名不适合于友元函数,因为友元并不属于类。然而,可以通过显式地转换为基类来调用正确的函数,如下面的友元函数的定义:
  1. ostream & operator<<(ostream & os, const Student & stu)
  2. {
  3.    os << "Scores for " << (const String &) stu << ":\n";
  4.    ...
  5. }
复制代码
假设 plato 是一个 Student 对象,那么下面的语句将调用上面的函数,stu 将是指向 plato 的引用,os 则是指向 cout 的引用:
  1. cout << plato;
复制代码
语句  os << "Scores for " << (const String &) stu << ":\n"; 显式地将 stu 转换为 string 对象的引用,进而调用函数 operator << (ostream &, const string &); ,该函数是 string 类的友元。


需要注意的是,引用 stu 不会自动转换为 string 引用。根本原因在于,在私有继承中,在不进行显式类型转换的情况下,不能将指向派生类的引用或指针赋值给基类引用或指针(原因可参考:《派生类与基类之间的几个特殊关系》)。


然而,即使这个例子使用的是公有继承,也必须使用显式类型转换。如果不是,那么 os << stu; 这条语句将与友元函数原型匹配,从而导致了递归调用。还有一个原因是,由于这个类是多重继承,如果两个基类都提供了函数 operator << (); ,那么编译器也将无法确定应该转换成哪个基类。


上面 studenti.h 对应的 studenti.cpp 文件代码:
[C++] 纯文本查看 复制代码
// studenti.cpp -- Student class using private inheritance
#include "studenti.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;

// public methods
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);         // use ArrayDb::operator[]()
}

double Student::operator[](int i) const
{
    return ArrayDb::operator[](i);
}

// private method
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; 
}

// friends
// use String version of operator>>()
istream & operator>>(istream & is, Student & stu)
{
    is >> (string &)stu;
    return is; 
}

// use string friend getline(ostream &, const string &)
istream & getline(istream & is, Student & stu)
{
    getline(is, (string &)stu);
    return is;
}

// use string version of operator<<()
ostream & operator<<(ostream & os, const Student & stu)
{
    os << "Scores for " << (const string &) stu  << ":\n";
    stu.arr_out(os);  // use private method for scores
    return os;
}



测试程序:
[C++] 纯文本查看 复制代码
// use_stui.cpp -- using a class with private inheritance
// compile with studenti.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";
    // cin.get();
    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; 
}



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-21 07:19 , Processed in 0.063864 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表