mirror of
https://github.com/VictorEijkhout/TheArtOfHPC_vol3_cppf08programming.git
synced 2026-01-24 22:44:48 +09:00
51 lines
1.1 KiB
C++
51 lines
1.1 KiB
C++
/****************************************************************
|
|
****
|
|
**** This file belongs with the course
|
|
**** Introduction to Scientific Programming in C++/Fortran2003
|
|
**** copyright 2017-2021 Victor Eijkhout eijkhout@tacc.utexas.edu
|
|
****
|
|
**** constat.cxx : explore const overloading
|
|
****
|
|
****************************************************************/
|
|
|
|
#include <iostream>
|
|
using std::cin;
|
|
using std::cout;
|
|
|
|
#include <vector>
|
|
using std::vector;
|
|
|
|
//codesnippet constatclass
|
|
class has_array {
|
|
private:
|
|
vector<float> values;;
|
|
public:
|
|
has_array(int l,float v)
|
|
: values(vector<float>(l,v)) {};
|
|
auto& at(int i) {
|
|
cout << "var at" << '\n';
|
|
return values.at(i); };
|
|
const auto& at (int i) const {
|
|
cout << "const at" << '\n';
|
|
return values.at(i); };
|
|
auto sum() const {
|
|
float p;
|
|
for ( int i=0; i<values.size(); ++i)
|
|
p += at(i);
|
|
return p;
|
|
};
|
|
};
|
|
|
|
int main() {
|
|
|
|
int l; float v;
|
|
cin >> l; cin >> v;
|
|
has_array fives(l,v);
|
|
cout << fives.sum() << '\n';
|
|
fives.at(0) = 2;
|
|
cout << fives.sum() << '\n';
|
|
//codesnippet end
|
|
|
|
return 0;
|
|
}
|