2024 4 7 c++ 作业多态性

1. 多态性:抽象类和派生类

(1) 定义一个抽象类CShape (至少有一个函数被声明为纯虚函数);

(2) 再利用CShape分别定义两个派生类CRectangle(矩形)和CCircle(圆), 三个类都有计算面积的成员函数GetArea()和计算对象周长的成员函数GetPerimeter() ;

(3) 在主函数中声明基类指针和派生类对象,并通过基类指针调用不同对象的成员函数GetArea() 和GetPerimeter()。

问题1:什么是纯虚函数

解决:纯虚函数的主要作用是定义一个接口,强制派生类提供特定的实现。给派生的基类提供一个特殊的接口来实现多态性。eg.

// 抽象基类

class Shape {

public:

// 纯虚函数 virtual void draw() const = 0;

// 虚析构函数 virtual ~Shape() {} };

问题2:子类中的函数定义

解决:eg:void draw() const override { std::cout << "绘制一个矩形" << std::endl;

override的意思是告诉编译器是从基类的虚函数继承下来的重写版本。

问题3: void  GetArea(int l,int w)  override{
               int area=0;
               length=l;
               width=w;
               area=l*w;
               std::cout<<area<<std::endl;
        };14    12    C:\c++\2024 4 7\CShape.h    [Error] 'void CRectangle::GetArea(int, int)' marked override, but does not override

这里的报错显示并没有覆盖基类的虚函数而是在派生类定义了一个新的函数,如何才能使基类的函数被覆盖。

解决:将派生类的定义单独拿出来,将基类函数和派生类函数的形式一制化;

问题4:    void CRectangle(int l,int w):length(l),width(w){
        };

解决:类的构造函数不需要void

在派生类覆盖基类的纯虚函数是一定要对应包括const

问题五:如何定义派生类对象;

解决: eg. CRectangle rectangle(3,4);
    CCircle circle(5);

 最终版:

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

};

#include <iostream>
#include "CShape.h"

int main (){
    
    CShape *shape1;
    CShape *shape2;

    CRectangle rectangle(3,4);
    CCircle circle(5);
    
    shape1=&rectangle;
    shape2=&circle;
    
    std::cout<<"the rectangle area : "<<shape1->GetArea()<<std::endl;
    std::cout<<"the rectangle Perimeter : "<<shape1->GetPerimeter()<<std::endl;
    
    std::cout<<"the circle area : "<<shape2->GetArea()<<std::endl;
    std::cout<<"the circle Perimeter : "<<shape2->GetPerimeter()<<std::endl;
    

}

#include<iostream> using namespace std; class CShape{ public:float Area; float Perimeter; CShape() { Area=0;Perimeter=0; } virtual void GetArea(){} virtual void GetPerimeter(){} }; class CRectangle:public CShape{ private: int l;int h; public: CRectangle(int h,int l ):CShape() {this->h=h;this->l=l; } void GetArea() {Area=l*h;cout<<"矩形的面积="<<Area<<endl;} void GetPerimeter() {Perimeter=(l+h)*2;cout<<"矩形的周长="<<Perimeter<<endl; } }; class CCirle:public CShape{ private:float r; public:CCirle(float r):CShape() {this->r=r; } void GetArea() { Area=3.14159*r*r; cout<<"圆的面积="<<Area<<endl; } void GetPerimeter() { Perimeter=r*3.14159*2; cout<<"圆的周长="<<Perimeter<<endl; } }; class CSquare:public CShape{ private: int a; public: CSquare(int a):CShape() { this->a=a; } void GetArea() { Area=a*a; cout<<"正方形面积="<<Area<<endl; } void GetPerimeter() { Perimeter=4*a; } }; class CTrapeziod:public CShape{ private: int lu; int ld; int h; public: CTrapeziod(int lu,int ld,int h):CShape() { this->lu=lu; this->ld=ld; this->h=h; } void GetArea() { Area=(lu+ld)*h/2; cout<<"梯形面积="<<Area<<endl; } }; class CTrangle:public CShape{ private: int d; int h; public: CTrangle(int d,int h):CShape() { this->d=d; this->h=h; } void GetArea() { Area=d*h/2; cout<<"三角形的面积="<<Area<<endl; } }; void main() { CShape *p[5]; CRectangle CR(5,10); CCirle CC(3); CSquare CS(4); CTrapeziod CT1(2,5,4); CTrangle CT2(6,2); p[0]=&CR; p[0]->GetArea(); p[1]=&CC; p[1]->GetArea(); p[2]=&CS; p[2]->GetArea(); p[3]=&CT1; p[3]->GetArea(); p[4]=&CT2; p[4]->GetArea(); int i, double SumArea=0.f; for(i=0;i<5;i++) { SumArea=p[i]->Area+SumArea; } cout<<"面积总和="<<SumArea<<endl; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值