贵州大学oj C++ 第五次 4.继承与虚函数之三

该博客介绍了C++编程中如何根据已有的基类(Base)创建公有派生类(Derived1, Derived2, Derived3),每个派生类实现了基类的纯虚函数`calculate()`。Derived1计算三个数的和,Derived2计算三个数的积,Derived3计算前两个数与第三个数的积。示例代码展示了如何实例化这些派生类并调用`calculate()`方法进行计算。

记录学习日常 代码可能有错 大家多多包涵 有好的建议提出的话 我会开心接纳 初学阶段 

有一个类Base及其公有派生类Derived1、Derived2、Derived3。类Base和main函数的代码已经完成,请根据已有的代码给出上述三个派生类的设计。(只需要提交这三个类的代码)

程序的开头部分如下:

#include <iostream>

using namespace std;

class Base{

    int x,y;

public:

    Base(int a,int b):x(a),y(b){}

    int getX()const{return x;}

    int getY()const{return y;}

    virtual void calculate()=0;

};

main函数的代码如下:

int main(){

    Base *p;

    Derived1 o1(1,2,3);

    Derived2 o2(4,5,6);

    Derived3 o3(7,8,9);

    p=&o1;

    p->calculate();

    p=&o2;

    p->calculate();

    p=&o3;

    p->calculate();

    return 0;

}

输出描述

三个数的和是6

三个数的积是120

前两个数的和与第三个数的积是135

class Derived1:public Base{
    int x,y,z;
public:
    Derived1()=default;
    Derived1(int x,int y,int z):x(x),y(y),z(z), Base(x,y){}
    void calculate(){
        cout<<"三个数的和是"<<x+y+z<<endl;
    }
};
class Derived2:public Base{
    int x,y,z;
public:
    Derived2()=default;
    Derived2(int x,int y,int z):x(x),y(y),z(z), Base(x,y){}
    void calculate(){
        cout<<"三个数的积是"<<x*y*z<<endl;
    }
};
class Derived3:public Base{
    int x,y,z;
public:
    Derived3()=default;
    Derived3(int x,int y,int z):x(x),y(y),z(z), Base(x,y){}
    void calculate(){
        cout<<"前两个数的和与第三个数的积是"<<(x+y)*z<<endl;
    }
};

为了满足用户的需求,以下是一个贵州大学 OJ 平台上面向对象编程第八次作业内容相关的题目及答案。该题目涉及使用虚函数抽象的概念。 ### 题目描述 编写一个抽象 `Shape`,在此基础上派生出两个子 `Rectangle`(长方形) `Circle`(圆)。这两个需要实现计算面积的函数 `getArea()` 计算周长的函数 `getPerim()`。 为了避免精度问题,取圆周率 π=3。 --- ### 码实现 ```cpp #include <bits/stdc++.h> using namespace std; // 抽象 Shape class Shape { public: virtual int getArea() = 0; // 纯虚函数,获取面积 virtual int getPerim() = 0; // 纯虚函数,获取周长 }; // Rectangle 继承自 Shape class Rectangle : public Shape { private: int length, width; public: Rectangle(int l, int w) : length(l), width(w) {} int getArea() override { return length * width; } int getPerim() override { return 2 * (length + width); } }; // Circle 继承自 Shape class Circle : public Shape { private: int radius; public: Circle(int r) : radius(r) {} int getArea() override { return 3 * radius * radius; // π = 3 } int getPerim() override { return 2 * 3 * radius; // π = 3 } }; // 主函数 int main() { int x, y, r; cin >> x >> y >> r; Shape* shape = new Rectangle(x, y); cout << shape->getArea() << &#39;-&#39; << shape->getPerim() << " "; shape = new Circle(r); cout << shape->getArea() << &#39;-&#39; << shape->getPerim() << " "; delete shape; // 释放内存 return 0; } ``` --- ### 示例输入输出 **输入:** ``` 5 3 4 ``` **输出:** ``` 15-16 48-24 ``` **解释:** - 长方形的长为 5,宽为 3: - 面积 = 5 × 3 = 15 - 周长 = 2 × (5 + 3) = 16 - 圆的半径为 4: - 面积 = 3 × 4&sup2; = 48 - 周长 = 2 × 3 × 4 = 24 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值