谁能解决这个问题。我试过了,我做不到。 [英] Can anyone solve this. I tried and I could not do it.

查看:72
本文介绍了谁能解决这个问题。我试过了,我做不到。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明一个存储浮点值列表的类Chart。定义一个名为plot chart()的纯虚函数。派生PieChart和BarChart类。在这两个类中实现plotchart()函数。



我尝试过:



Declare a class Chart that stores a list of floating-point values. Define a pure virtual function called plot chart(). Derive the classes PieChart and BarChart . Implement the plotchart() function in both these classes.

What I have tried:

#include<iostream>
#include<stdio.h>
using namespace std;

class chart
{
    float v;
public:
    virtual void plot_chart( float ,int ) = 0;
};

class PieChart: public chart
{
    float y;
public:
    void plot_chart(float u[],int n1)
     {
         int i;
         cout<<"Virtual function has been called through the object of PieChart.The array is :";
         for(i=0;i<n1;i++)
         {
             cout<<"\n"<<u[i];
         }
     }
};

class BarChart: public chart
{
    float z;
public:
    void plot_chart(float m[],int n2)
     {
         int j;
         cout<<"Virtual function has been called through the object of BarChart.The list of floating values is :";
         for(j=0;j<n2;j++)
         {
             cout<<"\n"<<m[j];
         }
     }
};

int main()
{
    chart x[100];
    int i,n;
    cout<<"\n Enter the size of the list :";
    cin>>n;
    for (i=0;i<n;i++)
    {
        cout<<"\nEnter element "<<i+1<<":";
        cin>>x[i];
    }
    PieChart o;
    o.plot_chart();
    BarChart t;
    t.plot_chart();


    return 0;
}

推荐答案

您误解了图表与数据之间的关系。在您的代码中,每个图表类型都有一个数据值(v,y或z),然后您有一个图表数组。它需要反过来。您应该有一个图表,图表应显示一组数据值。根据您的要求,Chart基类必须存储浮点数据列表。
You have misunderstood the relationship between a chart and the data. In your code, each chart type has one data value (v, y, or z) and then you have an array of charts. It needs to be the other way around. You should have one chart and the chart should display an array of data values. According to your requirements, the Chart base class has to store the list of floating point data.


这篇关于谁能解决这个问题。我试过了,我做不到。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆