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的作用就是 ...
随机推荐
- 第四篇:SQL
前言 确实,关于SQL的学习资料,各类文档在网上到处都是.但它们绝大多数的出发点都局限在旧有关系数据库里,内容近乎千篇一律.而在当今大数据的浪潮下,SQL早就被赋予了新的责任和意义. 本篇中,笔者将结 ...
- [转] 使用CodeViz生成C/C++函数调用关系图
运行环境:虚拟机下的Ubuntu 11.04 结合Graphviz工具,使用CodeViz可以生成直观和漂亮的C/C++程序函数之间的调用关系图. 1.安装graphviz 在安装CodeViz之前, ...
- Python之路,Day19 - CMDB、CMDB、CMDB
Python之路,Day19 - CMDB.CMDB.CMDB 本节内容 浅谈ITIL CMDB介绍 Django自定义用户认证 Restful 规范 资产管理功能开发 浅谈ITIL TIL即IT ...
- python-Pickle序列化
[Python之旅]第三篇(二):Pickle序列化 python 序列化 pickle 摘要: 说明:关于Pickle的说明 作如下说明: 1 2 3 4 5 6 7 序列化的概念很简单 ...
- VS2008 快捷键大全--------<<转>>
VS2008 快捷键大全 Ctrl+E,D ---- 格式化全部代码 Ctrl+K,F ---- 格式化选中的代码 CTRL + SHIFT + B ...
- iOS 不同类之间的传值
iOS是面向对象开发的,有很多不同的类,很多时候会遇到类与类之间的"交流"需求,比如通知.传递数值等等,(通知可以用nsnotificationcenter来做, 以后总结)下面主 ...
- sae crop 文档
原文是google缓存:http://webcache.googleusercontent.com/search?q=cache:MD_FP-G6RI8J:sae.sina.com.cn/%3Fm%3 ...
- Python datetime time 常用操作
测试版本: Python 2.7 获取当前时间的两种方法 import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") ...
- Extjs嵌入html
方式一:使用组件的html属性嵌入html代码,如果html代码中存在参数可以使用字符串拼接的方式拼接html代码. html页面: <!doctype html> <html> ...
- windows8.1 下搭建配置apache+php+mysql
软件版本: apache:Apache 2.4.10 Win64 http://www.apachelounge.com/download/VC11/binaries/httpd-2.4.10- ...