Created
April 17, 2014 02:15
-
-
Save calebreister/10948397 to your computer and use it in GitHub Desktop.
In class operator overloading and template examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DOG_HH_ | |
#define DOG_HH_ | |
#include <ostream> | |
class Dog { | |
private: | |
int weightLbs; | |
int ageYears; | |
public: | |
Dog(); | |
Dog(int weightLbs, int ageYears); | |
int getAge(); | |
int getAgeDogYears(); | |
friend std::ostream& operator<<(std::ostream& stream, const Dog& dog) { | |
return stream << "Age: " << dog.ageYears << " Weight: " << dog.weightLbs; | |
} | |
friend bool operator==(const Dog& dog1, const Dog& dog2) { | |
return (dog1.weightLbs == dog2.weightLbs) && (dog1.ageYears == dog2.ageYears); | |
} | |
friend bool operator!=(const Dog& dog1, const Dog& dog2) { | |
return !(dog1 == dog2); | |
} | |
friend Dog operator+(const Dog& dog1, const Dog& dog2) { | |
Dog out; | |
out.ageYears = dog1.ageYears + dog2.ageYears; | |
out.weightLbs = dog1.weightLbs + dog2.weightLbs; | |
return out; | |
} | |
}; | |
Dog::Dog() { | |
weightLbs = 0; | |
ageYears = 0; | |
} | |
Dog::Dog(int weightLbs, int ageYears) { | |
this->weightLbs = weightLbs; | |
this->ageYears = ageYears; | |
} | |
int Dog::getAge() { | |
return ageYears; | |
} | |
int Dog::getAgeDogYears() { | |
return ageYears * 7; | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SCRATCHPAD_HH_ | |
#define SCRATCHPAD_HH_ | |
#include <iostream> | |
const int MAX_ITEMS = 10; | |
//unordered list | |
template <class type> | |
class List { | |
private: | |
type data[MAX_ITEMS]; | |
int totalItems; | |
public: | |
List(); | |
bool insert(type dataToAdd); | |
void print(); | |
}; | |
template <class type> | |
List<type>::List() { | |
totalItems = 0; | |
} | |
template <class type> | |
bool List<type>::insert(type dataToAdd) { | |
if (totalItems >= MAX_ITEMS) | |
return false; | |
data[totalItems++] = dataToAdd; | |
return true; | |
} | |
template <class type> | |
void List<type>::print() { | |
for (int i = 0; i < totalItems; i++) { | |
std::cout << data[i] << std::endl; | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include "Dog.hh" | |
#include "ScratchPad.hh" | |
using namespace std; | |
int main() { | |
List<Dog> dogs; | |
Dog d(30, 5); | |
dogs.insert(d); | |
d = Dog(50, 2); | |
dogs.insert(d); | |
dogs.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment