Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
Sep 11, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Numerous Access Specifiers in a Class Can Improve Readability

There is no limit to the number of access specifiers (private, protected, and public) a class can have. When large classes are declared, it is useful to repeat the access specifier frequently to improve readability. You can also group class members logically. For example, you can declare all constructors under one access specifier, the destructor with its own access specifier, and similarly--overloaded operators, ordinary methods, etc.:
class DatabaseTable { // a large class representing a relational database table
private:		//data members
	int rows;
	string tableName;
	int fieldsPerRecord;
		//some useful comments appear here
private: 		//access specifier repeated to improve readability
	Date created;
		//page break here
	Date lastSorted
public:  		//constructors
	DatabaseTable(const DatabaseTable& other); //copy constructor
	DatabaseTable(); //default constructor
public:  		//assignment operator
	DatabaseTable& operatot = (const DatabaseTable&);
public: 		//destructor
	//_ rest of member functions
In fact, you can explicitly declare every class member with its own access specifier (like in Java). As opposed to common belief, repetition of access specifiers does not impose any performance overhead so you can use as many specifiers as you like.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date