#ifndef _STUDENT_H_
#define _STUDENT_H_ #include <iostream>
#include <string>
#include <valarray> class Student
{
private:
typedef std::valarray<double> ArrayDb;
std::string name; //contained object
ArrayDb scores; //contained object //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(void) const;
const std::string & Name(void) const; double & operator[](int i);
const double & operator[](int i) const; //friends
//inout
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
#include "student.h"
using std::ostream;
using std::istream;
using std::endl;
using std::string; //public methods
double Student::Average(void) const
{
if(scores.size() > )
{
return scores.sum() / scores.size();
}
else
{ return ;
}
} const string & Student::Name(void) const
{
return name;
} double & Student::operator[](int i)
{
return scores[i];
} const double & Student::operator[](int i) const
{
return scores[i];
} //private method
ostream & Student::arr_out(ostream & os) const
{
int i;
if(scores.size() > )
{
for(i = ; i < scores.size(); i++)
{
os << scores[i] << " ";
if(i % == )
{
os << endl;
}
}
if(i % != )
{
os << endl;
}
}
else
{
os << "empty array";
}
return os;
} //friend methods
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, const Student & stu)
{
os << "Scores for " << stu.name << ":\n";
stu.arr_out(os);
return os;
}
#include <iostream>
#include "student.h"
using std::cin;
using std::cout;
using std::endl; 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 = ; i < n; i++)
{
cin >> sa[i];
}
while(cin.get() != '\n')
{
continue;
}
} const int pupils = ;
const int quizzes = ; int main(void)
{
Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)}; for(int i = ; i < pupils; i++)
{
set(ada[i], quizzes);
}
cout << "\nStudent List:\n";
for(int i = ; i < pupils; i++)
{
cout << ada[i].Name() << endl;
}
cout << "\nResults:";
for(int i = ; i < pupils; i++)
{
cout << endl << ada[i];
cout << "average: " << ada[i].Average() << endl;
}
cout << "Done.\n"; return ;
}

has-a关系——包含对象成员的类的更多相关文章

  1. C++_代码重用2-包含对象成员的类

    对于姓名可以使用字符数组来表示,但这将限制姓名的长度.当然,还可以使用char指针和动态内存分配,但这要求提供大量的支持代码.有一个好的方法就是使用一个他人开发好的类的对象来表示.如果C++库提供了合 ...

  2. 【转载】C++对象成员与构造函数

    一个类的对象可以作为另一个类的数据成员,此时把该对象称为类的对象成员. 当一个类中出现对象成员时,该类的构造函数就要为对象成员初始化,对象成员的初始化必须在构造函数的初始化表中完成. 注意: 初始化对 ...

  3. 如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类

    本文翻译自 https://support.microsoft.com/zh-cn/help/168958/how-to-export-an-instantiation-of-a-standard-t ...

  4. C++编译器是如何管理类和对象的,类的成员函数和成员变量

    C++中的class从面向对象理论出发,将变量(属性)和函数(方法)集中定义在一起,用于描述现实世界中的类.从计算机的角度,程序依然由数据段(栈区内存)和代码段(代码区内存)构成. #include ...

  5. python基础-9.1 面向对象进阶 super 类对象成员 类属性 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理

    上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...

  6. Python全栈--9.1--面向对象进阶-super 类对象成员--类属性- 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理

    上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...

  7. 【IOS 开发】Object - C 面向对象 - 类 , 对象 , 成员变量 , 成员方法

    . 一. 类定义 类定义需要实现两部分 : -- 接口部分 : 定义类的成员变量和方法, 方法是抽象的, 在头文件中定义; -- 实现部分 : 引入接口部分的头文件, 实现抽象方法; 1. 接口部分定 ...

  8. C++ 类 & 对象-类成员函数-类访问修饰符-C++ 友元函数-构造函数 & 析构函数-C++ 拷贝构造函数

    C++ 类成员函数 成员函数可以定义在类定义内部,或者单独使用范围解析运算符 :: 来定义. 需要强调一点,在 :: 运算符之前必须使用类名.调用成员函数是在对象上使用点运算符(.),这样它就能操作与 ...

  9. Servlet中常用对象及API类之间的关系

    Servlet最常用的对象: 请求对象:ServletRequest和HttpServletRequest,通过该对象获取来自客户端的请求信息 响应对象:ServletResponse和HttpSer ...

随机推荐

  1. [转] C++临时变量的生命周期

    http://www.cnblogs.com/catch/p/3251937.html C++中的临时变量指的是那些由编译器根据需要在栈上产生的,没有名字的变量. 主要的用途主要有两类: 1) 函数的 ...

  2. php declare (ticks = N)

    A tick is an event that occurs for every N low-level tickable statements executed by the parser with ...

  3. codevs 4909 寂寞的堆(写的好丑0.0)

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #defin ...

  4. 2015 Multi-University Training Contest 5

    #include <iostream> //1002 #include<set> #include<stdio.h> using namespace std; co ...

  5. dedecms调用所有顶级栏目最新文章的实现方法

    做dedecms的模板,我们会遇到各种各样的调用问题,dedecms列表页调用所有顶级栏目文章列表的方法如下所述: 在文章页面经常使用的是 {dede:arclist orderby='pubdate ...

  6. Asp.net中的页面跳转及post数据

    /// <summary> /// This method prepares an Html form which holds all data /// in hidden field i ...

  7. PHPexcel数据按模板导出

    <?php header("Content-type: text/html; charset=gb2312"); error_reporting(E_ALL); ini_se ...

  8. Android VideoView

    这两天公司要让做一个播放视频的小Demo,于是网上学习了下VideoView的使用方法. 先看布局文件,很简单 就是一个VideoView和两个ImageView <RelativeLayout ...

  9. Android Marquee

    android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:e ...

  10. CXF处理Date类型的俩种方式

    1. CXF利用wsdl2java生成客户端时Date日期类型转换 http://cwfmaker.iteye.com/blog/1142835 2. GregorianCalendar cal = ...