answersLogoWhite

0


Best Answer

Members can be declared anywhere within a class declaration, and in any order.

Some programmers like to keep data member declarations at the top of the class declaration, either before or after the constructors, while others prefer them at the very bottom, but it really doesn't matter where they are placed as long as they appear somewhere (with an appropriate private, protected or public access specifier).

The order they are declared is generally only of importance when debugging, as the order of declaration generally dictates the order in which they are presented by the debugger's watch windows. Many debuggers will list the first few member variables without the need to expand the class completely, thus the variables that are of most interest should be placed before those of less interest.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Where should data member declarations of a class be?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


What is the difference between static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


Only public member functions can access public member data TrueFalse?

False. Public member data is accessible to all functions, whether they be public, protected or private members of the same class, or they are outside of the class completely.


Why cannot initialize data member within class?

A class is a type. Classes don't do anything except define the type. You have to instantiate an object of the type in order to actually do anything, including initialising data members. However, the class can define how a data member is initialised. The most efficient method of initialising class members if via the class constructor initialisation list.

Related questions

Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


What is the difference between static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


What is a static data linkage?

Static data members of a class in namespace scope have external linkage. Static data members follow the usual class access rules, except that they can be initialized in file scope. Static data members and their initializers can access other static private and protected members of their class. The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile. The declaration of a static data member in the member list of a class is not a definition. The definition of a static data member is equivalent to an external variable definition. You must define the static member outside of the class declaration in namespace scope.


Can you return a reference to public data member?

Yes, but since it is public there is no need to; any code outwith the class can access a public data member directly. If it were a private data member, then returning by reference would defeat the point of making it a private data member in the first place; you might as well make it public. Private data members should always be returned by value, never by reference and never by pointer, and data members should only be declared public if they are not critical to the internal operation of the class.


What is Difference between local variable and data members or instance variable?

A data member belongs to an object of a class whereas local variable belongs to its current scope. A local variable is declared within the body of a function and can be used only from the point at which it is declared to the immediately following closing brace. A data member is declared in a class definition, but not in the body of any of the class member functions. Data members are accessible to all member function of the class.


Only public member functions can access public member data TrueFalse?

False. Public member data is accessible to all functions, whether they be public, protected or private members of the same class, or they are outside of the class completely.


What is message passing in c plus plus?

-define class with necessary data member & member function. -create object of that class. -communication.


Why cannot initialize data member within class?

A class is a type. Classes don't do anything except define the type. You have to instantiate an object of the type in order to actually do anything, including initialising data members. However, the class can define how a data member is initialised. The most efficient method of initialising class members if via the class constructor initialisation list.


Are accessor member functions a sign of poor class design?

No, accessor member functions are a sign of good class design, particularly in terms of data encapsulation.


What is the reason for making Data as Private Member of a Class?

this is the reason for security purpose


What is nested class?

A class declared as a member of another class is called as a nested class or a class with in class. the general syntax of the nested class is: class outer_class_name{ private: //data protected: //data public: //methods class inner_class_name{ private: //data of inner class public: //methods of inner class };//end of inner class };//end of outer class