C++ Class Inheritance Lion, Fish class and classes derived from Animal

Description

C++ Class Inheritance Lion, Fish class and classes derived from Animal

 
#include <string>
#include <iostream>
#include <string>
using std::string;
 
class Animal//from  w  w  w.j a  v a2  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();
}



PreviousNext

Related