×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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.

Types of Data members and Member functions in C++

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.

Syntax of Data members and Member functions

Consider the below syntax with real data members and members functions in a C++ class:

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.


Example of Data members and Member functions

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

#include <string.h>

#include <iostream>
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("Alvin", 27);
  PER.put();

  return 0;
}

Output

Name: Alvin
Age: 27

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;

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.