Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
cs-142:inheritance3 [2015/11/18 23:02]
kseppi
cs-142:inheritance3 [2018/04/18 21:28] (current)
kseppi
Line 1: Line 1:
-This implementation uses several files to implement the inheritance. ​ This is a more interesting version. It reads data in from a file and and uses virtual and pure virtual methods.+This implementation uses several files to implement the inheritance. ​ This is a more interesting version. It reads data in from a file and and uses virtual and pure virtual methods. There is another version source.cpp (included at the bottom of this page) that reads from cin.
  
---- source.cpp ---+--- source.cpp ​(file reading version) ​---
 <code c++> <code c++>
 #include <​iostream>​ #include <​iostream>​
Line 90: Line 90:
  
  system("​pause"​);​  system("​pause"​);​
 +}
 +</​code>​
 +
 +--- source.cpp (cin reading version) ---
 +<code c++>
 +#include <​iostream>​
 +#include <​fstream>​
 +#include <​vector>​
 +#include <​sstream>​
 +#include <​string>​
 +
 +#include "​Animal.h"​
 +#include "​Dog.h"​
 +#include "​Cat.h"​
 +
 +using namespace std;
 +
 +
 +int main() {
 +   ​vector<​Animal *> animals;
 +   ​string filelocation;​
 +
 +   /​*cout << "What file do you want to read the data from? ";
 +   ​getline(cin,​ filelocation);​ // Important to use getline here, there may be blanks in the file name
 +   // Also remove "s, windows adds them in some cases
 +   if (filelocation.substr(0,​ 1) == "​\""​) {
 +   ​filelocation = filelocation.substr(1,​ filelocation.length() - 2);
 +   }
 +
 +   ​ifstream infile;
 +
 +   cout << "FYI, reading from" << filelocation << endl;
 +   ​ifstream myfile(filelocation);​ // Open the file at the location the user asks for
 +   ​infile.open(filelocation);​
 +   */
 +
 +   cout << "Dog weight name" << endl
 +      << "Cat weight hadvac name" << endl
 +      << "enter data, use STOP to stop" << endl << endl;
 +
 +   ​string line;
 +   ​string type;
 +   int aweight;
 +   ​string name;
 +   char achar;
 +
 +   while (getline(cin,​ line)) {
 +
 +      istringstream tempstream(line);​
 +      if (tempstream >> type) {
 +         if (type == "​Dog"​) {
 +            if ((tempstream >> aweight) &&
 +               ​(tempstream.get(achar)) &&
 +               ​(getline(tempstream,​ name))) {
 +               ​animals.push_back(new Dog(name, aweight));
 +            }
 +            else {
 +               cout << "Bad dog! : " << line << endl;
 +            }
 +         }
 +         else if (type == "​Cat"​) {
 +            bool hadvaccine;
 +            if ((tempstream >> aweight >> hadvaccine) &&
 +               ​(tempstream.get(achar)) &&
 +               ​(getline(tempstream,​ name))) {
 +               ​animals.push_back(new Cat(name, aweight, hadvaccine));​
 +            }
 +            else {
 +               cout << "Bad cat : " << line << endl;
 +            }
 +         }
 +         else if (type == "​STOP"​) {
 +            break;
 +         }
 +         else {
 +            cout << "Not a known kind of animal: " << line << endl;
 +         }
 +      }
 +      else {
 +         cout << "bad line: " << line << endl;
 +      }
 +   }
 +
 +
 +   cout << "​\nHere is the list of animals I found:​\n";​
 +   for (int i = 0; i < animals.size();​ i++) {
 +      cout << animals.at[i]->​toString() << endl;
 +   }
 +
 +   cout << "​\nHere is the list of animals that need a visit:​\n";​
 +   for (auto x : animals) {
 +      if (x->​needsvisit()) {
 +         cout << x->​toString() << endl;
 +      }
 +   }
 +   cout << "End of list of anmals needing a visit\n";​
 +
 +   ​system("​pause"​);​
 } }
 </​code>​ </​code>​
cs-142/inheritance3.txt ยท Last modified: 2018/04/18 21:28 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0