Home > Back-end >  Why [] operator overloading should have two versions, are used in what place?
Why [] operator overloading should have two versions, are used in what place?

Time:01-17

 
# # ifndef STUDENTC_H_
# define STUDENTC_H_

#include
#include
#include

The class Student: private STD: : string, STD: : valarray
{
Private:
Typedef STD: : valarray ArrayDb;
STD: : ostream& Arr_out (STD: : ostream& OS) const;
Public:
Student () : STD: : string (" Null Student "), ArrayDb () {}
Explicit Student (const STD: : string& S: STD: : string (s), ArrayDb () {}
Explicit Student (int n) : STD: : string (" Nully "), ArrayDb (n) {}
Student (const STD: : string& S, int n) : STD: : string (s), ArrayDb (n) {}
Student (const STD: : string& S, const ArrayDb& A) : STD: : string (s), ArrayDb (a) {}
Student (const char * STR, const double * pd, int n) : STD: : string (STR), ArrayDb (pd, n) {}
~ Student () {}

Double business () const;
Double& Operator [] (int I); A: why are there two versions//?
Double operator [] (int I) const;
Const STD: : string& The Name () const;

Friend STD: : istream& Operator> (STD: : istream& Is, Student& Stu);
Friend STD: : istream& Getline (STD: : istream& Is, Student& Stu);
Friend STD: : ostream& Operator<(STD: : ostream& OS, const Student& Stu);
};

# endif


 
# include "studenti. H"
Using STD: : cost;
Using STD: : ostream;
Using STD: : string;
Using STD: : endl;

Double Student: : business (const)
{
If (ArrayDb: : size () & gt; 0)
Return ArrayDb: : sum ()/ArrayDb: : size ();
The else
return 0;
}

Const string& Student: : Name (const)
{
Return (const string&) * this;
}

Double& Student: : operator (int I) []//what is the difference between the two versions?
{
STD: : cout & lt; <"Double& \ n ";
Return ArrayDb: : operator [] (I);
}

Double Student: : operator [] (int I) const
{
STD: : cout & lt; <"Double \ n";
Return ArrayDb: : operator [] (I);
}

Ostream& Student: : arr_out (ostream& OS) const
{
int i;
Int lim=ArrayDb: : size ();
If (lim & gt; 0)
{
For (I=0; I & lt; Lim. I++)
{
OS & lt; If (I % 5==4)
OS & lt; }
If (I % 5!=0)
OS & lt; }
The else
OS & lt; <"The empty array";
Return the OS;
}

Istream& Operator> (istream& Is, Student& Stu)
{
STD: : cout & lt; <"Cin \ n";
Is & gt;> (string&) Stu.
The return is;
}

Istream& Getline (istream& Is, Student& Stu)
{
STD: : cout & lt; <"Getline \ n";
Getline (is, (string&) Stu);
The return is;
}

Ostream& Operator<(ostream& OS, const Student& Stu)
{
OS & lt; <"Scores for" & lt; <(string&) Stu & lt; <": \ n";

Return stu. Arr_out (OS);
}


 
#include
# include "studenti. H"
Using STD: : cin;
Using STD: : cout;
Using STD: : endl;

Void the 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 & lt; Pupils; I++)
Set (Ada [I], quizzes);
cout <"\ nStudent List: \ n";
For (I=0; I & lt; Pupils; I++)
cout cout <"\ nResults:";
For (I=0; I & lt; Pupils; I++)
{
cout cout <"Business:" & lt; }
cout <"Done. \ n";
return 0;
}

Void the set (Student& Sa, int n)
{
cout <"Both Please enter the student 's name:";
Getline (cin, sa);
//cin & gt;> Sa;
cout <"Both Please enter" & lt; For (int I=0; I & lt; n; I++)
Cin & gt;> Sa [I];
While (cin. The get ()! )
='\ n'continue;
}

There are other operators need two versions of the so?

CodePudding user response:

[] a can modify, one can't modify the element value
  • Related