Skip to content

Instantly share code, notes, and snippets.

@dvtate
Created May 28, 2016 19:51
Show Gist options
  • Save dvtate/23e39de39b0bb81c9ce4c9a372c3a998 to your computer and use it in GitHub Desktop.
Save dvtate/23e39de39b0bb81c9ce4c9a372c3a998 to your computer and use it in GitHub Desktop.
tiny C++ OOP demonstration
#include <iostream> //the library we will be using
using namespace std;//no need to add the 'std::' scope
class Fish{
//Not the proper way to define a class
// should be in a separate fish.h file with an implementation fish.cpp file
private:
int _length;//how long
string _name, _type;
public:
//fishy functions:
Fish(string type, string name, int length);//constructor
void showfish();//depict the fish
void aboutfish();//give some stats on the fish
//length functions:
void grow(int length){_length+=length;}//make bigger
int getlength(){return _length;}
//name functions:
string getname(){return _name;}
void setname(string name){_name=name;}//change the fish's name
//type functions:
string gettype(){return _type;}
};
Fish::Fish(string type, string name, int length=1){
_type=type;
_name=name;
_length=length;
}
void Fish::showfish(){//rough(text) depiction of fish
cout<<"<";//print head
for(int i=0;i<_length;i++){//number of equal-signs = length
cout<<"=";
}cout<<"><\n";//print tail
}
void Fish::aboutfish(){//prints some fishy statistics
cout<<"~~About Fish~~~~~~~~\n";
cout<<"Name:"<<getname() <<"\nType: " <<gettype() <<"\nLength: "<<getlength() <<"\nLooks like: ";
showfish();
cout<<"~~~~~~~~~~~~~~~~~~~~\n";
}
int main(){
Fish bubbles("goldfish","Bubbles",8);
bubbles.showfish();
bubbles.aboutfish();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment