Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Example class
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- class Example {
- public:
- Example()
- :i{1}, names{vector<string>{"Whatever"}}{
- //empty body
- }
- explicit Example(int x)
- : i{x}, names{vector<string>{"Whatever"}}{
- if(i < 0){
- i = 0;
- }
- }
- Example(int x, const vector<string>& s)
- : i{x}, names{s}{
- if(i < 0){
- i = 0;
- }
- if(names.size() < 1){
- names.push_back("Whatever");
- }
- }
- void print(){
- cout << "i: " << i << "names size: " << names.size() << endl;
- }
- string getName(size_t position)const {
- string result{};
- if(position < names.size() && position >= 0){
- result = names[position];
- }
- return result;
- }
- void addName(const string& name){
- if(name.size() > 0){
- names.push_back(name);
- }
- }
- void replace(size_t position, const string& name){
- if(position < names.size()){
- names[position] = name;
- }
- }
- size_t count() const{
- return names.size();
- }
- private:
- int i{};
- vector<string> names{};
- };
- void printAll(const vector<Example>& list){
- for(const Example& f : list){
- for(size_t i{0}; i < f.count(); i += 1){
- cout << f.getName(i) << endl;
- }
- }
- }
- void testExample(){
- Example a{};
- Example b{3};
- Example c{5, vector<string>{"abc", "def"}};
- a.print();
- b.print();
- c.print();
- vector<Example> ve1{a,b,c};
- vector<Example> ve2{Example{}, Example{1}, Example{2, vector<string>{"a","b","c"}}};
- cout <<"===================" << endl;
- ve2[2].replace(0, "betty");
- for(Example e: ve2){
- cout << e.getName(0) << ' ';
- e.print();
- }
- printAll(ve2);
- }
- int main()
- {
- testExample();
- cout << "Hello world!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement