has-a关系——多重私有继承
#ifndef _STUDENT_H_
#define _STUDENT_H_ #include <iostream>
#include <string>
#include <valarray> class Student : private std::string, private std::valarray<double>
{
private:
typedef std::valarray<double> ArrayDb; //private method for scores output
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(void) const;
const std::string & Name(void) const;
//double & operator[](int i);
//const double & operator[](int i) const;
using std::valarray<double>::operator[]; //使用using重新定义基类方法在私有继承中的访问权限 //friends
//inout
friend std::istream & operator>>(std::istream & is, Student & stu); //1 word
friend std::istream & getline(std::istream & is, Student & stu); //1 line
//output
friend std::ostream & operator<<(std::ostream & os, const Student & stu);
}; #endif
#include "student.h"
using std::ostream;
using std::istream;
using std::endl;
using std::string; //public methods
double Student::Average(void) const
{
if(ArrayDb::size() > )
{
return ArrayDb::sum() / ArrayDb::size();
}
else
{
return ;
}
} const string & Student::Name(void) const
{
return (const string &)*this;
}
/*
double & Student::operator[](int i)
{
return ArrayDb::operator[](i);
} const double & Student::operator[](int i) const
{
return ArrayDb::operator[](i);
}
*/
//private method
ostream & Student::arr_out(ostream & os) const
{
int i;
if(ArrayDb::size() > )
{
for(i = ; i < ArrayDb::size(); i++)
{
//os << ArrayDb::operator[](i) << " ";
os << ((const ArrayDb &)*this)[i] << " ";
if(i % == )
{
os << endl;
}
}
if(i % != )
{
os << endl;
}
}
else
{
os << "empty array";
}
return os;
} //friend methods
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;
}
#include <iostream>
#include "student.h"
using std::cin;
using std::cout;
using std::endl; 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 = ; i < n; i++)
{
cin >> sa[i];
}
while(cin.get() != '\n')
{
continue;
}
} const int pupils = ;
const int quizzes = ; int main(void)
{
Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)}; for(int i = ; i < pupils; i++)
{
set(ada[i], quizzes);
}
cout << "\nStudent List:\n";
for(int i = ; i < pupils; i++)
{
cout << ada[i].Name() << endl;
}
cout << "\nResults:";
for(int i = ; i < pupils; i++)
{
cout << endl << ada[i];
cout << "average: " << ada[i].Average() << endl;
}
cout << "Done.\n"; return ;
}
has-a关系——多重私有继承的更多相关文章
- Effective C++ 第二版 40)分层 41)继承和模板 42)私有继承
条款40 通过分层来体现"有一个"或"用...来实现" 使某个类的对象成为另一个类的数据成员, 实现将一个类构筑在另一个类之上, 这个过程称为 分层Layeri ...
- C++ 中私有继承、保护继承与公有继承
区别 下面通过一个示例来介绍三种继承的区别. 定义一个基类(假设为一个快退休的富豪): class RichMan { public: RichMan(); ~RichMan(); int m_com ...
- 《C++ Primer Plus》14.2 私有继承 学习笔记
C++(除了成员变量之外)还有另一种实现has-a关系的途径——私有继承.使用私有继承,基类的公有成员和保护成员都将成为派生类的私有成员.(如果使用保护继承,基类的公有成员和保护成员都将称为派生类的保 ...
- 谈谈C++私有继承
很多C++程序猿从来没用过私有继承来设计他的类.的确,假设是本该用私有继承的地方却用了公有继承.对程序的功能的实现并无影响. 但这样的误用是一种错位的描写叙述.会引起阅读者的误解,甚至会引起类的使用者 ...
- C++学习笔记14,private/protected/public继承,私有继承,保护继承,公有继承(五)(总结)
各种继承方式: 特征 公有继承 保护继承 私有继承 公有成员变为 派生类的公有成员 派生类的保护成员 派生类的私有成员 保护成员变为 派生类的保护成员 派生类的保护成员 派生类的私有成员 私有成员变为 ...
- C++中公有继承、保护继承、私有继承的区别
公有继承时基类中各成员属性保持不变,基类中private成员被隐藏.派生类的成员只能访问基类中的public/protected成员,而不能访问private成员:派生类的对象只能访问基类中的publ ...
- C++公有继承、保护继承和私有继承
C++中的继承方式有: public.private.protected三种(它们直接影响到派生类的成员.及其对象对基类成员访问的规则). (1)public(公有继承):继承时保持基类中各成员属性不 ...
- c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承
公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...
- C++之共有继承、保护继承、私有继承
1.封装,public,private作用就是这个目的. 类外只能访问public成员而不能方位private成员: private成员只能被类成员和友元访问: 2.继承,protected的作用就是 ...
随机推荐
- codevs2618核电站问题
/* 因为m为连续放的个数 所以状态要包括这个条件 定义状态:f[n][m]表示第n个坑连续放了m个 转移:分两种 1. 第x个坑 放 即m>=1 则 f[x][k]=f[x-1][k-1] 2 ...
- 二分图最大匹配(匈牙利算法Dfs模板)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2020 using name ...
- 在CSS文件中引入其他CSS文件
引入CSS的方法有两种,一种是@import,一种是link 一.@import url('地址');二.<link href="地址" rel="styleshe ...
- c# 使用递归 循环遍历导航树结构 并解析
1.数据书库结构 1 家用电器 0 一级菜单 2 手机.数码.京东通信 0 一级菜单 3 电脑.办公 0 一级菜单 4 家具.家居.厨房 0 一级菜单 5 男装.女装.童装.内衣 0 一级菜单 6 个 ...
- N3292系列资料之RTC介绍
N3292系列资料之RTC介绍 1 RTC特性 Ø 拥有时间计数器(秒,分,时)和日历计数器,用来计算时间 Ø 绝对定时功能(秒,分,时,日,月,年) Ø 相对定时功能 Ø 支持12小时/24小时模式 ...
- Win7 64位 php-5.5.13+Apache 2.4.9+mysql-5.6.19 配置
一 :准备阶段 1:php php-5.5.13下载链接:http://windows.php.net/downloads/releases/php-5.5.13-Win32-VC11-x64.zip ...
- python对比两个文件问题
写一个比较两个文本文件的程序. 如果不同, 给出第一个不同处的行号和 列号. 比较的时候可以使用zip()函数 a=open('test.txt','r') b=open('test2.txt','r ...
- 判断鼠标从哪个方向进入--jQuery
转载自:http://sentsin.com/web/112.html $("#wrap").bind("mouseenter mouseleave",func ...
- Fireworks Extension —— AutoSlice 介绍
前不久在网上到处瞎晃的时候,发现Adobe的软件几乎都可以写插件.Fireworks更是很早的版本就支持使用javascript编写插件,于是乎如入桃园,奋斗几日为VD小伙伴们写了一个插件,命名Aut ...
- 感觉差不多了。CLOUDSTACK的NAT,端口转发和防火墙结合穿透
构架差不多OK啦. 测试也近尾声啦. 感觉它适合私有云,或是混合云,但结合了SDN的网络,真的很安全. 如果是纯公有云,也是更适合特定方案.或是要第二次开发,毕竟让ACCOUNT登陆到后台,结合GUE ...