好文档 - 专业文书写作范文服务资料分享网站

c++面向对象程序设计课后习题答案(谭浩强版) 

天下 分享 时间: 加入收藏 我要投稿 点赞

//输出面积的函数

void printArea(const Shape &s)

{cout<

int main() {

Circle circle(12.6); //建立Circle类对象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 {public:

Square(double s):side(s){} virtual double area() const {return side*side;} protected:

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

//输出rectangle的 //纯虚函数 //结构函数 //定义虚函数 //半径 //结构函数 //定义虚函数 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; }

第七章

1:解法一

#include #include using namespace std; int main()

{double a,b,c,s,area;

cout<<\ cin>>a>>b>>c; if (a+b<=c)

cerr<<\ else if(b+c<=a)

cerr<<\ else if (c+a<=b)

cerr<<\ else

{s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<\ return 0; }

1:解法二

#include #include using namespace std;

void input(double a,double b,double c) {cout<<\cin>>a>>b>>c; }

void area(double a,double b,double c) {double s,area; if (a+b<=c)

cerr<<\ else if(b+c<=a)

cerr<<\

else if (c+a<=b)

cerr<<\ else

{s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<\}

int main()

{double a=2,b=3,c=5; input(a,b,c); area(a,b,c); return 0; }

2:解法一

#include #include using namespace std;

int main() {float a[5];

cout<<\ for(int i=0;i<5;i++) cin>>a[i];

cout<

cout<

2:解法二

#include using namespace std; int main() {float a[5]; int i;

cout<<\ for(i=0;i<5;i++) cin>>a[i];

cout.setf(ios::fixed); cout.precision(2); for(i=0;i<5;i++) {cout.width(10); cout<

3:

#include #include using namespace std; int main() {

for(int n=1;n<8;n++)

cout<

#include #include

using namespace std; //VC++ 6.0要此行

//fun1函数从键盘输入20个整数,分别存放在两个磁盘文件中

void fun1() {int a[10];

ofstream outfile1(\ //分别定义两个文件流对象 if(!outfile1) //检查打开f1.dat是否成功 {cerr<<\ exit(1); }

if(!outfile2) //检查打开f2.dat是否成功 {cerr<<\ exit(1); }

cout<<\

for(int i=0;i<10;i++) //输入10个数存放到f1.dat文件中 {cin>>a[i];

outfile1<

cout<<\

for(i=0;i<10;i++) //输入10个数存放到f2.dat文件中 {cin>>a[i];

outfile2<

outfile1.close(); //关闭f1.dat文件 outfile2.close(); //关闭f2.dat文件 }

//从f1,dat读入10个数,然后存放到f2.dat文件原有数据的后面 void fun2()

{ifstream infile(\ //f1.dat作为输入文件 if(!infile)

1snua7w6oy37lyd0yejn
领取福利

微信扫码领取福利

微信扫码分享