Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 3 vector and container class code
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef __CONTAINER_H__ | ||
#define __CONTAINER_H__ | ||
|
||
class Container { | ||
public: | ||
virtual double& operator[](int) = 0; | ||
virtual int size() const = 0; | ||
virtual ~Container() {}; | ||
}; | ||
|
||
#endif //__CONTAINER_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
|
||
#include "container.hpp" | ||
#include "vector_container.hpp" | ||
|
||
void use(Container& c) { | ||
const int sz = c.size(); | ||
|
||
for(int i = 0; i != sz; ++i) { | ||
std::cout << c[i] << '\n'; | ||
} | ||
} | ||
|
||
void g() { | ||
Vector_container vc{10,9,8,7,6,5,4,3,2,1,0}; | ||
|
||
use(vc); | ||
} | ||
|
||
int main() { | ||
g(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "vector.hpp" | ||
|
||
double& Vector::operator[](int i) { | ||
return elem[i]; | ||
} | ||
|
||
int Vector::size() const { | ||
return sz; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef __VECTOR_H__ | ||
#define __VECTOR_H__ | ||
|
||
#include <initializer_list> | ||
#include <algorithm> | ||
|
||
class Vector { | ||
private: | ||
double* elem; | ||
int sz; | ||
|
||
public: | ||
Vector(int s) : elem{new double[s]}, sz{s} { | ||
for(int i = 0; i != s; ++i) elem[i] = 0; | ||
} | ||
|
||
Vector(std::initializer_list<double> lst) | ||
: elem{new double[lst.size()]}, sz{static_cast<int>(lst.size())} { | ||
std::copy(lst.begin(), lst.end(), elem); | ||
} | ||
|
||
~Vector() { delete[] elem; } | ||
|
||
double& operator[](int i); | ||
int size() const; | ||
}; | ||
|
||
#endif //__VECTOR_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __VECTOR_CONTAINER_H__ | ||
#define __VECTOR_CONTAINER_H__ | ||
|
||
#include <initializer_list> | ||
|
||
#include "container.hpp" | ||
#include "vector.hpp" | ||
|
||
class Vector_container : public Container { | ||
Vector v; | ||
public: | ||
Vector_container(int s) : v(s) {} | ||
Vector_container(std::initializer_list<double> lst) : v(lst) {} | ||
~Vector_container() {} | ||
|
||
double& operator[](int i) { return v[i]; } | ||
int size() const { return v.size(); } | ||
}; | ||
|
||
#endif //__VECTOR_CONTAINER_H__ |