Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Implement Two Virtual Function Methods?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 65
    Answer it

    So what I've been given: Mean.cc is a class that inherits polymorphically from Statistic.h (the only things defined in that class are virtual void Collect(double) = 0; and virtual double Calculate() const = 0;)

    So the mean.cc is defined as:

    /* Stores data (datum) such than an average may be calculated.
     * - NOTE: You do not necessarily need to store each datum.
     */
         void Collect(double datum);
    
    /* Returns the mean of the data (datum) from the Collect method.
     */
         double Calculate() const;

    And the methods need to be able to pass these tests:

      void TestMeanCollect(Statistic* stat) 
      {
        const double kData[] = {34, 54, 99, 102, 43};
        const unsigned int kSize = sizeof(kData) / sizeof(double);
    
        for (unsigned int i = 0; i < kSize; ++i)
        stat->Collect(kData[i]);
       }`
    
       bool TestMeanCalculate(Statistic* stat) 
      {
        const double kData[] = {34, 54, 99, 102, 43};
        const unsigned int kSize = sizeof(kData) / sizeof(double);
        const double kExpected = (34 + 54 + 99 + 102 + 43) / 5.0;
    
        for (unsigned int i = 0; i < kSize; ++i)
        stat->Collect(kData[i]);
    
        double actual = stat->Calculate();
        if (kExpected != actual) {
        cout << "  Expected: " << kExpected << ", Actual: " << actual << 
        endl;
        return false;
       }
    
      return true;
    }

    What I'm confused about is, can someone please explain the logic of how to do either of these methods? I'm not requesting straight code, I just need an idea of where to start because I'm drawing a complete blank. The way I tried it before is as shown below, but I've been told that that ignores the functions parameters/negates what the actual method is being asked to do.

    void Collect(double datum)
      {
          double datum = 0;
          int kSize = 5;
          double kData[5];
          cout << "You can enter 5 values" << endl;
          cout << "Please enter your values" << endl;
          for (int i = 0; i < kSize; ++i)
          {
              cin >> kData[i];
              datum += kData[i];
          }
          datum = datum / kSize;
      }

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: