C++语言程序设计(郑莉)第八章多态练习题

例1:定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。

#include 
#include 
using namespace std;

class Mammel {
     
private:
	string name;
	int weight;
public:
	Mammel() {
     
		cout << "正在默认构造Mammel对象。" << endl;
	}
	Mammel(string name, int weight) {
     
		this->name = name;
		this->weight = weight;
	}
	~Mammel();
	virtual void speak();
};
void Mammel::speak() {
     
	cout << "Mammel" << endl;
}
Mammel::~Mammel() {
     
	cout << "正在析构Mammel对象。" << endl;
}
class Dog :public Mammel {
     
private:
	int age;
public:
	Dog() {
     
		cout << "正在默认构造Dog对象。" << endl;
	}
	Dog(string name, int weight, int age);
	~Dog();
	void speak();
};
Dog::Dog(string name, int weight, int age) :Mammel(name, weight), age(age) {
     
	cout << "正在构造Dog对象。" << endl;
}
Dog::~Dog() {
     
	cout << "正在析构Dog对象。" << endl;
}
void Dog::speak() {
     
	cout << "Dog." << endl;
}
int main() {
     
	Dog mydog("haha", 20, 2);
	mydog.speak();			//Dog
	Mammel* animal1 = &mydog;
	(*animal1).speak();		//Dog
	Mammel& animal2 = mydog;
	animal2.speak();			//Dog
	Mammel animal3 = mydog;
	animal3.speak();			//Mammel
	//通过以上测试我们可以看出派生类转化为基类的三种方法:赋值、指针、引用
	//只能通过指针,引用来访问虚函数

	return 0;
}

例2:对类Point重载++(自增),-–(自减)运算符,要求同时重载前缀和后缀的形式。本答案也重载了‘+’, ‘-’, 实现连个点的加减。

#include 
using namespace std;

class Point {
     
private:
	int x, y;
public:
	Point(int xx = 0, int yy = 0) : x(xx), y(yy) {
     }
	Point(const Point & p) : x(p.x), y(p.y) {
     }
	~Point() {
     }
	Point& operator++();
	Point operator++(int);
	Point& operator--();
	Point operator--(int);
	Point operator+(const Point& p2);
	Point operator-(const Point& p2);
	void ShowPoint() {
      cout << "x: " << x << " y: " << y << endl; }
};
Point& Point::operator++() {
     
	x++;
	y++;
	return *this;
}
Point Point::operator++(int) {
     
	Point temp = *this;
	++(*this);
	return temp;
	
	//另一种写法,效果一样
	/*	
	int nx = this->x;
	int ny = this->y;
	++(*this);
	return Point(nx, ny);
	*/

}
Point& Point::operator--() {
     
	x--;
	y--;
	return *this;
}
Point Point::operator--(int) {
     
	Point temp = *this;
	--(*this);
	return temp;
}
Point Point::operator+(const Point& p2) {
     
	return Point(x + p2.x, y + p2.y);
}
Point Point::operator-(const Point& p2) {
     
	return Point(x - p2.x, y - p2.y);
}

int main() {
     
	cout << "后置加加:" << endl;
	Point n1(2, 1);
	n1.ShowPoint();
	(n1++).ShowPoint();
	n1.ShowPoint();

	cout << endl << endl;
	cout << "前置++:" << endl;
	Point n2(3, 1);
	n2.ShowPoint();
	(++n2).ShowPoint();
	n2.ShowPoint();

	cout << endl << endl;
	cout << "重载家减:" << endl;
	Point n3(1, 1), n4(1, 1);
	cout << "n5 = n3 + n4: " << endl;
	Point n5 = n3 + n4;
	n5.ShowPoint();
	cout << "n6 = n3 - n4: " << endl;
	Point n6 = n3 - n4;
	n6.ShowPoint();

	return 0;
}

例3:编写并测试一个计数器Counter类,对其重载运算符+。

#include 
using namespace std;
class Counter {
     
private:
	int a;
public:
	Counter() {
     }
	Counter(int a):a(a){
     }
	~Counter(){
     }
	Counter operator+ (Counter& t);
	void Show() {
      cout << a << endl; }
};
Counter Counter::operator+ (Counter& t) {
     
	return Counter(a + t.a);
}
int main() {
     
	Counter a(1), b(2), c;
	c = a + b;  //c.operator+(b)
	c.Show();

	return 0;
}

你可能感兴趣的:(C++语言程序设计(郑莉)第八章多态练习题)