Source of TestStuff20140922.cpp


  1: //TestStuff20140922.cpp

  2: //Monday, September 22, 2014

  3: 
  4: #include <iostream>

  5: #include <fstream>

  6: #include <iomanip>

  7: #include <string> 

  8: #include <cstdlib>

  9: #include <vector>

 10: using namespace std;
 11: 
 12: #include "utilities.h"

 13: using Scobey::Pause;
 14: 
 15: //Starting a C++ class

 16: class Person
 17: {
 18: public: //<--We have a "public section"; don't have to label each thing public

 19:     void setName //a "setter" method for the class

 20:         (
 21:         const string& theName //

 22:         )
 23:     {
 24:         name = theName;
 25:     }
 26: 
 27: private: //<--We have a "private section"; don't have to label each thing public

 28:     string name;
 29:     int age;
 30: };//<--Note the semi-colon; not present in Java

 31: 
 32: 
 33: int main(int argc, char* argv[])
 34: {
 35:     //vector<int> vi; //an empty vector of integers

 36:     //cout << vi.empty() << endl; //outputs 1

 37:     //cout << boolalpha << vi.empty() << endl; //outputs true

 38:     //boolalpah is, line endl a "manipulator", which you get "for free" here

 39:     //in Visual C++, but technically you should #include <iomanip>

 40: 
 41:     //vector<int> vi(4); //a vector of integers of size 4, containing 4 zeroes

 42:     //vector<int> vi(6,12); //a vector of integers of size 6, containing 6 12s

 43: 
 44:     //Outputs the contents of a vector just as though it was an array,

 45:     //except it uses size_type for the index type, just like string

 46:     //for (vector<int>::size_type i = 0; i < vi.size(); i++)

 47:     //{

 48:     //    cout << vi[i] << endl;

 49:     //}

 50: 
 51:     //The at() method for the indes is safer than the square 

 52:     //brackets used above, and should be preferred

 53:     //for (vector<int>::size_type i = 0; i <= vi.size(); i++)

 54:     //{

 55:     //    cout << vi.at(i) << endl;

 56:     //}

 57: 
 58:     //Initializing a vector

 59:     //vector<int> vi1{ 2, 4, 6, 8, 10 }; //only since C++11

 60: 
 61:     //Simplest and best way to process (output, in this case) a compete vector

 62:     //for (int i : vi1) cout << i << " "; //only since C++11

 63:     //cout << endl;

 64: 
 65:     //vector<string> vs(3);

 66:     //vs.at(1) = "Hello";

 67:     //for (string s : vs) cout << s << endl;

 68: 
 69:     //push_back() method adds an element to the end of a vector

 70:     //vs.push_back("one");

 71:     //vs.push_back("two");

 72:     //vs.push_back("three");

 73:     //for (string s : vs) cout << s << endl;

 74:     //cout << vs.size() << endl;

 75: 
 76:     //Note the following example carefully ... make sure you understand it!

 77:     //vector<int> v(3);

 78:     //v.push_back(1);

 79:     //v.push_back(2);

 80:     //v.push_back(3);

 81:     ////cout << v.at(1) << endl;

 82:     //for (int i : v) cout << i << endl;

 83: 
 84:     //vector<int> v1;

 85:     //v1 = v;

 86:     //v.at(1) = 333;

 87:     //for (int i : v1) cout << i << endl;

 88:     //for (int i : v) cout << i << endl;

 89: 
 90:     //do

 91:     //{

 92: 
 93:     //} while (true); <-- VS2013 formats a do...while like this

 94: }