類繼承練習題三
3、在上一題中,把基類成員who()的訪問指定符改為public,但把who()函數實現為每個派生類的成員,且在輸出消息中顯示派生類名?,F在修改main()函數,為每個派生類對象調用who()的基類版本和派生類版本。

Animal.h
// Animal.h
// Animal clas and classes derived from Animal
#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
using std::string;
class Animal {
public:
Animal(string theName, int wt); // Constructor
void who() const; // Display name and weight
private:
string name; // Name of the animal
int weight; // Weight of the animal
};
class Lion: public Animal {
public:
Lion(string theName, int wt): Animal(theName, wt) {}
void who() const;
};
class Aardvark: public Animal {
public:
Aardvark(string theName, int wt): Animal(theName, wt) {}
void who() const;
};
#endif

Animal.cpp
// Animal.cpp
// Implementations of the Animal class and classes derived from Animal
#include "Animal.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
// Constructor
Animal::Animal(string theName, int wt): name(theName), weight(wt) { }
// Identify the animal
void Animal::who() const {
cout << "\nMy name is " << name << " and I weight " << weight << endl;
}
// Identify the Lion
void Lion::who() const {
cout << "I'm lion: ";
Animal::who(); // Call protected base function
}
// Identify the Aardvark
void Aardvark::who() const {
cout << "I'm aardvark: ";
Animal::who(); // Call protected base function
}

main.cpp
// main.cpp
#include <iostream>
#include "Animal.h"
using std::cout;
using std::endl;
void main() {
Lion myLion("Leo", 400);
Aardvark myAardvark("Algernon", 50);
cout << "\nCalling derived versions of who():\n";
myLion.who();
myAardvark.who();
cout << "\n\nCalling base versions of who():";
myLion.Animal::who();
myAardvark.Animal::who();
cout << endl;
}
// Animal.h
// Animal clas and classes derived from Animal
#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
using std::string;
class Animal {
public:
Animal(string theName, int wt); // Constructor
void who() const; // Display name and weight
private:
string name; // Name of the animal
int weight; // Weight of the animal
};
class Lion: public Animal {
public:
Lion(string theName, int wt): Animal(theName, wt) {}
void who() const;
};
class Aardvark: public Animal {
public:
Aardvark(string theName, int wt): Animal(theName, wt) {}
void who() const;
};
#endif
// Animal.cpp
// Implementations of the Animal class and classes derived from Animal
#include "Animal.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
// Constructor
Animal::Animal(string theName, int wt): name(theName), weight(wt) { }
// Identify the animal
void Animal::who() const {
cout << "\nMy name is " << name << " and I weight " << weight << endl;
}
// Identify the Lion
void Lion::who() const {
cout << "I'm lion: ";
Animal::who(); // Call protected base function
}
// Identify the Aardvark
void Aardvark::who() const {
cout << "I'm aardvark: ";
Animal::who(); // Call protected base function
}
// main.cpp
#include <iostream>
#include "Animal.h"
using std::cout;
using std::endl;
void main() {
Lion myLion("Leo", 400);
Aardvark myAardvark("Algernon", 50);
cout << "\nCalling derived versions of who():\n";
myLion.who();
myAardvark.who();
cout << "\n\nCalling base versions of who():";
myLion.Animal::who();
myAardvark.Animal::who();
cout << endl;
}
posted on 2009-03-26 17:29 luqingfei 閱讀(466) 評論(0) 編輯 收藏 引用 所屬分類: C++基礎

