answersLogoWhite

0


Best Answer

The correct terminology is member variables and member methods. They are both members of a class. The member variables are used to store an object's data, while the member methods are used to operate upon that data.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Data members are any containers that hold data, like variables or arrays inside object definitions. Member functions are methods or functions that are defined inside of objects and are generally used to manipulate data members and other object data.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A data member is a class member variable. That is, a variable that is declared to be a member of a class. The variable may be a primitive or complex data type (such as an object), or it may be a pointer to any data type.

The following examples show some of the ways member variables can be declared and initialised by a class. Note that member variables are typically declared private since it is normally only the class itself that requires direct access to the member variables.

class A

{

private:

int m_data; // member variable (primitive data type)

public:

A():m_data(0){} // default constructor

A(const A& rhs):m_data(rhs.m_data){} // copy constructor

A(const int rhs):m_data(0){set_data(rhs);} // overloaded constructor

int get_data()const{return(m_data);} // accessor (by value)

void set_data(int data){ // mutator with validation

if(data>-1000 && data<1000)

m_data = data; }

};

Note the purpose of class A is to store an int in the range -999 to +999 inclusive. If an invalid value is provided, or no value is provided, the class defaults the member to the value 0. The set_data() mutator method acts as the gatekeeper to the member variable, thus ensuring the data member is in a valid state at all times.

class B

{

private:

A m_object; // member variable (complex data type)

public:

B():m_object(){} // default constructor calling variable's default

constructor

B(const B& rhs):m_object(rhs){} // copy constructor

A get_object()const{return(m_object);} // accessor by value

void set_object(const A object){m_object=object;} // mutator

};

class C

{

private:

B* m_pobject; // member pointer (no access outside of this class)

public:

C():m_pobject(new B){} // default constructor instantiating pointer

C(const C& rhs):m_pobject(new B(*rhs.m_pobject){} // copy constructor (deep copy)

~C(){delete(m_pobject);} // destructor

C& operator=(const C& rhs){ // assignment operator

delete(m_pobject);

m_pobject = new B(*rhs.m_pobject);}

};

Note that class C does not expose the member variable because any code outside of the class could delete the pointer, thus invalidating the class. If you must expose a pointer via an accessor then it's often better to return a deep copy of the pointer (the caller is then responsible for storing and subsequently releasing the returned pointer). If you must return the pointer itself, ensure it is a constant pointer, even if the indirect value is to be treated as non-constant.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

I think you meant member data and member function respectively. Both are the major elements of a class of object. You use the member functions (methods) to operate upon and extract data from the object. In other words, the methods provide the interface to the member data. What use it is inside the main program is ultimately determined by the purpose of the program and the type of objects you employ to arrive at the solution.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are data members and members function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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 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.


Is it True or False that Variables declared in the particular member function are know as data members and can be used in all member functions of the class?

False. Variables declared within a particular member function are local to that function, and are automatically allocated and deallocated at entry and exit of the function.


Difference friend and public access modifier?

In C++, a friend function or friend class can grant access to its private data members to other classes. The public member allows any class to access that data.


Do declarations describe the data that will be used in the function?

Prototype of function describes return value as well as which data can be passed to the function.


What is the computer function in which processed data is displayed?

the computer function processed data is displayed is known as...


What is the function of the motherboard?

function of the processor is to process data


What is the function of a CD?

the function of a cd is to store data


What is the function of CD?

the function of a cd is to store data


What are public data members in a class?

Public data members are akin to structure data members (which are public by default). Since they are public, they are fully exposed outside of the class. Data validation becomes impossible. Even if you define an interface, there's no requirement to use it since the members are readily available. If the members must be validated, do not assign them public access.


What is the function of processor on motherboard?

function of the processor is to process data


What is the function where raw data is received is known as?

The input function is the computer function where raw data is received. This is done via an input device such as a keyboard and mouse.