C++ Prime Plus 学习笔记033

书籍:C++ Primer Plus (第六版)(中文版)

工具:Dev-C++ 5.11

电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz

系统类型:64位操作系统,基于X64的处理器 windows10 专业版

第14章 C++中的代码重用

14.1 包含对象成员的类

实例14.1

studentc.h

cpp 复制代码
#ifndef STUDENTC_H_
#define STUDENTC_H_

#include <iostream>
#include <string>
#include <valarray>

class Student
{
private:
    typedef std::valarray<double> ArrayDb;
    std::string name;
    ArrayDb scores;
    //private method for scores output
    std::ostream & arr_out(std::ostream & os) const;
public:
    Student() :name("Null student"), scores() {}
    explicit Student(const std::string &s) :name(s), scores() {}
    explicit Student(int n):name("Nully"), scores(n) {}
    Student(const std::string & s, int n) :name(s), scores(n) {}
    Student(const std::string & s, const ArrayDb & a) :name(s), scores(a) {}
    Student(const char * str, const double * pd, int n) :name(str), scores(pd, n) {}
    ~Student() {};
    double Average() const;
    const std::string & Name() const;
    double & operator[] (int i);
    double operator[](int i) const;
    //friend
    //input
    friend std::istream & operator >> (std::istream & is, Student &stu);  // 1 word
    friend std::istream & getline(std::istream & is, Student &stu);    // 1  line
    //output
    friend std::ostream & operator <<(std::ostream & os, const Student & stu);

};

#endif

student.cpp

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

using namespace std;

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

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

double & Student::operator[](int i)
{
    return scores[i];   //use valarray<double>::operator[]()
}

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

//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 friend getline(ostream &,const 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 << "Scores for " << stu.name << ": \n";
    stu.arr_out(os);
    return os;
}

use_stuc.cpp

cpp 复制代码
#include <iostream>
#include "studentc.h"
using namespace std;

void set(Student & sa, int n);
const int pupils = 3;
const int quizzes = 5;

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 : \n";
    for (i = 0; i < pupils; i++)
    {
        cout << endl << ada[i];
        cout << "average : " << 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 scores: \n";
    for (int i = 0; i < n; i++)
        cin >> sa[i];
    while (cin.get() != '\n')
        continue;
}

编译运行结果:

cpp 复制代码
Please enter the student's name : Gil Bayts
Please enter 5 quiz scores:
92 94 96 93 95
Please enter the student's name : pat Roone
Please enter 5 quiz scores:
83 89 75 78 95
Please enter the student's name : Fleur Day
Please enter 5 quiz scores:
92 89 96 74 64

 Student List :

Scores for Gil Bayts:
92  94  96  93  95
average : 94

Scores for pat Roone:
83  89  75  78  95
average : 84

Scores for Fleur Day:
92  89  96  74  64
average : 83
Done.

--------------------------------
Process exited after 86.83 seconds with return value 0
请按任意键继续. . .
相关推荐
Java后端的Ai之路10 小时前
【神经网络基础】-神经网络学习全过程(大白话版)
人工智能·深度学习·神经网络·学习
粉红色回忆10 小时前
用链表实现了简单版本的malloc/free函数
数据结构·c++
写代码的小球11 小时前
C++计算器(学生版)
c++·算法
k***921611 小时前
【C++】继承和多态扩展学习
java·c++·学习
weixin_4407305011 小时前
java结构语句学习
java·开发语言·学习
序属秋秋秋12 小时前
《Linux系统编程之进程控制》【进程等待】
linux·c语言·c++·进程·系统编程·进程控制·进程等待
航Hang*12 小时前
Photoshop 图形与图像处理技术——第8章:图像的色彩与色彩调整和图像的输出与优化
图像处理·笔记·ui·photoshop
l木本I12 小时前
Reinforcement Learning for VLA(强化学习+VLA)
c++·人工智能·python·机器学习·机器人
小桥流水---人工智能13 小时前
风电机组故障诊断与状态监测方法的研究局限性整理(背景笔记)
笔记
strive programming13 小时前
Effective C++_异常(解剖挖掘)
c++