// algorithms.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include #include using namespace std; class Person { private: string firstname; string lastname; int age; public: Person(string fname, string lname, int a) :firstname(fname), lastname(lname), age(a) {}; virtual ~Person() {}; // shoud be virtual string GetName() const { return firstname + " " + lastname; }; int GetAge() const { return age; }; void SetName(string const& first, string const& last) { firstname = first; lastname = last; }; void SetAge(int const& newage) { age = newage; }; virtual void Say() { cout << "I am a person and my name is " << GetName() << endl; } }; class Customer : public Person { private: int id; public: Customer(string fname, string lname, int a, int ID) : Person(fname, lname, a), id(ID) {}; void Say() { cout << "I am a customer of your Bank and my name is " << GetName() << endl; //try protected }; }; class BankAccount { private: string id; public: BankAccount(string ID) :id(ID) {}; void SetID(string const &ID) { id = ID; }; string GetID() const { return id; }; virtual void CalculateFees() {}; }; bool less_five(BankAccount const & p) { if (p.GetID().length() <= 5) { return true; } else { return false; } }; void print(int i) { cout << i << endl; }; int main() { BankAccount ba1("123"); BankAccount ba2("124"); BankAccount ba3("125"); vector v; //reminder about the vector v.push_back(ba1); v.push_back(ba2); v.push_back(ba3); for (auto i = v.begin(); i != v.end(); i++) { cout << i->GetID() << endl; } for (auto i = v.rbegin(); i != v.rend(); i++) { cout << i->GetID() << endl; } ///For each for_each(v.begin(), v.end(), [](BankAccount const & p) {cout << p.GetID() << endl; }); //use lambda //Searching auto flag = find_if(v.begin(), v.end(), less_five); if (flag != v.end()) { cout << flag->GetID() << endl; } else { cout << "All elements are longer than 5" << endl; } //Sorting vector vi; //reminder about the vector vi.push_back(1); vi.push_back(6); vi.push_back(10); vi.push_back(9); for_each(vi.begin(), vi.end(), print); sort(vi.begin(), vi.end()); for_each(vi.begin(), vi.end(), print); system("pause"); return 0; }