Home » C++ programming language

Data Members and Member Functions in C++ programming



Learn: what are the data members and member functions in C++ programming language? Data members are the variables and member functions are the functions used in the class in C++ programming.
Submitted by IncludeHelp, on May 28, 2018

Data members and Member functions in C++

"Data Member" and "Member Functions" are the new names/terms for the members of a class, which are introduced in C++ programming language.

The variables which are declared in any class by using any fundamental data types (like int, char, float etc) or derived data type (like class, structure, pointer etc.) are known as Data Members. And the functions which are declared either in private section of public section are known as Member functions.

There are two types of data members/member functions in C++:

  1. Private members
  2. Public members

1) Private members

The members which are declared in private section of the class (using private access modifier) are known as private members. Private members can also be accessible within the same class in which they are declared.

2) Public members

The members which are declared in public section of the class (using public access modifier) are known as public members. Public members can access within the class and outside of the class by using the object name of the class in which they are declared.

Consider the example:

class Test
{
	private:
		int a;
		float b;
		char *name;

		void getA() { a=10; }
		...;
		
	public:
		int count;
		void getB() { b=20; }

		...;
};

Here, a, b, and name are the private data members and count is a public data member. While, getA() is a private member function and getB() is public member functions.



C++ program that will demonstrate, how to declare, define and access data members an member functions in a class?

#include <iostream>
#include <string.h>
using namespace std;

#define MAX_CHAR 30

//class definition
class person
{
	//private data members
	private:    
		char name [MAX_CHAR];
		int age;
	
	//public member functions
	public:     
		//function to get name and age
		void get(char n[], int a)
		{
			strcpy(name , n);
			age = a;
		}
		
		//function to print name and age
		void put()
		{
			cout<< "Name: " << name <<endl;
			cout<< "Age: " <<age <<endl;
		}    
};

//main function
int main() 
{
	//creating an object of person class
	person PER;

	//calling member functions
	PER.get("Manju Tomar", 23);
	PER.put();

	return 0;
}

Output

    Name: Manju Tomar
    Age: 23

As we can see in the program, that private members are directly accessible within the member functions and member functions are accessible within in main() function (outside of the class) by using period (dot) operator like object_name.member_name;






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.