Bootstrap

C++程序设计(谭浩强)课后习题第五章第五题和第六章第四、五题

第五章

5.(1)可以用b1.i引用对象b1中的基类A的成员i,因为它是公用数据成员。不能用b1.j和b1.k引用对象b1中的基类A的成员j和k,因为j和k分别是保护数据成员和私有数据成员,在类外都不能访问。

修改之后:

#include<iostream>
using namespace std;
class A
{public:
	void f1();
	int i;
	int j;
	int k;
 protected:
 	void f2();
};
class B: public A
{public:
	void f3();
 protected:
 	int m;
 private:
 	int n;
};
class C: public B
{public:
	void f4();
 private:
 	int p;
};
int main()
{
	A a1;
	B b1;
	C c1;
	b1.i;
	b1.j;
	b1.k;
	return 0;
}

(2)可以调用基类A中的成员函数f1和f2,因为f1是公用成员函数,f2是保护成员函数,B对A是公有继承方式,因此它们在派生类中仍然保持原有的访问权限,可以被派生类的成员函数访问。

#include<iostream>
using namespace std;
class A
{public:
	void f1();
	int i;
 protected:
 	void f2();
 	int j;
 private:
 	int k;
};
class B: public A
{public:
	void f3(){A::f1();A::f2();};
 protected:
 	int m;
 private:
 	int n;
};
class C: public B
{public:
	void f4();
 private:
 	int p;
};
int main()
{
	A a1;
	B b1;
	C c1;
	return 0;
}

(3)可以引用基类A中的数据成员i和j,因为它们在派生类中是公用成员和保护成员,可以被派生类的成员函数访问。不可以引用基类A中的数据成员k,它在派生类中是不可访问的成员。

修改之后:

#include<iostream>
using namespace std;
class A
{public:
	void f1();
	int i;
 protected:
 	void f2();
	int j;
	int k; 	
};
class B: public A
{public:
	void f3(){int i,j,k;};
 protected:
 	int m;  
 private:
 	int n;
};
class C: public B
{public:
	void f4();
 private:
 	int p;
};
int main()
{
	A a1;
	B b1;
	C c1; 
	b1.f3();
	return 0;
}

(4)可以用c1.i引用对象c1中基类A的成员i,不能用c1.j和c1.k引用基类A的成员j和k,因为它们是保护成员和私有成员,不能被类外访问,也不能访问c1中派生类B的成员m和n,它们也是保护成员和私有成员,不能被类外访问。也不能访问派生类对象c1中的私有成员p。

修改之后:

#include<iostream>
using namespace std;
class A
{public:
	void f1();
	int i;
	int j;
	int k;
 protected:
 	void f2();
};
class B: public A
{public:
	void f3();
 	int m; 
 	int n;
};
class C: public B
{public:
	void f4();
 	int p;
};
int main()
{
	A a1;
	B b1;
	C c1;
	c1.i;
	c1.j; 
	c1.k;
	c1.m;
	c1.n;
	c1.p;
	return 0;
}

(5)可以调用成员函数f1、f3、f4,它们是公用成员函数。不能调用成员函数f2,因为它是保护成员函数。

修改之后:

#include<iostream>
using namespace std;
class A
{public:
	void f1();
	int i;
 	void f2();
 protected:
	int j;
 private:
	int k; 	
};
class B: public A
{public:
	void f3();
 protected:
 	int m;  
 private:
 	int n;
};
class C: public B
{public:
	void f4();
 private:
 	int p;
};
int main()
{
	A a1;
	B b1;
	C c1; 
	c1.f1();
	c1.f3();
	c1.f4();
	c1.f2();
	return 0;
}

(6)都可以被派生类C的成员函数调用,因为f1和f3是公用成员函数,f2是保护成员函数。

#include<iostream>
using namespace std;
class A
{public:
	void f1();
	int i;
 protected:
 	void f2();
	int j;
 private:
	int k; 	
};
class B: public A
{public:
	void f3();
 protected:
 	int m;  
 private:
 	int n;
};
class C: public B
{public:
	void f4(){
		A::f1();
		A::f2();
		B::f3(); 
	};
 private:
 	int p;
};
int main()
{
	A a1;
	B b1;
	C c1; 
	return 0;
}

第六章

4.

#include<iostream>
using namespace std;
class Shape
{public: 
	virtual double area() const=0;
}; 
class Circle: public Shape
{public:
	Circle(double r):radius(r) {}
	virtual double area() const {return 3.14159*radius*radius;}
 protected:
 	double radius;
};
class Rectangle: public Shape
{public:
	Rectangle(double w,double h):width(w),height(h) {}
	virtual double area() const {return width*height;}
 protected:
 	double width,height;
};
class Triangle: public Shape
{public:
	Triangle(double w,double h):width(w),height(h) {}
	virtual double area() const {return 0.5*width*height;}
 protected:
 	double width,height;
};
void printArea(const Shape &s)
{
	cout<<s.area()<<endl;
}
int main()
{
	Circle circle(11.5);
	cout<<"area of circle = ";
	printArea(circle);
	Rectangle rectangle(4.7,5.6);
	cout<<"area of circle = ";
	printArea(rectangle);
	Triangle triangle(4.7,5.6);
	cout<<"area of circle = ";
	printArea(triangle);
	return 0;
}

5.

#include<iostream>
using namespace std;
class Shape
{public: 
	virtual double area() const=0;
}; 
class Circle: public Shape
{public:
	Circle(double r):radius(r) {}
	virtual double area() const {return 3.14159*radius*radius;}
 protected:
 	double radius;
};
class Square: public Shape
{public:
	Square(double s):side(s) {}
	virtual double area() const {return side*side;}
 protected:
 	double side;
};
class Rectangle: public Shape
{public:
	Rectangle(double w,double h):width(w),height(h) {}
	virtual double area() const {return width*height;}
 protected:
 	double width,height;
};
class Trapezoid: public Shape
{public:
	Trapezoid(double t,double b,double h):top(t),bottom(b),height(h) {}
	virtual double area() const {return 0.5*(top+bottom)*height;}
 protected:
 	double top,bottom,height;
};
class Triangle: public Shape
{public:
	Triangle(double w,double h):width(w),height(h) {}
	virtual double area() const {return 0.5*width*height;}
 protected:
 	double width,height;
};
int main()
{
	Circle circle(3.6);
	Square square(1.5);
	Rectangle rectangle(2.5,6.4);
	Trapezoid trapezoid(2.0,2.5,6.4);
	Triangle triangle(2.5,6.4);
	Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};
	double areas=0.0;
	for(int i=0;i<5;i++)
	{
		areas=areas+pt[i]->area();
	}
	cout<<"total of all areas = "<<areas<<endl;
	return 0;
}

;