Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c++ static class?

This program won't compile correctly

// Bell.h file

#ifndef BELL_H

#define BELL_H

class Bell

{

private:

int count1;

static int count2;

public:

Bell()

{ count1 = 0; }

void ring(int);

int getCount() const

{ return count1; }

static int getStaticCount() const

{ return count2; }

};

#endif

//specification of ring function and main function (Bell.cpp)

#include <iostream>

#include "Bell.h"

using namespace std;

void Bell::ring(int num_rings)

{

for (int index = 0; index < num_rings; index++)

{

cout << "ring ";

count1++;

count2++;

}

}

int main()

{

const int NUM_RINGS1 = 3, NUM_RINGS2 = 4;

Bell bell1, bell2;

bell1.ring(NUM_RINGS1);

bell2.ring(NUM_RINGS2);

cout << "Count for first bell: " << bell1.getCount();

cout << "Count for second bell: " << bell2.getCount();

cout << "Static Counter: " << bell1.getStaticCount();

system("pause");

return 0;

}

Basically bell1 should ring 3 times and bell2 should ring 4 times

and should print out the count for each bell and the static counter,

which is the same for both.

Please help me? I'm not so good with programming

1 Answer

Relevance
  • 7 years ago
    Favorite Answer

    Take the const off of the declaration for getStaticCount. That only applies to non-static member functions. (see below)

    Add a *definition* (the class only declares it) in a .cpp file. Put it next to (but NOT inside of) the Bell::ring() method definition (before main()) for now, but both of those would ordinarily be in Bell.cpp as the implementation of the class.

    int Bell::count2 = 0; // static ring counter

    There is no "constructor" for the class to initialize static member variables, so you have to do that in a file-level declaration, like that one.

    After that, you'll get:

    ring ring ring ring ring ring ring Count for first bell: 3Count for second bell: 4Static Counter: 7

    ...so it looks like you need to add << endl; to some of your cout<< lines.

    About const member functions: The "const" in a member function makes the object the function is called on a constant. Since a static member function isn't associated with any object, there is no reason for const.

    PS: You should also add "#include <cstdlib>" if you use system(), since that's where system() is officially defined. VC++ includes this as part of <iostream>, but that's not required (or prohibited, apparently) so other compilers for Windows may fail. MinGW is one of them. I just commented out the system() call to get this to compile under Code::Blocks with MinGW. (C::B automatically inserts a pause at end of run, anyway.)

Still have questions? Get your answers by asking now.