-1

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;
}
Ayush Gupta
  • 37
  • 1
  • 6
  • 1
    What do you mean by "define different private, public and protected functions outside the class definition"? Can you please try to elaborate? And the answer to both your questions should be in any [good book](https://stackoverflow.com/a/388282/440558). – Some programmer dude Sep 12 '19 at 10:11
  • @Ayush Gupta It is a too broad question. Read a book on C++. – Vlad from Moscow Sep 12 '19 at 10:12
  • i tried looking up for answer but i couldn't find it. I was asking, if this area() function will public or private? if it is public function, how can i make it private or protected? – Ayush Gupta Sep 12 '19 at 10:13
  • That is really basic C++, and even pretty bad books should have taught you how to *declare* a member function inside the class, and them *define* (implement) it outside. – Some programmer dude Sep 12 '19 at 10:16

1 Answers1

3
  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 friends 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
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108