1、引入
/*关闭隐式转换的原因*/
/*
Student doh("xiaoming",10); //使用构造函数Student(const std::string s,int n)创建Stident对象doh
doh = 5;
//如果没有关键字explict,doh=5 将调用Student(int n) : name("NUll name"),scores(n) {},并使用NUll name来设置name的值
//如果加上explicit则不会进行隐式转换
*/
/*valarray类的介绍*/
/*
01)需包含头文件#include <valarray>
02)使用时,需要在valarray后面加上一对尖括号并在里面写上要使用的数据类型
valarray<int> q_values; //an array of int
valarray<double> weights; //an array of double
03)使用valarray类构造函数创建对象的例子:
double gpa = {3.1,4.2,5.4,3.2,5.6,7.8}; //创建一个double数组
valarray<double> v1; //an array of double,size 0
valarray<int> v2(8); //an array of int,size 8
valarray<int> v3(10,8); //an array of int,size 8,each set to 10
valarray<double> v4(gpa,4); //an array of double,size 4,each set to first 4 elements of gpa
valarray<int> v5 = {10,9,8,7}; //c++11
04)valarray类方法
operator[]() //访问各个元素
size(); //返回包含元素数
sum(); //返回包含元素和
max(); //返回包含元素最大值
min(); //返回包含元素最小值
05)在第四章中介绍的vector和array也可以存储数据,但是这些泪提供的算术支持没有valarray多
*/
1 /*Student类公有继承*/ 2 #ifndef STUDENT_H 3 #define STUDENT_H 4 5 #include <iostream> 6 #include <string> 7 #include <valarray> 8 9 class Student 10 { 11 private: 12 typedef std::valarray<double> ArrayDb; //给std::valarray<double>起别名为ArrayDb 13 14 std::string name; //name为一个包含对象 15 ArrayDb scores; //scores为一个包含对象 16 std::ostream & arr_out(std::ostream & os) const; //私有方法 17 public: 18 Student() : name("Null Student"),scores() {} //定义构造函数,并使用成员初始化列表对name和scores进行初始化 19 explicit Student(const std::string s) : name(s),scores() {} //explicit表示关闭隐式转换 20 explicit Student(int n) : name("NUll name"),scores(n) {} //set name to Null name and creat array of n elements 21 Student(const std::string s,int n) : name(s),scores(n) {} 22 Student(const std::string s, const ArrayDb & a) : name(s),scores(a) {} 23 Student(const char* str,const double* pd,int n) : name(s),scores(pd,n) {} 24 ~Student() {} //析构函数 25 26 double Average() const; 27 const std::string & Name() const; 28 double & operator[](int i); //对[]的重载 29 double operator[](int i) const; //对[]的重载 30 31 //friends 32 friend std::istream & operator>>(std::istream & is, Student & stu); //输入一个单词 33 friend std::istream & getline(std::istream & is, Student & stu); //输入一行 34 friend std::ostream & operator<<(std::ostream & os,Student & stu); //输出 35 36 }; 37 38 #endif 39 40 /*关闭隐式转换的原因*/ 41 /* 42 Student doh("xiaoming",10); //使用构造函数Student(const std::string s,int n)创建Stident对象doh 43 doh = 5; 44 //如果没有关键字explict,doh=5 将调用Student(int n) : name("NUll name"),scores(n) {},并使用NUll name来设置name的值 45 //如果加上explicit则不会进行隐式转换 46 */ 47 48 /*valarray类的介绍*/ 49 /* 50 01)需包含头文件#include <valarray> 51 02)使用时,需要在valarray后面加上一对尖括号并在里面写上要使用的数据类型 52 valarray<int> q_values; //an array of int 53 valarray<double> weights; //an array of double 54 03)使用valarray类构造函数创建对象的例子: 55 double gpa = {3.1,4.2,5.4,3.2,5.6,7.8}; //创建一个double数组 56 valarray<double> v1; //an array of double,size 0 57 valarray<int> v2(8); //an array of int,size 8 58 valarray<int> v3(10,8); //an array of int,size 8,each set to 10 59 valarray<double> v4(gpa,4); //an array of double,size 4,each set to first 4 elements of gpa 60 valarray<int> v5 = {10,9,8,7}; //c++11 61 04)valarray类方法 62 operator[]() //访问各个元素 63 size(); //返回包含元素数 64 sum(); //返回包含元素和 65 max(); //返回包含元素最大值 66 min(); //返回包含元素最小值 67 05)在第四章中介绍的vector和array也可以存储数据,但是这些泪提供的算术支持没有valarray多 68 */
1 /*公有继承Student.cpp实现*/ 2 #include "Student.h" 3 4 using std::ostream; 5 using std::istream; 6 using std::endl; 7 using std::string; 8 9 /*私有方法定义*/ 10 std::ostream & arr_out(std::ostream & os) const 11 { 12 int i; 13 int lim = scores.size(); 14 if(lim>0) 15 { 16 for(i=0; i<lim; i++) 17 { 18 os<<scores[i]<<" " 19 if(i%5 == 4) //scores中的四个数据为一行 20 os<<endl; 21 } 22 if(i%5!=0) 23 os<<endl; 24 } 25 else 26 os<<"Empty array"; 27 return os; 28 } 29 30 /*计算分数平均值*/ 31 double Student::Average() const 32 { 33 if(scores.size()>0) 34 return scores.sum()/scores.size(); //直接调用valarray类中的方法 35 else 36 return 0; 37 } 38 39 /*利用类方法访问私有数据*/ 40 const std::string & Student::Name() const 41 { 42 return name; 43 } 44 45 /*对[]的重载*/ 46 double & Student::operator[](int i) 47 { 48 return scores[i]; //同样直接使用valarray类中的方法operator[]() 49 } 50 /*对[]的重载*/ 51 double Student::operator[](int i) const 52 { 53 return scores[i]; //同样直接使用valarray类中的方法operator[]() 54 } 55 /*友元函数定义--输入一个单词*/ 56 std::istream & operator>>(std::istream & is, Student & stu) 57 { 58 is>>stu.name; 59 return is; 60 } 61 62 /*友元函数定义--输入一行*/ 63 std::istream & getline(std::istream & is, Student & stu) 64 { 65 getline(is,stu.name); 66 return is; 67 } 68 69 /*友元函数定义--输出*/ 70 std::ostream & operator<<(std::ostream & os,Student & stu) 71 { 72 os<<"Scores for "<<stu.name<<":\n"; 73 stu.arr_out(os); //使用私有方法 74 return os; 75 }
1 /*user_stu.cpp*/ 2 #include <iostream> 3 #include "Student.h" 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 void set(Student &sa,int n); //在主函数内声明方法 10 const int pupils = 3; //定义常数 11 const int quizzes = 5; 12 13 int main() 14 { 15 Student ada[pupils] = {Student(quizzes),Student(quizzes),Student(quizzes)}; //定义Student类矩阵,内包含了三个Student类对象 16 int i; 17 for(i = 0;i<pupils; i++) 18 set(ada[i],quizzes); 19 cout<<"\nStudent List:\n"; 20 for(i=0;i<pupils;i++) 21 cout<<ada[i].Name()<<endl; 22 cout<<"Results:\n"; 23 for(i=0;i<quizzes;i++) 24 { 25 cout<<endl<<ada[i]; //这里的<<ada[i]将会首先调用std::ostream & operator<<(std::ostream & os,Student & stu)这个函数,在该函数内继续调用arr_out()函数 26 cout<<"average: "<<ada[i].Average()<<endl; 27 } 28 29 cout<<"Done.\n" 30 return 0; 31 } 32 33 /*子函数定义*/ 34 void set(Student &sa,int n); 35 { 36 cout<<"Please input the student‘s name: "; 37 getline(cin,sa); 38 cout<<"Please input "<<n<<"quiz scires:\n"; 39 for(i=0;i<n;i++) 40 cin>>sa[i]; //这里将会调用double & Student::operator[](int i)函数,返回的是scores[i] 41 while(cin.get() !=‘\n‘) 42 continue; //如果没有接收到换行符,那么继续去接收换行符;接收到了换行符则推出此while循环 43 }
注意:在主函数中,使用了sa[i],这个是使用了对[]的重载函数,返回的是scores[i],详见user_main.cpp,刚开始的没看明白
原文:https://www.cnblogs.com/YiYA-blog/p/11362025.html