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.利用私有继承的方法

相关推荐
ElvInR24 分钟前
冒泡排序详解
c语言·c++·排序算法·冒泡排序
天若有情67327 分钟前
探秘 C++ 计数器类:从基础实现到高级应用
java·开发语言·c++
进击的愤怒33 分钟前
GIM发布新版本了 (附rust CLI制作brew bottle流程)
开发语言·后端·rust
x-cmd39 分钟前
x-cmd install | cargo-selector:优雅管理 Rust 项目二进制与示例,开发体验升级
开发语言·后端·rust·cargo·示例
春生野草1 小时前
如何用JAVA手写一个Tomcat
java·开发语言·tomcat
hy____1231 小时前
list类的详细讲解
c++
范纹杉想快点毕业2 小时前
Google C++ Style Guide 谷歌 C++编码风格指南,深入理解华为与谷歌的编程规范——C和C++实践指南
c语言·数据结构·c++·qt·算法
幸运黒锦鲤2 小时前
Qt5、C++11 获取wifi列表与wifi连接
开发语言·qt
满怀10152 小时前
【Python中的Socket套接字详解】网络通信的核心基石
开发语言·网络·python·网络编程·socket
信奥洪老师2 小时前
2025年 全国青少年信息素养大赛 算法创意挑战赛C++ 小学组 初赛真题
c++·算法·青少年编程·等级考试