References to Base class

box.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Box.h in Ex9_09
#pragma once
#include <iostream>

class CBox                             // Base class
{
  public:
    // Function to show the volume of an object
    void ShowVolume() const
    {
      std::cout << std::endl << "CBox usable volume is " << Volume(); 
    }

    // Function to calculate the volume of a CBox object
    virtual double Volume() const
    { return m_Length*m_Width*m_Height; }

    // Constructor
    explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv) {}

  protected:
    double m_Length;
    double m_Width;
    double m_Height;
};


CGlassBox.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// GlassBox.h in Ex9_09
#pragma once
#include "Box.h"

class CGlassBox: public CBox           // Derived class
{
  public:
    // Function to calculate volume of a CGlassBox
    // allowing 15% for packing
    virtual double Volume() const override
    { return 0.85*m_Length*m_Width*m_Height; }

	double add(int x,int a)
	{return x+a;
	}
    // Constructor
    CGlassBox(double lv, double wv, double hv): CBox(lv, wv, hv){}
};



Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Ex9_09.cpp
// Using a reference to call a virtual function
#include <iostream>
#include "GlassBox.h"                  // For CBox and CGlassBox
using std::cout;
using std::endl;

void Output(const CBox& aBox);         // Prototype of function

int main()
{
  CBox myBox(2.0, 3.0, 4.0);           // Declare a base box
  CGlassBox myGlassBox(2.0, 3.0, 4.0); 
  CBox& nib=myBox;
  CBox& ni=myGlassBox;// Declare derived box of same size

  Output(nib);                       // Output volume of base class object
  Output(ni);                  // Output volume of derived class object

  cout<<ni.ShowVolume()<<endl;
  return 0;
}

void Output(const CBox& aBox)
{
  aBox.ShowVolume();
}



On line 20 I know it should give a error but on my compiler I think it is not giving the correct error?

1>------ Build started: Project: Tests, Configuration: Debug Win32 ------
1>  Ex9_09.cpp
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_09\ex9_09.cpp(20): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


There was way more output but I couldn't fit it all here.

This is the error it should be giving

1>------ Build started: Project: Tests, Configuration: Debug Win32 ------
1>  Ex9_09.cpp
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_09\ex9_09.cpp(20): error C2039: 'add' : is not a member of 'CBox'
1>          c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 09\ex9_09\box.h(6) : see declaration of 'CBox'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Instead of using that function I used the add one in CGlassBox and I got expected behavior.


Edit: I figured it out, it is because it returns void right? and you cant output void lol
Last edited on
You should replace line 20 with just ni.ShowVolume()
I know I figured it out xD
Topic archived. No new replies allowed.