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 (file reading version) —
#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); if (infile.is_open()) { string line; string type; int aweight; string name; char achar; while (getline(infile, line)) { cout << line << endl; 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 { 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 (auto x : animals) { cout << x->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"); }
— source.cpp (cin reading version) —
#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"); }
— Animal.h —
#pragma once #include<string> using namespace std; class Animal { public: Animal(string aname, double aweight); ~Animal(); virtual string toString(); virtual bool needsvisit() = 0; protected: string name; double weight; };
— Animal.cpp —
#include<iostream> #include<sstream> #include<string> #include "Animal.h" Animal::Animal(string aname, double aweight) { name = aname; weight = aweight; } Animal::~Animal() { } string Animal::toString() { ostringstream tempstream; tempstream << "Name: >" << name << "<\tweight: " << weight; return tempstream.str(); }
— Cat.h —
#pragma once #include "Animal.h" class Cat : public Animal { public: Cat(string aname, double weight, bool hadcalicivaccine); ~Cat(); virtual string toString(); virtual bool needsvisit(); private: bool calicivaccine; };
— Cat.cpp —
#include<iostream> #include<sstream> #include<string> #include "Cat.h" Cat::Cat(string aname, double aweight, bool hadcalicivaccine) : Animal(aname, aweight) { calicivaccine = hadcalicivaccine; } Cat::~Cat() { } string Cat::toString() { ostringstream tempstream; tempstream << Animal::toString(); if (calicivaccine) { tempstream << "\tHas had vaccine"; } else { tempstream << "\tHas NOT had vaccine"; } return tempstream.str(); } bool Cat::needsvisit() { if (!calicivaccine) { return true; } return false; }
— Dog.h —
#pragma once #include "Animal.h" class Dog : public Animal { public: Dog(string aname, double aweight); ~Dog(); virtual bool needsvisit(); };
— Dog.cpp —
#include<iostream> #include<string> #include "Dog.h" Dog::Dog(string aname, double aweight) : Animal(aname, aweight) { } Dog::~Dog() { } bool Dog::needsvisit() { const double MAXWEIGHT = 30.0; if (weight > MAXWEIGHT) { return true; } return false; }
Here is some data from this example:
Dog 20 Bo Schembechler Dog 50 Wrecker Dog Dog 15 Dog 16 Cat Cat 7 1 E.T. Cat 83 0 Fritz Parrot 13 Albert Einstein junkBack to top