1.公有继承可以实现
2.包含、私有继承、保护继承用于实现has-a关系,即新的类将包含另一个类的对象。
(使用这样类成员:本身是另外一个类对象称为包含 (组合或层次化)。)
3.函数模板、类模板
valarray类构造函数举例
double gap[5] = { 3.1,3.5,3.8,2.9,3.3 };
valarray v1; //创建1个double类型的空数组
valarray v2(8); //创建8个int类型数组
valarray v3(10,8); //创建8个int类型数组,数组中每个数都是10
valarray v4(gap,4);//取出gap数组的前四个元素用于填充v4数组
valarray v5 = { 20,32,17,9 };//C++ 11中
类方法举例:
operator[]() : 访问各个元素
size() : 返回包含的元素数
sum() : 返回所有元素的总和
max() : 返回最大的元素
min(): 返回最小的元素
举例:每个学生的录入考试成绩 (has_a关系,学生有姓名,也有一组考试成绩)
用string对象表示学生的名字,valarray
将其声明为私有,意味着Valarray类的成员函数可以使用string和valarray
代码:
valarray.h
#ifndef VALARRAY_H_
#define VALARRAY_H_
#include
#include
#include
using namespace std;
//14章 14.1 valarray包含成员对象的类
class Student
{
private:
typedef valarray ArrayDb;
string name;
ArrayDb scores;//valarray 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
#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
#include
#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');
}
运行结果: