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的作用就是 ...
随机推荐
- 学习《Spring 3.x 企业应用开发实战》Day-1
Day-1 记录自己学习spring的笔记 提要:根据<Spring 3.x 企业应用开发实战>开头一个用户登录的例子,按照上面敲的. 1.项目分层
- TCP/IP协议原理与应用笔记08:对等层和对等实体
1. 我们直接通过下面这个图,就可以直观了解: Outlook :收发邮件的软件组件. IE:浏览器. CutFTP:文件传输工具. 小结:因为为了完成不同的功能,所以会出现不同实体,这些不同的实体为 ...
- DataSet离线数据集实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- ASP.NET-FineUI开发实践-14
以前写过一个表格自动补全,改下,就出现了部分级联修改.测试版本4.1.1 共享下JS,ext-part2.js 后台再来个得到数据的方法就可以了. 1.方法还是删除+插入,主要在 i ...
- LinqJoin方法
Linq知识点总结: (一).构建两个List泛型集合 List<Person> list=new List<Person>() { ...
- 关于asp.net中cookie在调试过程中读写正常发布后乱码问题
最近在做的项目发布后出现了乱码的问题,既然出现了乱码很大的可能性是跟编码有关系,所以首先的解决方案就是重新对cookie进行编码, 在写入的cookie的时候编码,在读取的时候解码 在写入cookie ...
- iOS微信支付
SDK接入 服务器签名版本 官方已经是建议使用服务器签名来接入微信支付,实际上从安全上考虑,确实是每个客户端不应该知道RAS密钥,也不需要每个客户端都写一遍签名的算法. 服务端接入流程文档:https ...
- JavaScript--函数-按值传递
按值传递(byValue): 两个变量间赋值或将变量作为参数传入函数时,其实都是将原变量中的值,赋值一份给对方(新变量) 对原始类型的值: 修改新变量,不会影响原变量 对引用类型的对象: 通过新变量修 ...
- 基于Jquery easyui 选中特定的tab并更新
获取选中的 Tab // 获取选中的 tab panel 和它的 tab 对象 var pp = $('#tt').tabs('getSelected'); var tab = pp.panel('o ...
- linux之umask函数解析
[lingyun@localhost umask_1]$ vim umask.c + umask.c ...