c++练习题---Shape抽象类

这篇博客介绍了一个C++编程问题,涉及Shape抽象类的使用,包括派生出Rectangle矩形类和Circle圆类,实现计算面积和周长的纯虚函数。博主提供了输入样例和输出样例,以及相关的代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6-1 Shape抽象类 (30分)

已知抽象类类Shape定义如下,其中两个纯虚函数分别为计算面积getArea()和计算周长getPerim()。请通过Shape类派生出矩形类Rectangle和圆类Circle,计算各自的面积和周长,并能够按照main函数给出的测试代码完成给定的输入。

Shape类定义及测试程序如下:

#include <iostream>
using namespace std;
const double PI=3.14;
class Shape {
public:
	virtual double getArea()=0;
	virtual double getPerim()=0;
};

/*请在这里输入你的代码*/

int main(){
	double r,l,w;
	cin>>r;
	cin>>l>>w;
	Shape *p=NULL;
	p=new Circle(r);
	print(p);//输出圆面积与周长
	p=new Rectangle(l,w);
	print(p);//输出矩形面积与周长
	return 0;	
}

输入样例:

1 2 3

输出样例:

Area:3.14
Perimeter:6.28
Area:6
Perimeter:10

 代码实例:

class Circle:public Shape
{
	private:
		double r;
	public:
		Circle(double r_):r(r_)
		{
			
		}
	 double getArea(){
	 	return 3.14*r*r;
	 }
	 double getPerim(){
	 	return 2*
### C++ 继承与派生的相关练习题 以下是几个基于继承派生概念的练习题目及其示例代码: --- #### 题目 1: 设计一个多态程序模拟动物叫声 要求定义一个 `Animal` 基以及若干派生(如 `Dog`, `Cat`)。每个派生重载虚函数 `makeSound()` 来输出不同动物的声音。 ```cpp #include <iostream> using namespace std; // 定义 Animal class Animal { public: virtual void makeSound() const = 0; // 纯虚函数,使 Animal 成为抽象类 }; // 定义 Dog class Dog : public Animal { public: void makeSound() const override { // 覆盖基中的纯虚函数 cout << "Woof! Woof!" << endl; } }; // 定义 Cat class Cat : public Animal { public: void makeSound() const override { // 覆盖基中的纯虚函数 cout << "Meow! Meow!" << endl; } }; int main() { Animal* animal1 = new Dog(); // 动态分配内存并指向派生对象 Animal* animal2 = new Cat(); animal1->makeSound(); // 输出狗叫声音 animal2->makeSound(); // 输出猫叫声音 delete animal1; // 释放动态分配的内存 delete animal2; return 0; } ``` 此代码展示了如何利用多态性虚函数来实现行为的不同表现形式[^2]。 --- #### 题目 2: 使用虚基解决菱形继承问题 设计一个具有菱形继承结构的程序,并通过虚基消除重复成员变量的问题。 ```cpp #include <iostream> using namespace std; // 定义虚基 A class A { protected: int value; public: A(int v) : value(v) {} void showValue() const { cout << "A's Value: " << value << endl; } }; // 定义 B C 派生于 A class B : virtual public A { public: B(int v) : A(v * 2) {} // 构造函数初始化列表调用 A 的构造函数 void showMessage() const { cout << "B Class Message" << endl; } }; class C : virtual public A { public: C(int v) : A(v * 3) {} // 构造函数初始化列表调用 A 的构造函数 void showMessage() const { cout << "C Class Message" << endl; } }; // D 同时继承自 B C class D : public B, public C { public: D(int v) : A(v), B(v), C(v) {} // 明确指定虚基 A 的构造参数 void displayAllValues() const { showValue(); B::showMessage(); C::showMessage(); } }; int main() { D d(5); d.displayAllValues(); // 展示最终的结果 return 0; } ``` 这段代码演示了如何使用虚基避免菱形继承带来的二义性问题[^4]。 --- #### 题目 3: 实现几何图形的面积计算 创建一个基 `Shape` 并提供两个派生 `Circle` `Rectangle`。分别实现它们各自的面积计算逻辑。 ```cpp #include <iostream> #include <cmath> // 提供 M_PI 常量 using namespace std; // 定义 Shape 抽象基 class Shape { public: virtual double getArea() const = 0; // 纯虚函数用于获取面积 virtual ~Shape() {} // 虚析构函数确保安全删除指针 }; // Circle 是 Shape 的派生 class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) {} double getArea() const override { // 重写基纯虚函数 return M_PI * pow(radius, 2); } void setRadius(double r) { radius = r; } }; // Rectangle 是 Shape 的另一个派生 class Rectangle : public Shape { private: double width, height; public: Rectangle(double w, double h) : width(w), height(h) {} double getArea() const override { // 重写基纯虚函数 return width * height; } void setSize(double w, double h) { width = w; height = h; } }; int main() { Circle circle(2.0); // 创建半径为 2 的圆形 Rectangle rectangle(3.0, 4.0); // 创建宽高分别为 3 4 的矩形 cout << "Circle Area: " << circle.getArea() << endl; // 输出圆的面积 cout << "Rectangle Area: " << rectangle.getArea() << endl; // 输出矩形的面积 return 0; } ``` 该示例说明了如何通过继承扩展功能的同时保持良好的封装性[^5]。 --- #### 题目 4: 函数模板的应用——求数组最大值 编写一个通用的函数模板用来求解不同型的一维数组的最大值。 ```cpp #include <iostream> using namespace std; template<typename T> T findMax(const T array[], int size) { T maxVal = array[0]; for (int i = 1; i < size; ++i) { if (array[i] > maxVal) { maxVal = array[i]; } } return maxVal; } int main() { int intArray[] = {1, 7, 3, 9, 4}; double doubleArray[] = {1.1, 7.7, 3.3, 9.9, 4.4}; cout << "Max Int: " << findMax(intArray, 5) << endl; // 查找整数数组的最大值 cout << "Max Double: " << findMax(doubleArray, 5) << endl; // 查找浮点数数组的最大值 return 0; } ``` 上述代码体现了泛型编程的思想,能够处理多种数据型的输入[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值