IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++面向对象的程序设计谭浩强 第六章课后题 -> 正文阅读

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

22222

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;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-11-20 18:12:41  更:2021-11-20 18:13:56 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 6:22:23-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码