valarray 包含对象成员的类(cpp14章)

C++代码重用

1.公有继承可以实现

2.包含、私有继承、保护继承用于实现has-a关系,即新的类将包含另一个类的对象。

(使用这样类成员:本身是另外一个类对象称为包含(组合或层次化)。)

3.函数模板、类模板

valarray类构造函数举例

cpp 复制代码
double gap[5] = { 3.1,3.5,3.8,2.9,3.3 };
valarray<double> v1;   //创建1个double类型的空数组
valarray<int> v2(8);   //创建8个int类型数组
valarray<int> v3(10,8); //创建8个int类型数组,数组中每个数都是10
valarray<double> v4(gap,4);//取出gap数组的前四个元素用于填充v4数组
valarray<int> v5 = { 20,32,17,9 };//C++ 11中

类方法举例:

cpp 复制代码
operator[]() : 访问各个元素
size() : 返回包含的元素数
sum() : 返回所有元素的总和
max() : 返回最大的元素
min(): 返回最小的元素

举例:每个学生的录入考试成绩 (has_a关系,学生有姓名,也有一组考试成绩)

Valarray 包含了string类和valarray<double>类

用string对象表示学生的名字,valarray<double>表示考试的分数

将其声明为私有,意味着Valarray类的成员函数可以使用string和valarray<double>类的公有接口来访问和修改name和scores对象。但类外不可这么做,只能通过Valarray类的公有接口访问name和scores。通常被描述为:Valarray类获得了其成员对象的实现,但没有继承接口。

1.代码:(用包含的方法)

valarray.h

cpp 复制代码
#ifndef VALARRAY_H_
#define VALARRAY_H_
#include <iostream>
#include <string>
#include <valarray>
using namespace std;

//14章  14.1 valarray包含成员对象的类
class Student
{
private:
	typedef valarray<double> ArrayDb;
	string name;
	ArrayDb scores;//valarray<double> ArrayDb 
public:
	Student():name("Null student"),scores(){} //成员初始化列表
	explicit Student(const string&s):name(s), scores() {}  //explicit关闭隐式转换,使其只能显调用
	explicit Student(int n) :name("Nully"), scores(n) {}
	Student(const string&s,int n) :name(s), scores(n) {}
	Student(const string&s, const ArrayDb &a) :name(s), scores(a) {}
	Student(const string&s, const double *pd,int n) :name(s), scores(pd,n) {}
	~Student(){}

	double Average() const; //平均成绩 不可修改
	const string &Name() const;
	double &operator[](int n); //stu[0]=100;
	double operator[](int n) const;//a=stu[0]

	friend istream &operator >>(istream &is, Student &stu);//友元函数重载输入输出运算符
	friend istream &getline(istream &is, Student &stu);
	friend ostream &operator<<(ostream &os, Student &stu);
};


#endif // !VALARRAY_H_

valarray.cpp

cpp 复制代码
#include "valarray.h"

double Student::Average() const
{
	if (scores.size() > 0)
		return  scores.sum() / scores.size();
	else
	    return 0.0;
}

const string & Student::Name() const
{
	return name;
}

double & Student::operator[](int n)
{
	return scores[n];
}

double Student::operator[](int n) const
{
	return scores[n];
}

istream & operator>>(istream & is, Student & stu)
{
	is >> stu.name;
	return is;
}

istream & getline(istream & is, Student & stu)
{
	getline(is, stu.name);
	return is;
}

ostream & operator<<(ostream & os, Student & stu)
{
	os << "Scores for" << stu.name << ":" << endl;//显示学生的姓名和各科分数
	int i;
	int lim = stu.scores.size();
	if (lim > 0)
	{
		for ( i = 0; i < lim; i++)
		{
			os << stu.scores[i] << " ";
			if (i % 5 == 4)
				os << endl;
		}
		if (i % 5 != 0)
			os << endl;
	}
	else
		os << "Empty array" << endl;
	return os;
	  
}

main.cpp

cpp 复制代码
#include <iostream>
#include "valarray.h"

using namespace std;
const int pupils = 3; //人数
const int quizzes = 5;//每个人都有5门成绩
void set(Student &sa, int n);
int main()
{
	Student ada[pupils] = { Student(quizzes),Student(quizzes) ,Student(quizzes) };
	int i;
	for (i = 0; i < pupils; i++)
		set(ada[i], quizzes);
	
	cout << "\n Student List:" << endl;
	for (i = 0; i < pupils; i++)
		cout << ada[i].Name() << endl;//显示每个学生的姓名

	cout << "\n Result List:" << endl;
	for (i = 0; i < pupils; i++)
		cout << ada[i];
	cout << "Average:" << ada[i].Average() << endl;
	return 0;
}

void set(Student &sa, int n)
{
	cout << "Please enter the student's name:";
	getline(cin, sa);
	cout << "Please enter:" << n << "quiz scores:" << endl;
	for (int i = 0; i < n; i++)
		cin >> sa[i];
	while (cin.get() != '\n');
}

运行结果:

2.利用私有继承的方法

相关推荐
Biomamba生信基地4 分钟前
两天入门R语言,周末开讲
开发语言·r语言·生信
RAN_PAND5 分钟前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
Bio Coder8 分钟前
R语言安装生物信息数据库包
开发语言·数据库·r语言
Tiger Z9 分钟前
R 语言科研绘图第 27 期 --- 密度图-分组
开发语言·程序人生·r语言·贴图
life_time_2 小时前
C语言(22)
c语言·开发语言
Minner-Scrapy3 小时前
DApp 开发入门指南
开发语言·python·web app
mit6.8243 小时前
[实现Rpc] 通信-Muduo库的实现 | && 完美转发 | reserve | unique_lock
c++·网络协议·rpc
孤雪心殇3 小时前
简单易懂,解析Go语言中的Map
开发语言·数据结构·后端·golang·go
庸俗今天不摸鱼3 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设