Implementations of the Lion, Fish class and classes derived from Animal - C++ Class

C++ examples for Class:Inheritance

Description

Implementations of the Lion, Fish class and classes derived from Animal

Demo Code

                                                
#include <string>
#include <iostream>
#include <string>
using std::string;
                                                
class Animal/* w  w  w  . java 2 s . c om*/
{
private:
  string name;                      // Name of the animal
  int weight;                       // Weight of the animal
                                                
public:
  Animal(string aName, int wt);     // Constructor
  void who() const;                 // Display name and weight
};
                                                
class Lion: public Animal
{
public:
  Lion(string aName, int wt):Animal(aName, wt) {}
};
                                                
class Fish: public Animal
{
public:
  Fish(string aName, int wt):Animal(aName, wt){}
};
using std::string;
                                                
// Constructor
Animal::Animal(string aName, int wt) : name(aName), weight(wt) {}
                                                
// Identify the animal
void Animal::who() const
{
  std::cout << "My name is " << name << " and I weigh " << weight << "lbs." << std::endl;
}
                                                
int main()
{
  Lion myLion("Leo", 400);
  Fish myFish("Algernon", 50);
  myLion.who();
  myFish.who();
}

Result


Related Tutorials