C++ Class Attributes & Methods

 

Class Attributes & Methods

 

Class attributes and methods are variables and functions that are defined inside the class. They are also known as class members altogether.

 

Consider an example below to understand what class attributes are

#include <iostream>
using namespace std;

class Employee
{
    int eID;
    string eName;
    public:
};


int main()
{
    Employee Maxon;
}

 

A class named Employee is built and two members, eId and eName are defined inside the class. These two members are variables and are known as class attributes. Now, an object named Maxon is defined in the main. Maxon can access these attributes using the dot operator. But they are not accessible to Maxon unless they are made public.

 

class Employee
{
public:
    int eID;
    string eName;
};


int main()
{
    Employee Maxon;
    Maxon.eID = 5;
    Maxon.eName = "Maxon";
    cout << "Employee having ID " << Maxon.eID << " is " << Maxon.eName << endl;
}


Output

Employee having ID 5 is Maxon

 

Class methods are nothing but functions that are defined in a class or belong to a class. Methods belonging to a class are accessed by their objects in the same way that they access attributes. Functions can be defined in two ways so that they belong to a class.

  • Defining inside the class

An example that demonstrates defining functions inside classes is

class Employee
{
public:
    int eID;
    string eName;

    void printName()
    {
        cout << eName << endl;
    }
};

 

  • Defining outside the class

Although, a function can be defined outside the class, it needs to be declared inside. Later, we can use the scope resolution operator (::) to define the function outside.


An example that demonstrates defining functions outside classes is

class Employee
{
public:
    int eID;
    string eName;

    void printName();
};


void Employee::printName()
{
    cout << eName << endl;
}