简体   繁体   中英

defining a private member function outside class definition and use of static data

I have two questions for given class Rectangle.

class Rectangle{
private:
int l;
int w;
static int count;
};

1) how do I define different private, public and protected functions outside the class definition? also how can i make them static function?

2) A private variable cannot be accessed outside the class definition, but a static data can be called directly using class name without creating any object. Can i access private static data count directly in main() function, like Rectangle.count=1 ? If no, should i always declare static data as public?

I know that we define member function using scope operator. but i don't know how to make them private or public.

int Rectangle::area()
{
return l*w;
}
  1. Definition of class member functions outside the class definition:

     class Rectangle { public: int area() const; // member function declaration private: int l; int w; static int count; }; int Rectangle::count = 0; // initialization of the static variable int Rectangle::area() const { // member function definition return l * w; } 
  2. Can i access private static data count directly in main() function, like Rectangle.count=1 ? If no, should i always declare static data as public?

    No and no. Making it accessible from outside the class would be no different from making it a global variable, and if that's what you want, make it a global variable. Otherwise, only let Rectangle objects, static member functions or friend s access the static data. You could for example make main() a friend of the class to let it get raw access to the data (both in Rectangle objects and the static data):

     class Rectangle { private: friend int main(); }; 

    but this should be avoided if you can. It's most often better to create member functions for accessing the raw data - or else you'd suddenly have main() changing the count value which I guess would be a terrible idea in this case.

I know that we define member function using scope operator. but i don't know how to make them private or public.

They are what you declared them to be in your class definition. If they are declared public , they will be public etc.


Example of an instance counter:

class Rectangle {
public:
    Rectangle(int L, int W);
    Rectangle();
    ~Rectangle();

    int area() const;

    static unsigned get_count();

private:
    int l;
    int w;
    static unsigned count;
};
unsigned Rectangle::count = 0;

unsigned Rectangle::get_count() {
    return count;
}

Rectangle::Rectangle(int L, int W) : l(L), w(W) {
    ++count;
}

Rectangle::Rectangle() :
    Rectangle(0, 0) // delegate
{}

Rectangle::~Rectangle() {
    --count;
}

int Rectangle::area() const {
    return l * w;
}
#include <iostream>

int main() {
    {
        Rectangle r;
        std::cout << Rectangle::get_count() << "\n";
    }
    std::cout << Rectangle::get_count() << "\n";
}

Output:

1
0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM