[cs23021-2] on access specifiers

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Oct 27 19:32:04 EDT 2009


CS1 students,

I looked up on multiple "public" and "private" keywords in a single
class definition. The keywords are called "access specifiers". C++
syntax states that access specifiers can be mentioned in arbitrary
order and may repeat. g++ allows it as well. For example, the below
program compiles. Moreover, by default (with no access specifier) the
members are private. Here is more info on this

http://www.cplusplus.com/doc/tutorial/classes/

This said, I am used to having exactly two access specifiers in a
single class definition with "public:" followed by "private:". So for
now, I encourage you to stick to this format.

Thanks,
-- 
Mikhail

-----------
// testing multiple access specifiers
// Mikhail Nesterenko
// 10/27/2009

#include <iostream>
using namespace std;

class myclass{
public:
        // inline mutator
        void set(const int n){data=n;};
private:
        int data;
public:
        // inline accessor
        int get() const {return(data);};
};

int main() {
        myclass ob1;
        //              ob1.data=5;
        ob1.set(5);
        cout << ob1.get() << '\n';    // get the state of ob1

}


More information about the cs23021-2 mailing list