Skip to content

Instantly share code, notes, and snippets.

@dvtate
Last active November 27, 2016 20:01
Show Gist options
  • Save dvtate/08a66c1fbf625af331e999d5c2b5614e to your computer and use it in GitHub Desktop.
Save dvtate/08a66c1fbf625af331e999d5c2b5614e to your computer and use it in GitHub Desktop.
ULTIMATE FISH OOP DEMONSTRATION C++
#ifndef ANIMAL_H
#define ANIMAL_H
#include <iostream>
class Animal {
public:
//some things that animals do:
template <typename T>
void say(const T quote){
std::cout <<quote;
}
virtual void eat(){
std::cout <<"om-nom-nom-nom\n";
}
// this tells the compiler that we haven't allocated any
// memory so it's safe to just let everything simply go
// out of scope.
virtual ~Animal(){ }
// this is a pure virtual function that must be overloaded
virtual void move() = 0;
virtual void makeNoise() = 0;
};
#endif
#include <iostream>
#include "libFish.hpp" //has <iostream> included
/*Object instantiations:
*Please read: http://stackoverflow.com/questions/8839943/why-does-the-use-of-new-cause-memory-leaks/8840302#8840302
*
*There are several ways to instantiate an object here are 3 (that you might encounter):
*1- `Fish myFish(args);` // this makes an object
*2- `Fish myFish = Fish(args);` // this copies the object using the copy constructor
*3- `Fish* myFish = new Fish(args);` // this makes a pointer to the object
*/
int main(){
//instantiate the lostfish onto the stack (reccomended)
Fish lostFish("Clown Fish","Nemo",1);
//run methods using the dot (`.`) operator
lostFish.aboutFish();
//show the fish grow(ing)
for (int i = 0; i < 10; i++) {
//increase length (default is to add 1)
lostFish.grow();
//say it's new length
lostFish.say("Now I'm ");
lostFish.sayAnonymously(lostFish.getLength());
lostFish.sayAnonymously(" long!\t");
//show off that body
lostFish.show();
}
//create a pointer to an object (gives more control because it's in the stack)
Fish* crazyFish = new Fish("Blue Tang", "Dory",3);
//when using a pointer to an object, use the `->` operator (looks like an arrow)
crazyFish->aboutFish();
crazyFish->move();
//always delete objects created using the new keyword, or else you get a memory leak
delete crazyFish;
//you can't delete objects unless they were made with the `new` keyword, they get deleted automatically.
std::cout <<std::endl;
lostFish.say("I'm lonely now.");
std::cout <<std::endl;
//no happy endings here :S
}
#include "libFish.hpp" //has <iostream> included
void Fish::show(){ // rough(text) depiction of fish
std::cout <<"<"; // print head
// print '=' to show length
for (int i = 0; i < _length; i++) // remember, single statement loops don't need braces `{}`
std::cout<<"=";
std::cout <<"><" <<std::endl;// print tail
}
void Fish::aboutFish(){ // print stats
std::cout <<"~~About Fish~~~~~~~~"
<<"\nName: " <<getName()
<<"\nType: " <<getType()
<<"\nLength: " <<getLength()
<<"\nLooks like: ";
show();//print the body
std::cout <<"~~~~~~~~~~~~~~~~~~~~" <<std::endl;
}
// inherited from animal
// redefining virtual functions
void Fish::move(){
for (int i = 0; i < 3; i++)
std::cout <<"\njust keep swimming";
std::cout <<" swimming swmming" <<std::endl;
}
#ifndef LIBFISH_H
#define LIBFISH_H
#include <iostream> //std::cout , std::string
#include "animal.h"
//class fish is derived from the class animal
class Fish : public Animal {
private:
int _length;//how long
const char *_name, *_type;
public:
// note that all functions defined within this class are inline functions.
// ( http://www.cprogramming.com/tutorial/lesson13.html )
// the ones which are defined in libFish.cpp aren't
//constructor
Fish(const char* type, const char* name, const int length):
_length(length), //Note: this needs to be in the same order that the members were declared
_name(name),
_type(type)
{
std::cout <<"\nEnter " <<name <<'.' <<std::endl;
}
//destructor
~Fish() // called when we `delete` our fish or when it goes out of scope
{ std::cout <<getName() <<" has died." <<std::endl; }
//fishy functions:
void show();//depict the fish
void aboutFish();//give some stats on the fish
//length functions:
// make bigger
void grow(const int length = 1)
{ _length += length; }
int getLength()
{ return _length; }
//name functions:
const char* getName()
{ return _name; }
// change the fish's name (not that it really matters)
void setName(const char* name)
{ _name = name; }
// type functions:
const char* getType()
{ return _type; }
const char* getSpecies()
{ return getType(); } // this function is exactly the same as getType() (just as efficient to call too)
// methods inherited from Animal
// I have to overload this pure-virtual function
void move(); // defined elsewhere
void makeNoise()
{ std::cout <<"blub-blub-blub\n"; }
template <typename T> void say(const T quote)
{ std::cout <<_name <<": " <<quote; }
// sometimes we don't want the name to be printed...
template <typename T> void sayAnonymously(const T quote)
{ std::cout <<quote; }
//NOTE: because I didn't overload eat() it uses the definition from
// the Animal base class
};
#endif
@dvtate
Copy link
Author

dvtate commented Jun 4, 2016

[tate@archbook fishy]$ g++ *.cpp -Wall -Wextra && ./a.out 

Enter Nemo.
~~About Fish~~~~~~~~
Name: Nemo
Type: Clown Fish
Length: 1
Looks like: <=><
~~~~~~~~~~~~~~~~~~~~
Nemo: Now I'm 2 long!	<==><
Nemo: Now I'm 3 long!	<===><
Nemo: Now I'm 4 long!	<====><
Nemo: Now I'm 5 long!	<=====><
Nemo: Now I'm 6 long!	<======><
Nemo: Now I'm 7 long!	<=======><
Nemo: Now I'm 8 long!	<========><
Nemo: Now I'm 9 long!	<=========><
Nemo: Now I'm 10 long!	<==========><
Nemo: Now I'm 11 long!	<===========><

Enter Dory.
~~About Fish~~~~~~~~
Name: Dory
Type: Blue Tang
Length: 3
Looks like: <===><
~~~~~~~~~~~~~~~~~~~~

just keep swimming
just keep swimming
just keep swimming swimming swmming
Dory has died.

Nemo: I'm lonely now.
Nemo has died.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment