Caminhoneiro

C++Class

Jan 4th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class Animal{
  7.    
  8.     //Attributes : height weitgh variablees
  9.     //Capabilites : Run Eat function / methods
  10.  
  11.     private:
  12.         int height;
  13.         int weight;
  14.         string name;
  15.        
  16.        
  17.         static int numOfAnimals;
  18.        
  19.     public:
  20.         int getHeigth(){ return heigth; }
  21.         int getWeigth(){ return weight; }
  22.         string getName(){ return name; }
  23.        
  24.         void setHeigth(int cm) { height = cm;}
  25.         void setWeigth(int kg) { weight= kg;}
  26.         void setName(string animalName) { name = animalName;}
  27.        
  28.         void setAll(int, int, string)
  29.        
  30.         //EXAMPLE OVERLOAD
  31.         Animal(int, int, string);
  32.        
  33.         ~Animal(); //dESTRUCTOR
  34.        
  35.         Animal(); //Another constructor
  36.    
  37.    
  38.         static int getNumOfAnimals() { return numOfAnimals; }
  39.        
  40.         void toString();
  41.        
  42. };
  43.  
  44. int Animal::numOfAnimals = 0;
  45.  
  46. void Animal::setAll(int height, int weight, string name){
  47.    
  48.     this -> height = height;
  49.     this -> weight = weight;
  50.     this -> height = height;
  51.     Animal::numOfAnimals++;
  52.    
  53. }
  54.  
  55. //Do the same thing from above
  56. Animal::Animal(int height, int weight, string name)
  57. {
  58.    
  59.     this -> height = height;
  60.     this -> weight = weight;
  61.     this -> name = name;
  62.     Animal::numOfAnimals++;
  63. }
  64.  
  65. //Desconstructor
  66.  
  67. Animal::~Animal()
  68. {
  69.    
  70.     cout << "Animal" << this -> name << "Destroyed" << endl;
  71. }
  72.  
  73. //OVERLOAD
  74. Animal::Animal()
  75. {
  76.     Animal::numOfAnimals++;
  77. }
  78.  
  79. void Animal::toString(){
  80.     cout << this -> name << this -> height <<
  81.         "cms tall and " << this -> weight << "kgs in weight" << endl;
  82. }
  83.  
  84. class Dog : public Animal{
  85.  
  86. private:
  87.     string sound = "Woof";
  88.    
  89. public:
  90.     void getSound(){ cout << sound << endl; }
  91.  
  92.     Dog(int, int, string, string);
  93.    
  94.     Dog() : Animal(){};
  95.    
  96.     void toString();
  97.  
  98. };
  99.  
  100. Dog::Dog(int height, int weight, string name, string bark) :
  101. Animal(height, weightm name){
  102.     this -> sound = bark;
  103. }
  104.  
  105. void Dog::toString(){
  106.  
  107.     cout << this -> getName() << "is" << this -> getHeigth() << "cms tall and " << this -> getWeigth() << "kgs in weight and says " << this -> sound << endl;
  108.  
  109. }
  110.  
  111. int main(){
  112.    
  113.     Animal fred;
  114.    
  115.     fred.setHeigth(33);
  116.     fred.setWeigth(10);
  117.     fred.setName("Fred");
  118.    
  119.     cout << fred.getName() << " is " << fred.getHeigth() <<
  120.         "cms tall and" << fred.getWeigth() << "kgs n weight" << endl;
  121.        
  122.     Animal tom(36, 15, "Tom");
  123.    
  124.    
  125.     cout << tom.getName() << " is " << tom.getHeigth() <<
  126.         "cms tall and" << tom.getWeigth() << "kgs n weight" << endl;
  127.        
  128.     Dog spot(38, 16, "Spot", "Woof");
  129.    
  130.     cout << "Number of Animals << Animal::getNumOfAnimals() << endl;
  131.    
  132.     spot.getSound();
  133.    
  134.     tom.toString();
  135.    
  136.     //Super class
  137.     spot.Animal::toString();
  138.    
  139.     return 0;
  140.    
  141. }
Add Comment
Please, Sign In to add comment