C++ Class Members and Objects

To apply Object-Oriented Programming (OOP) concepts in C++, we must learn its underlying syntax and behavior. Every language has its implementation of the OOP concepts, and it is mostly the same.

More like this:

This lesson will teach you the core syntax of C++ classes and objects and how to use them.

Data Members of a Class

Access Specifiers

There are different levels of access specifiers, which are private, protected, and public. We can define the class data and functions inside any of these access specifiers.

In this lesson, we will define all class members under the public access specifier, which will make our code accessible outside the class definition.

Declaring Data Members

Data members are simply variables declared inside a class.

class Product {
    /* declare all the data just like normal variables */
    int id, qty;
  float price;

In the example above, we have defined a class named Product. Under its public specifier, we have declared a few variables, which are now class data members.
[adinserter block=”2″]

Member functions of a class

Any function that is part of a class definition is called a member function. It allows us to add some behavior or action to an object. For example, using member functions, we can update the class data members and perform other useful operations.

There are two ways to define them:

  1. Inside the class
  2. Outside the class

Defining Functions inside a Class

Inside a class, we can define a member function just like a normal function under any access specifier. The example below defines a member function to update the product qty data.

class Product {
    /* declare all the data just like normal variables */
    int id, qty;
  float price;
  void add_qty(int n) {
    /* a member function to increment the quantity of the product*/
    qty = qty + n;

However, there is a difference between a normal function and a member function of a class. A member function can directly access any data member of a class without passing it as a parameter.

Defining Functions outside a Class

There are certain advantages to define a member function with a large amount of code outside. For defining member functions outside a class, follow these two steps:

  1. Create the required function declaration inside the preferred access specifier.
  2. Define the function outside the class, and replace the functionname with classname:: functionname to resolve the scope of the member function. The code below explains the steps.
class Product {
    int id, qty;
  float price;
  /* 1. It is important to declare the function first */
  void add_qty(int n);
/* 2. Define the function outside the class using the scope resolution(::) operator */
void Product::add_qty(int n) {
  qty = qty + n;
[adinserter block=”2″]

Declaring and Initializing objects

Once we have defined all the necessary class members, we are ready to create objects from them.

There are two ways to declare objects:

1. Declaring Objects immediately after the class definition.

We can define objects after the class definition by separating each object with a comma. It must be terminated by a semicolon whether the objects are declared or not.

class Product {
  /* class data and function definitions… */
p1, p2;

In the example above, p1 and p2 are objects of the class Product.

2. Declaring Objects using a class tag.

We can also use the class tag to declare and create objects separated by commas. This is a common method of declaring objects. It is similar to creating variables using some data type.

The code below declares three objects of the class Product.

Product p1, p2, p3;

Working with objects

We can use the dot (.) operator to access any member of an object. Also, the dot operator can only access the publicly defined class members.

The code below shows an example of working with objects by accessing its members.


using namespace std;
/* define a product class */
class Product {
    int id, qty;
  void add_qty(int n) {
    qty = qty + n;
int main() {
  /* declare two Product objects */
  Product p1, p2;
  /* access and initialize their id data */
  p1.id = 0;
  p2.id = 1;
  /* read qty values from the user */
  cout << "Enter the qty for product 1: ";
  cin >> p1.qty;
  cout << "Enter the qty for product 2: ";
  cin >> p2.qty;
  /* increment the quantity of product 1 by 10 */
  cout << "The final product 1 qty is: " << p1.qty << endl;
  cout << "The final product 2 qty is: " << p2.qty << endl;


Enter the qty for product 1: 4
Enter the qty for product 2: 7
The final Product 1 qty is: 14
The final Product 2 qty is: 7

If you study the above code carefully, you will find these two conclusions:

  1. First, we can use the object’s data members just like normal C++ variables. We can assign values to it, read inputs, access and read its value, etc.
  2. A member function can only modify the values of that object on which it was invoked.

So, if we assign 10 to both the qty of p1 and p2 objects as:

p1.qty = 10;
p2.qty = 10;

and then calling the add_qty() member function on p1 as:


Then this member function will increment and modify the p1’s qty by 5, and p1.qty will be 15 while p2.qty will remain the same, which is expected.
[adinserter block=”2″]

Scope of access

Scope defines the accessibility of a certain class, function, variable, or object. The class must be in scope to declare an object of a particular class.

Global Class

We can define a class outside of all the functions. Then the class is in the global scope, and its objects can be created anywhere in the program.

Local Class

If we define a class inside some function, it will be possible to create objects of that class only inside that function.

The example below shows a class defined globally and a class defined inside the main function.

class X1 {
  /* globally declared class */
int main() {
  class X2 {
    /* locally declared class, only visible inside this function */
  /* can declare objects of X1 and X2 as both are in scope */
  X1 a, b;
  X2 c, d;
void myfunc() {
  X1 a, b;
  /* error, since the class X2 is not in scope */
  X2 c, d;

We can extend the same concept to global or local objects depending on where it is declared.

If we declare an object globally, it will be accessible anywhere in the program. Similarly, if we declare our object inside a function, it will be a local object.

Points to Remember

  • Class members are always defined under these access specifiers: private, protected, and public.
  • “Data members” are variables of a class, while “member functions” are the functions of a class.
  • Class Member functions can be defined internally or externally (using scope resolution operator).
  • Dot (.) operator allows us to use the public members of an object.
  • C++ also allows an array of objects, as shown below:
    /* declare an array of 10 objects */
    classname object[10];
    /* access each object using the array index */
    object[2].property = value;
  • The rules of scope apply to classes and objects too. Thus, for example, the class definition must be in scope to declare its objects.
Back to: Learn C++ Programming > Object Oriented Programming in C++

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.