2024 4 7 c++作业第二题

文章讲述了如何在C++中使用虚函数、基类指针数组和派生类来定义正方形、梯形和三角形等图形,计算它们的面积,并处理相关错误。着重讲解了虚函数的概念以及在多态中的应用。
摘要由CSDN通过智能技术生成

2.对象指针数组

  1. 在第1题基础上再定义三个CShap的派生类CSquare(正方形)、CTrapezoid(梯形)、CTriangle(三角形);
  2. 分别用虚函数计算五种图形的面积,并求它们的和;
  3. 要求用基类指针数组,使它的每一个元素指向一个派生类对象。 

问题1:什么是虚函数

解决:基类的虚函数允许,被派生类的虚函数来覆盖实现派生类虚函数特有的价值。通过这样可以实现多态性。

问题二:什么是基类指针数组

解决:例子:int main() {

Base* array[3];

// 声明一个基类指针数组

// 创建基类和派生类对象,并将指针存储到数组中

Base baseObj;

Derived1 derived1Obj;

Derived2 derived2Obj;

array[0] = &baseObj;

array[1] = &derived1Obj;

array[2] = &derived2Obj;

// 遍历数组,调用各个对象的 print() 函数

for (int i = 0; i < 3; ++i)

{ array[i]->print(); }

return 0;

}

思路:就像上一题一样先将他们构造再构造成员函数。

问题3:12    14    C:\c++\2024 4 7\2\main.cpp    [Error] cannot declare variable 'shape3' to be of abstract type 'CSquare'

  CSquare  shape3(6);

解决:原因是缺少了基类中对应的一个函数

class CShape{
public:
    virtual double GetArea()const=0;//纯虚函数
    virtual double GetPerimeter()const=0;
    virtual ~CShape(){}
    
};

在派生类中即使主函数中不调用也需要再派生类中补上

问题4:24    11    C:\c++\2024 4 7\2\main.cpp    [Error] invalid operands of types 'double' and 'double (CShape::*)() const' to binary 'operator+'

for(i=0;i++;i<5)
    {
       sum+=&array[i]->GetArea;  //这一行
    }
    std::cout<<sum<<std::endl;

在这里出现了报错

解决:在array数组前加地址符的意思是使他们的地址进行相加,并不是值,GetArea要加上括号才能知道你调用的是成员函数。

最终版:

2.

#include <iostream>

class CShape{

public:

        virtual double GetArea()const=0;//纯虚函数

        virtual double GetPerimeter()const=0;

        virtual ~CShape(){}

   

};

class CRectangle:public CShape{

        int length;

        int width;

        public:

                  CRectangle(int l,int w):length(l),width(w){

                 };

            virtual double  GetArea() const override{

               return length*width;

            };

             virtual double GetPerimeter()const override{

                   return 2*(length+width);

            };

};

class CCircle:public CShape{

        int radium;

                 public:

                         CCircle(int r):radium(r){

                         };

            virtual double  GetArea() const override{

               return radium*radium*3.14;

            };

            virtual double GetPerimeter() const override{

                   return 2*3.14*radium;

            };

};

class CSquare :public CShape{

       

        int x;

           public:

              CSquare(int b):x(b){};

              virtual double GetArea()const override{

                   return x*x;

              };

              virtual double GetPerimeter()const override{

                   return 0;

            };

};

class CTrapezoid :public CShape{

       

        int upperlength;

        int lowerlength;

        int height;

           public:

              CTrapezoid(int u,int l,int h):upperlength(u),lowerlength(l),height(h){};

              virtual double GetArea() const override{

                   return (upperlength+lowerlength)*height/2;

              };

               virtual double GetPerimeter()const override{

                   return 0;

            };

};

class CTriangle :public CShape{

       

        int length;

        int height;

           public:

              CTriangle(int l,int h):length(l),height(h){};

              virtual double GetArea() const override{

                   return (length*height)/2;

              };

               virtual double GetPerimeter()const override{

                   return 0;

            };

};

#include <iostream>

#include "CShape.h"

int main (){

       

    double sum=0;

    int i;

        //定义基类和派生类

        CShape *array[5];

    CRectangle shape1(3,4);

    CCircle shape2(3);

    CSquare shape3(6);

    CTrapezoid shape4(2,3,3);

    CTriangle shape5(5,6);

    array[0]=&shape1;

    array[1]=&shape2;

    array[2]=&shape3;

    array[3]=&shape4;

    array[4]=&shape5;

    for(i=0;i<5;i++)

    {

       sum+=array[i]->GetArea();

    }

    std::cout<<sum<<std::endl;

}

  • 15
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值