-2

I am trying to learn C++ and I ran the following code in Visual Studio 2019. I am getting 12 errors , but the code runs fine on the website.

You can find the code here: https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm

#include <iostream>

    using namespace std;

class Box{
public:
    static int objectCount;

    //Constructor definition
    Box(double l = 2.0 double b = 2.0 double h = 2.0) {
        cout << "Constructor Called." << endl;
        length = l;
        breadth = b;
        height = h;

        //Increase everytime the object is created
        objectCount++;
    }
    double Volume() {
        return length * breadth* height;
    }
private:
    double length;
    double breadth;
    double height;
};
// Initialize static member of class Box
int Box::objectCount = 0;

int main(void) {
    Box Box1(3.3, 1.2, 1.5);
    Box Box2(8.5, 6.0, 2.0);

    cout << "Total objects: " << Box::objectCount << endl;

    return 0;
}

enter image description here

user7377353
  • 43
  • 1
  • 2
  • 9

1 Answers1

1

Just modify the constructor of Box Class. You just missed , between parameters and name of b.

Box(double l = 2.0, double b = 2.0, double h = 2.0) {

Please see the full code as following:

#include <iostream>

using namespace std;

class Box {
public:
    static int objectCount;

    //Constructor definition
    Box(double l = 2.0, double b = 2.0, double h = 2.0) {
        cout << "Constructor Called." << endl;
        length = l;
        breadth = b;
        height = h;

        //Increase everytime the object is created
        objectCount++;
    }
    double Volume() {
        return length * breadth* height;
    }
private:
    double length;
    double breadth;
    double height;
};
// Initialize static member of class Box
int Box::objectCount = 0;

int main(void) {
    Box Box1(3.3, 1.2, 1.5);
    Box Box2(8.5, 6.0, 2.0);

    cout << "Total objects: " << Box::objectCount << endl;

    return 0;
}

Please get your result as following:

Constructor Called.
Constructor Called.
Total objects: 2
yaho cho
  • 1,779
  • 1
  • 7
  • 19