山科oj 2312 Peach

我们又要开始卖桃子啦!!
山科oj水果店水果持续上新中,桃子新鲜水多是串门访友的必备物品.但疫情期间还是要减少出行哦~x
言归正传,当class与*碰撞会出现什么样的火花呢?没看出来,反正不会做了,就是了。
小Fivelish给你们带来神奇!叮叮叮当当当
virtual!就是他这个东西!真的令人捉摸不透。也就是这个题干中提到的多态。
在这里插入图片描述看我拿出来了题干,哇咔咔咔咔。
我来教多态了,是沟是路我就不知道了。哈哈哈哈
多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。
这概念,绕哇绕。上例子。

#include <iostream> 
using namespace std;
 
class Shape {
   protected:
      int width, height;
   public:
      Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      int area()
      {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};
class Rectangle: public Shape{
   public:
      Rectangle( int a=0, int b=0):Shape(a, b) { }
      int area ()
      { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
class Triangle: public Shape{
   public:
      Triangle( int a=0, int b=0):Shape(a, b) { }
      int area ()
      { 
         cout << "Triangle class area :" <<endl;
         return (width * height / 2); 
      }
};
// 程序的主函数
int main( )
{
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);
 
   // 存储矩形的地址
   shape = &rec;
   // 调用矩形的求面积函数 area
   shape->area();
 
   // 存储三角形的地址
   shape = &tri;
   // 调用三角形的求面积函数 area
   shape->area();
   
   return 0;
}//参考菜鸟教程例子

Parent class area
Parent class area

这个在那个指针那里其实是Shape的类,所以编译器让其按照基类的area运行并没有什么问题(静态多态),本来就要用你的,其他也用你的就不麻烦了呗。为了让其不这么图,方便必须有什么理由,比如他的这个东西不好用,那我们就要发坏了,用virtual把它弄坏。

class Shape {
   protected:
      int width, height;
   public:
      Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      virtual int area()
      {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};

我们将Shape改成这个模样,结果让我们来看看

Rectangle class area
Triangle class area

哇,我成功了!实用工具成功,不愧是我.jpg。此时,编译器看的是指针的内容,而不是它的类型。因此,由于 tri 和 rec 类的对象的地址存储在 *shape 中,所以会调用各自的 area() 函数。正如你所看到的,每个子类都有一个函数 area() 的独立实现。这就是多态的一般使用方式。有了多态,你可以有多个不同的类,都带有同一个名称但具有不同实现的函数,函数的参数甚至可以是相同的。
我真是一个好博主,为大家免费科普!耶!
其实这个题主要用的是虚基类。

#include<bits/stdc++.h>
using namespace std;
class Fruit
{
    double w;
public:
    Fruit(double a):w(a) {}
    virtual double weight()
    {
        return w;
    }
    virtual double juice_weight()=0;
    virtual double salad_weight()=0;
};
class Juice:virtual public Fruit
{
public:
    double juice_ratio;
    Juice(double a,double b):Fruit(a),juice_ratio(b) {}
    double juice_weight()
    {
        return Fruit::weight()*juice_ratio;
    }
    double salad_weight()
    {
        return 0;
    }
};
class Salad:virtual public Fruit
{
public:
    double Salad_ratio;
    Salad(double a,double b):Fruit(a),Salad_ratio(b) {}
    double salad_weight()
    {
        return Fruit::weight()*Salad_ratio;
    }
    double juice_weight()
    {
        return 0;
    }
};
class JuiceSalad:public Salad,public Juice
{
public:
    JuiceSalad(double a,double b,double c):Juice(a,b),Salad(a,c),Fruit(a) {}

    double juice_weight()
    {
        return 0.6*weight()*juice_ratio;
    }
    double salad_weight()
    {
        return 0.4*weight()*Salad_ratio;
    }

};
class Apple:public JuiceSalad
{
public:
    Apple(double a,double b,double c):JuiceSalad(a,b,c),Fruit(a) {}

};
class Peach:public JuiceSalad
{
public:
    Peach(double a,double b,double c):JuiceSalad(a,b,c),Fruit(a) {}

};
class Banana:public Salad
{
public:

    Banana(double a,double b):Salad(a,b),Fruit(a) {}
};
class Pineapple:public Juice
{
public:
    Pineapple(double a,double b):Juice(a,b),Fruit(a) {}

};

int main()
{
    Fruit* fruit[100];
    Juice* juice[100];
    Salad* salad[100];

    string name;
    int cases, f, j, s;
    f = j = s = 0;
    cin >> cases;
    while(cases--)
    {
        cin >> name;
        double weight, s_ratio, j_ratio;
        if(name == "Peach")
        {
            cin >> weight >> j_ratio >> s_ratio;
            Peach* p = new Peach(weight, j_ratio, s_ratio);
            fruit[f++] = p;
            juice[j++] = p;
            salad[s++] = p;
        }
        if(name == "Pineapple")
        {
            cin >> weight >> j_ratio;
            Pineapple* p = new Pineapple(weight, j_ratio);
            fruit[f++] = p;
            juice[j++] = p;
        }
        if(name == "Banana")
        {
            cin >> weight >> s_ratio;
            Banana* b = new Banana(weight, s_ratio);
            fruit[f++] = b;
            salad[s++] = b;
        }
        if(name == "Apple")
        {
            cin >> weight >> j_ratio >> s_ratio;
            Apple* a = new Apple(weight, j_ratio, s_ratio);
            fruit[f++] = a;
            juice[j++] = a;
            salad[s++] = a;
        }
    }

    double fruit_weight, salad_weight, juice_weight;
    fruit_weight = salad_weight = juice_weight = 0;
    for(int i = 0; i < f; i++)
        fruit_weight += fruit[i]->weight();
    cout << "All fruits weight " << fruit_weight << " kg." << endl;
    for(int i = 0; i < j; i++)
        juice_weight += juice[i]->juice_weight();
    cout << "These fruits can juicing " << juice_weight << " kg fruit juice." << endl;
    for(int i = 0; i < s; i++)
        salad_weight += salad[i]->salad_weight();
    cout << "These fruits can making " << salad_weight << " kg fruit salad." << endl;
}

就是这样。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值