C++面向对象的程序设计谭浩强 第六章课后题

以往章节

C++面向对象的程序设计谭浩强 第二章课后题

C++面向对象的程序设计谭浩强 第三章课后题

C++面向对象的程序设计谭浩强 第四章课后题

C++面向对象的程序设计谭浩强 第五章课后题

C++面向对象的程序设计谭浩强 第六章课后题

C++面向对象的程序设计谭浩强 第七章课后题

C++面向对象的程序设计谭浩强 第八章课后题

1. (简答题) 在例6.1程序基础上作一些修改。定义Point(点)类,由Point类派生出Circle(圆)类,再由Circle类派生出Cylinder(圆柱体)类。将类的定义部分分别作为3个头文件,对它们的成员函数的声明部分分别作为3个源文件(.cpp文件),在主函数中用#include命令把它们包含进来,形成一个完整的程序,并上机运行。

对应的三个头文件

#pragma once
#include <iostream>
using namespace std;
class Point
{
public:
	Point (double x = 0, double y = 0);
	void SetPoint(double, double);
	double GetX() const { return x; }
	double GetY() const { return y; }
	friend ostream& operator<<(ostream&, const Point&);
protected:
	double x, y;
};
#pragma once
class Circle : public Point
{
public:
	Circle(double x = 0, double y = 0, double r = 0);
	void SetRadius(double r);
	double GetRadius() const;
	double Area() const;
	friend ostream& operator<<(ostream&, const Circle&);
protected:
	double radius;
};
#pragma once
class Cylinder : public Circle
{
public:
	Cylinder(double x = 0, double y = 0, double r = 0, double h = 0);
	void SetHeight(double);
	double GetHeight() const;
	double Area() const;
	double Volume() const;
	friend ostream& operator<<(ostream&, const Cylinder&);
protected:
	double height;
};

对应的函数实现

#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(double a, double b){x = a; y = b;}
void Point::SetPoint(double a, double b){x = a; y = b;}
ostream& operator<<(ostream& output, const Point& p)
{
	output << "[" << p.x << ", " << p.y << "]" << endl;
	return output;
}
#include <iostream>
#include "Point.h"
#include "Circle.h"
using namespace std;
Circle::Circle(double a, double b, double r) : Point(a, b), radius(r) {}
void Circle::SetRadius(double r) { radius = r; }
double Circle::GetRadius() const { return radius; }
double Circle::Area() const { return 3.14 * radius * radius; }
ostream& operator<<(ostream& output, const Circle& c)
{
	output << "Center=[" << c.x << ", " << c.y << "], r=" << c.radius << ", area=" << c.Area() << endl;
	return output;
}
#include <iostream>
#include "Point.h"
#include "Circle.h"
#include "Cylinder.h"
using namespace std;
Cylinder::Cylinder(double a, double b, double r, double h):Circle(a, b, r), height(h) {}
void Cylinder::SetHeight(double h) { height = h; }
double Cylinder::GetHeight() const { return height; }
double Cylinder::Area() const { return 2 * Circle::Area() + 2 * 3.14159 * radius * height; }
double Cylinder::Volume() const { return Circle::Area() * height; }
ostream& operator<<(ostream& output, const Cylinder& cy)
{
	output << "Center=[" << cy.x << ", " << cy.y << "], r=" << cy.radius << ", h=" << cy.height
		<< "\narea=" << cy.Area() << ", volume=" << cy.Volume() << endl;
	return output;
}

我的主函数

#include <iostream>
#include "Point.h"
#include "Circle.h"
#include "Cylinder.h"
using namespace std;
int main()
{
	Cylinder cy1(3.5, 6.4, 5.2, 10);
	cout << "\noriginal cylinder:\nx=" << cy1.GetX() << ", y=" << cy1.GetY() << ", r="
		<< cy1.GetRadius() << ", h=" << cy1.GetHeight() << "\narea=" << cy1.Area()
		<< ", volume=" << cy1.Volume() << endl;

	cy1.SetHeight(15);
	cy1.SetRadius(7.5);
	cy1.SetPoint(5, 5);
	cout << "\nnew cylinder:\n" << cy1;
	Point& pRef = cy1;
	cout << "\npRef as a Point:" << pRef;
	Circle& cRef = cy1;
	cout << "\ncRef as a Circle:" << cRef;
	return 0;
}

3.在例6.3的基础上作以下修改,并作必要的讨论。(1) 把构造函数修改为带参数的函数,在建立对象时初始化。(2) 先不将析构函数声明为virtual,在main函数中另设一个指向Circle类对象的指针变量,使它指向grad1。运行程序,分析结果。(3) 不作第(2)点的修改而将析构函数声明为virtual,运行程序,分析结果。

1.11

#include <iostream>
using namespace std;
class Point
{
public:
	Point(int a , int b) {a = x;b = y;}
	~Point(){cout << "executing Point destructor" << endl;}
private:
	int x;
	int y;
};
class Circle :public Point
{
public:
	Circle(int a, int b, int c) :Point(a, b), radus(c) {}
	~Circle(){cout << "executing Circle destructor" << endl;}
private:
	int radus;
};
int main()
{
	Point *p=new Circle(1,3,4);
	delete p;
	return 0;
}

2.

#include <iostream>
using namespace std;
class Point
{
public:
	Point(int a , int b) {a = x;b = y;}
	~Point(){cout << "executing Point destructor" << endl;}
private:
	int x;
	int y;
};
class Circle :public Point
{
public:
	Circle(int a, int b, int c) :Point(a, b), radus(c) {}
	~Circle(){cout << "executing Circle destructor" << endl;}
private:
	int radus;
};
int main()
{
	Point *p=new Circle(1,3,4);
	Circle *p1=new Circle(1, 3, 4);
	delete p1;
	return 0;
}

3.

#include <iostream>
using namespace std;
class Point
{
public:
	Point(int a , int b) {a = x;b = y;}
	virtual ~Point(){cout << "executing Point destructor" << endl;}
private:
	int x;
	int y;
};
class Circle :public Point
{
public:
	Circle(int a, int b, int c) :Point(a, b), radus(c) {}
	virtual ~Circle(){cout << "executing Circle destructor" << endl;}
private:
	int radus;
};
int main()
{
	Point *p=new Circle(1,3,4);
	Circle *p1=new Circle(1, 3, 4);
	delete p1;
	return 0;
}

2.先调用派生类的析构函数 在调用基类的析构函数但是要是有很多派生类就不方便了一个指针只指向他基类,所以要用虚函数

3.基类构造函数声明虚函数 派生类的析构函数也就是自动声明为虚函数了也就也是其实我这块的virtual在派生类其实可以不用写

4. (简答题)  写一个程序,定义抽象基类Shape,由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。

#include <iostream>
using namespace std;
//父类 里面只有一个纯虚函数
//是一个抽象类
class Shape
{
public:
	virtual double PrintArea() const = 0;
};

class Circle : public Shape
{
public:
	Circle(double = 0);
	//子类必须重写抽象类的纯虚函数
	virtual double PrintArea() const { return 3.14 * radius * radius; }
private:
	double radius;
};

class Rectangle : public Shape
{
public:
	Rectangle(double = 0, double = 0);
	//子类必须重写抽象类的纯虚函数
	virtual double PrintArea() const { return 0.5 * width * height; };
private:
	double height;
	double width;
};

class Triangle : public Shape
{
public:
	Triangle(double = 0, double = 0);
	//子类必须重写抽象类的纯虚函数
	virtual double PrintArea() const { return 0.5 * width * height; };
private:
	double height;
	double width;
};
//父类指针或者引用指向子类对象
//使用多态
void PrintArea(const Shape& s)
{
	cout << s.PrintArea() << endl;
}
//类外构造函数
Circle::Circle(double r) :radius(r) {}
Triangle::Triangle(double w, double h) : width(w), height(h) {}
Rectangle::Rectangle(double w, double h) : width(w), height(h) {}

int main()
{
	Circle c(2.0);
	cout << "圆形面积:";
	PrintArea(c);
	Rectangle r(1, 1);
	cout << "矩形面积:";
	PrintArea(r);
	Triangle t(3.0, 8.0);
	cout << "三角形面积:";
	PrintArea(t);
	return 0;
}

5. (简答题) 写一个程序,定义抽象基类Shape,由它派生出5个派生类: Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形)。用虚函数分别计算几种图形面积,并求它们的和。要求用基类指针数组,使它的每一个元素指向一个派生类对象。

#include <iostream>
using namespace std;
//父类有纯虚函数
//是一个抽象类
class Shape
{
public:
	virtual double PrintArea() const = 0;
	virtual void display()const = 0;
};

class Circle : public Shape
{
public:
	Circle(double = 0);
	//子类继承必须重写纯虚函数,否则是抽象类
	virtual double PrintArea() const { return 3.14 * radius * radius; }
	virtual void display()const
	{
		cout << "圆面积:" << PrintArea() << endl;
	}
private:
	double radius;
};

class Rectangle : public Shape
{
public:
	Rectangle(double = 0, double = 0);
	//子类继承必须重写纯虚函数,否则是抽象类
	virtual double PrintArea() const { return 0.5 * width * height; }
	virtual void display()const
	{
		cout << "矩形面积:" << PrintArea() << endl;
	}
private:
	double height;
	double width;
};
class Square : public Shape
{
public:
	Square(double = 0, double = 0);
	//子类继承必须重写纯虚函数,否则是抽象类
	virtual double PrintArea() const { return width * height; }
	virtual void display()const
	{
		cout << "正方形面积:" << PrintArea() << endl;
	}
private:
	double height;
	double width;
};

class Triangle : public Shape
{
public:
	Triangle(double = 0, double = 0);
	//子类继承必须重写纯虚函数,否则是抽象类
	virtual double PrintArea() const { return 0.5 * width * height; }
	virtual void display()const
	{
		cout << "三角形面积:" << PrintArea() << endl;
	}
private:
	double height;
	double width;
};
class Trapezoid : public Shape
{
public:
	Trapezoid(double = 0, double = 0, double = 0);
	//子类继承必须重写纯虚函数,否则是抽象类
	virtual double PrintArea() const { return 0.5 * height * (top + down); }
	virtual void display()const
	{
		cout << "梯形面积:" << PrintArea() << endl;
	}
private:
	double height;
	double top;
	double down;
};
//使用多态是父类指针或者引用指向子类对象
void PrintArea(const Shape& s)
{
	cout << s.PrintArea() << endl;
}
//类外构造函数
//利用参数列表进行初始化
Circle::Circle(double r) :radius(r) {}
Triangle::Triangle(double w, double h) : width(w), height(h) {}
Rectangle::Rectangle(double w, double h) : width(w), height(h) {}
Square::Square(double w, double h) : width(w), height(h) {}
Trapezoid::Trapezoid(double t, double d, double h) : top(t), down(d), height(h) {}

int main()
{
	Circle c(2.0);
	Rectangle r(1, 1);
	Triangle t(3, 8);
	Square s(2, 2);
	Trapezoid tr(1, 2, 4);
	//对象数组
	Shape* p[5] = { &c,&r,&t,&s,&tr };
	for (int i = 0; i < 5; i++)
	{
		p[i]->display();
	}
	return 0;
}

  • 9
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五毛变向.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值