C++Primer Plus C++中的代码重用:包含对象成员的类14.2

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:辅助函数也可用作其他用户级输出函数的构建块–如果您选择提供这样的函数的话。程序清单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中使用的,相当的好用(最主要免费), 代码书写的速度能大大的提升

在这里插入图片描述

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值