系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:辅助函数也可用作其他用户级输出函数的构建块–如果您选择提供这样的函数的话。程序清单14.2是 Student类的类方法文件,其中包含了让您能够使用[]运算符来访问 Student 对象中名项成绩的方法。
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
辅助函数也可用作其他用户级输出函数的构建块–如果您选择提供这样的函数的话。程序清单14.2是 Student类的类方法文件,其中包含了让您能够使用[]运算符来访问 Student 对象中名项成绩的方法。
提示:以下是本篇文章正文内容,下面案例可供参考
一、程序清单14.2 student.cpp
// Student 14.2.cpp -- Student class using containment
#include <iostream>
#include "studentc14.1.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
//public methods
double Student::Average() const
{
if(scores.size() > 0)
return scores.sum() / scores.size();
else
{
return 0;
}
}
const string & Student::Name() const
{
//return (const string &) *this;
return name;
}
double & Student::operator[](int i)
{
return scores[i]; //use valarray<double>::operator[]()
}
double Student::operator[](int i) const
{
return scores[i]; //use valarray<double>::operator[]()
}
//private methods
ostream & Student::arr_out(ostream & os) const
{
int i;
int lim = scores.size();
if (lim > 0)
{
for (i = 0; i < lim; i++)
{
os << scores[i] << " ";
if (i % 5 == 4)
os << endl;
}
if (i % 5 != 0)
os << endl;
}
else
{
os << " empty array ";
}
return os;
}
//friends
//use string version of operator>>()
istream & operator>>(istream & is, Student & stu)
{
is >> stu.name;
return is;
}
//use string frient getline(ostream &, string &)
istream & getline(istream & is, Student & stu)
{
getline(is, stu.name);
return is;
}
//use string version of operator<<()
ostream & operator<<(ostream & os, const Student & stu)
{
os << "Name: " << stu.name << endl
<< "Scores: ";
stu.arr_out(os);
return os;
}
除私有辅助方法外,程序清单 14.2并没有新增多少代码。使用包含让您能够充分利用已有的代码。
二,程序清单 14.3use _stuc.cpp
// Student 14.2.cpp -- Student class using containment
#include <iostream>
#include "studentc14.1.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
//public methods
double Student::Average() const
{
if(scores.size() > 0)
return scores.sum() / scores.size();
else
{
return 0;
}
}
const string & Student::Name() const
{
//return (const string &) *this;
return name;
}
double & Student::operator[](int i)
{
return scores[i]; //use valarray<double>::operator[]()
}
double Student::operator[](int i) const
{
return scores[i]; //use valarray<double>::operator[]()
}
//private methods
ostream & Student::arr_out(ostream & os) const
{
int i;
int lim = scores.size();
if (lim > 0)
{
for (i = 0; i < lim; i++)
{
os << scores[i] << " ";
if (i % 5 == 4)
os << endl;
}
if (i % 5 != 0)
os << endl;
}
else
{
os << " empty array ";
}
return os;
}
//friends
//use string version of operator>>()
istream & operator>>(istream & is, Student & stu)
{
is >> stu.name;
return is;
}
//use string frient getline(ostream &, string &)
istream & getline(istream & is, Student & stu)
{
getline(is, stu.name);
return is;
}
//use string version of operator<<()
ostream & operator<<(ostream & os, const Student & stu)
{
os << "Name: " << stu.name << endl
<< "Scores: ";
stu.arr_out(os);
return os;
}
三,3.使用新的 Student 类
下面编写一个小程序来测试这个新的 Student 类。出于简化的目的,该程序将使用一个只包含3个Student 对象的数组,其中每个对象保存5个考试成绩。另外还将使用一个不复杂的输入循环,该循环不验证输入,也不让用户中途退出。程序清单14.3列出了该测试程序,请务必将该程序与Student.cpp 一起进行编译。
程序清单14.3 use stuc.cpp
//use_stuc.cpp -- using a composite class
#include <iostream>
#include "studentc14.1.h"
using std::cout;
using std::cin;
using std::endl;
void set(Student & sa, int n);
const int pupil = 3;
const int quizzes = 5;
int main()
{
Student ada[pupil] = {
Student(quizzes),
Student(quizzes),
Student(quizzes)
};
int i;
for (i = 0; i < pupil; i++)
{
set(ada[i], quizzes);
}
cout<<"\nStudent List:\n";
for ( i = 0; i < pupil; i++)
{
cout << ada[i].Name() << endl;
}
cout<<"\nResults:\n";
for ( i = 0; i < pupil; i++)
{
cout<<endl<<ada[i];
cout<<"Average grade: "<<ada[i].Average()<<endl;
}
cout<<"Done\n";
return 0;
}
void set(Student & sa, int n)
{
cout<<"Please enter the student's name: ";
getline(cin, sa);
cout<<"Please enter "<<n<<" quiz grades:\n";
for (int i = 0; i < n; i++)
{
cin>>sa[i];
}
while (cin.get() != '\n')
continue;
}
总结
提示:这里对文章进行总结:
1,这里给大家推荐一个插件VS中使用的,相当的好用(最主要免费), 代码书写的速度能大大的提升