類繼承練習題二
2、在Animal類中,把who()函數(shù)的訪問指定符改為protected,但類的其他內(nèi)容不變。現(xiàn)在修改派生類,使原來的main()函數(shù)仍然工作。

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
protected:
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 lion1("Leo", 400);
Aardvark aardvark1("Algernon", 50);
lion1.who();
aardvark1.who();
}


// 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
protected:
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 lion1("Leo", 400);
Aardvark aardvark1("Algernon", 50);
lion1.who();
aardvark1.who();
}
posted on 2009-03-26 17:15 luqingfei 閱讀(449) 評論(0) 編輯 收藏 引用 所屬分類: C++基礎