Advertisement
TermSpar

Vector Classes

Apr 21st, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. //Users.h Code
  2.  
  3. #ifndef USERS_H
  4. #define USERS_H
  5.  
  6. #include<iostream>
  7. #include<string>
  8.  
  9. using namespace std;
  10.  
  11. class Users{
  12.  
  13. public:
  14. //Functions
  15.  
  16.     //Default Constructor:
  17.     Users();
  18.  
  19.     //Overload Constructor:
  20.     Users(string, string, int);
  21.  
  22.     //Deconstructor:
  23.     ~Users();
  24.  
  25.     //Acessor Functions:
  26.     string getUsername() const;
  27.  
  28.     string getPassword() const;
  29.  
  30.     int getUserID() const;
  31.  
  32.  
  33. private:
  34. //Member variables
  35.     string newUsername;
  36.     string newPassword;
  37.     int userID;
  38. };
  39.  
  40.  
  41. #endif
  42.  
  43. //Users.cpp code:
  44.  
  45. #include "Users.h"
  46.  
  47.  
  48. //Default Constructor:
  49.  
  50. Users::Users(){
  51.     userID = 0;
  52. }
  53.  
  54. //Overload Constructor:
  55.  
  56. Users::Users(string name, string password, int ID ){
  57.     newUsername = name;
  58.     newPassword = password;
  59.     userID = ID;
  60. }
  61.  
  62. Users::~Users(){
  63.    
  64. }
  65.  
  66. //Acessor Functions:
  67.  
  68. string Users::getUsername() const{
  69.     return newUsername;
  70. }
  71.  
  72. string Users::getPassword() const{
  73.     return newPassword;
  74. }
  75.  
  76. int Users::getUserID() const{
  77.     return userID;
  78. }
  79.  
  80.  
  81. //Main.cpp code:
  82. #include<iostream>
  83. #include<string>
  84. #include<vector>
  85. #include "Users.h"
  86.  
  87. using namespace std;
  88.  
  89. void createVec(vector<Users>&); //Create vector function (Add users to vector)
  90. void showVec(const vector<Users>&); //Show vector function (Display users in vector)
  91.  
  92. int main(){
  93.  
  94.     vector<Users> uVec;
  95.  
  96.     createVec(uVec);
  97.     showVec(uVec);
  98.  
  99.     system("pause");
  100.     return 0;
  101. }
  102.  
  103. void createVec(vector<Users>& newVec){
  104.    
  105.     int lSize;
  106.     string username;
  107.     string password;
  108.     int ID;
  109.  
  110.     cout << "Enter user list size: "; //Have user define number of loops
  111.     cin >> lSize;
  112.  
  113.     for(int i = 0; i < lSize; i++){
  114.         //Loop logic
  115.         cout << "Username: ";
  116.         cin >> username;
  117.         cout << "Password: ";
  118.         cin >> password;
  119.         cout << "ID Number: ";
  120.         cin >> ID;
  121.         cout << "\nUser: " << username << " added\n";
  122.         cout << endl;
  123.  
  124.         Users newUser(username, password, ID); //Create user with given information
  125.         newVec.push_back(newUser); //Add it to vector
  126.     }
  127. }
  128.  
  129. void showVec(const vector<Users>& newVec){
  130.    
  131.     unsigned int vecSize = newVec.size(); //So C++ doesn't have to keep referencing Users.h (Saving time)
  132.  
  133.     cout << "-----------" << endl;
  134.     cout << "User Data:" << endl;
  135.     cout << endl;
  136.  
  137.     for(unsigned int i = 0; i < vecSize; i++){
  138.        
  139.         cout << "Username: " << newVec[i].getUsername() << endl;
  140.         cout << "Password: " << newVec[i].getPassword() << endl;
  141.         cout << "ID: " << newVec[i].getUserID() << endl;
  142.         cout << endl;
  143.  
  144.     }
  145.     cout << endl;
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement