C++面向对象程序设计第六章课后习题答案(第2版—谭浩强) 下载本文

内容发布更新时间 : 2024/11/18 2:30:10星期一 下面是文章的全部内容请认真阅读。

第六章课后习题答案(第二版谭浩强)

1:

//xt6-1/cpp

#include //如用VC++应改为∶#include using namespace std; //如用VC++应取消此行 #include \#include \#include \#include \int main()

{Cylinder cy1(3.5,6.4,5.2,10);

cout<<\<

cout<<\ Point &pRef=cy1;

cout<<\ Circle &cRef=cy1;

cout<<\ return 0; }

3:解法一

#include using namespace std; class Point

{public:

Point(float a,float b):x(a),y(b){}

~Point(){cout<<\ private: float x; float y; };

class Circle:public Point {public:

Circle(float a,float b,float r):Point(a,b),radius(r){}

~Circle(){cout<<\ private:

float radius;

};

int main()

{Point *p=new Circle(2.5,1.8,4.5); delete p; return 0; }

3:解法二

#include using namespace std; class Point {public:

Point(float a,float b):x(a),y(b){}

~Point(){cout<<\ private: float x; float y; };

class Circle:public Point {public:

Circle(int a,int b,int r):Point(a,b),radius(r){}

~Circle(){cout<<\ private:

float radius; };

int main()

{Point *p=new Circle(2.5,1.8,4.5); Circle *pt=new Circle(2.5,1.8,4.5); delete pt; return 0; }

3:解法三

#include using namespace std; class Point {public:

Point(float a,float b):x(a),y(b){}

virtual ~Point(){cout<<\ private: float x; float y; };

class Circle:public Point {public:

Circle(float a,float b,float r):Point(a,b),radius(r){}

virtual ~Circle(){cout<<\ private: float radius; };

void main()

{Point *p=new Circle(2.5,1.8,4.5); delete p; }

4:

#include using namespace std;

//定义抽象基类Shape class Shape {public:

virtual double area() const =0; //纯虚函数 };

//定义Circle类

class Circle:public Shape {public:

Circle(double r):radius(r){} virtual double area() const {return 3.14159*radius*radius;}; protected:

double radius; };

//定义Rectangle类

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<

int main() {

Circle circle(12.6); circle

cout<<\ =\

printArea(circle); Rectangle rectangle(4.5,8.4); 象rectangle

cout<<\ printArea(rectangle); 面积

Triangle triangle(4.5,8.4); //建立Triangle类对象 cout<<\ =\

printArea(triangle); //输出triangle的面积 return 0; }

5:

#include using namespace std; //定义抽象基类Shape class Shape {public:

virtual double area() const =0; };

//定义Circle(圆形)类 class Circle:public Shape {public:

Circle(double r):radius(r){} virtual double area() const {return 3.14159*radius*radius;}; protected:

double radius; };

//定义Square(正方形)类 class Square:public Shape

//建立Circle类对象 //输出circle的面积 //建立Rectangle类对

//输出rectangle的 //纯虚函数 //结构函数 //定义虚函数 //半径 {public:

Square(double s):side(s){} //结构函数 virtual double area() const {return side*side;} //定义虚函数 protected: double side; };

//定义Rectangle(矩形)类 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; //宽与高 };

//定义Trapezoid(梯形)类 class Trapezoid:public Shape {public:

Trapezoid(double t,double b,double h):top(t),bottom(t),height(h){} //结构函数 virtual double area() const {return 0.5*(top+bottom)*height;} //定义虚函数 protected:

double top,bottom,height; //上底、下底与高 };

//定义Triangle(三角形)类 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(12.6); //建立Circle类对象circle

Square square(3.5); //建立Square类对象square

Rectangle rectangle(4.5,8.4); //建立Rectangle类对象rectangle

Trapezoid trapezoid(2.0,4.5,3.2); //建立Trapezoid类对象trapezoid

Triangle triangle(4.5,8.4); //建立Triangle类对象 Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};

//定义基类指针数组pt,使它每一个元素指向一个派生类对象

double areas=0.0; //areas为总面积 for(int i=0;i<5;i++)

{areas=areas+pt[i]->area();}

cout<<\ //输出总面积 return 0; }