C++ Classes and Objects

What are Classes and Objects in C++ ?

Classes and objects are building blocks of C++. There are user-defined data types that are created to mimic real-world objects.

Classes hold their own –

  • Data Members
  • Member Functions
class

Why Classes?

Programmers long demanded a feature in the programming language that allowed creating an interface with which they can define some common properties and methods that can be used by instances of that interface.

Let’s take an example to understand this –

Car as a class/interface, all cars in the world share some common properties –

  • colour
  • tyres
  • mileage
  • weight
  • speed-limit etc

Similarly, all cars in world have common functions like – start(), brake(), stop(), reverse() etc.

With classes, we can create an interface of such common properties and methods and create thousands of instances i.e. objects to share them.

Classes & Objects in C++

Another Example

All students in school have some common properties –

  • Roll No
  • DOB
  • Weight
  • Name
  • Email ID

We can create a class with the name student to have all these properties defined.

If there are 1000 students in a school, we create 1000 objects unique to each student to store their details

Classes & Objects in C++ – 1

Defining Class

// name of the class
class Student
{
    // access specifier
    public:
    
    // data members variables
    int rollNo, weight, age;
    string name;
    
    // member functions
    void displayDetails()
    {
        cout << "Roll No: " << rollNo << endl;
        cout << "Name: " << name << endl;
    }
};

Declaring Objects

Inside Main

int main(){
    
    // declaring two objects here
    class_name object_name1, object_name2;
    
}

Outside Main, just After Class definition

class class_name
{
    
    //declare data members 
    // declare member functions
    
}object_name1, object_name2;

Example to Demonstrate above

Run
#include <iostream>
using namespace std;

// name of the class
class Student
{
    // access specifier
    public:
    
    // data members variables
    int rollNo, weight, age;
    string name;
    
    // member functions
    void displayDetails()
    {
        cout << "Roll No: " << rollNo << endl;
        cout << "Name: " << name << endl;
    }
};

int main(){
    
    // declare Student object s1
    Student s1;
    
    // assigning values
    s1.rollNo = 1;
    s1.weight = 80;
    s1.age = 21;
    s1.name = "Peter";
    
    // calling function for s1 object
    s1.displayDetails();
    
    return 0;
}

Output

Roll No: 1
Name: Peter

Defining Member Functions

Member functions can be declared in two ways –

  • Inside the class (show in previous example)
  • Outside the class (Using scope resolutor)

Following is the template if you want to declare member function outside class

return_type class_name::functionName(arguments){

    // function working here

}

Note, even though the function is defined outside, it must be declared inside the class as –

functionName(arguments);

Lets see an example below for both ways of function declaration –

Example of Function Declaration (Both ways)

Run
#include <iostream>
using namespace std;

// name of the class
class Student
{
    public:
    
    int rollNo, weight, age;
    string name;
    
    // function declaration & definition inside
    void printRollNo()
    {
        cout << "Roll No: " << rollNo << endl;
    }
    
    // function declaration inside
    void printName();
};

// function definition outside
void Student::printName(){
    cout << "Name: " << name << endl;
}
int main(){
    Student s1;
    
    // assigning values
    s1.rollNo = 1;
    s1.name = "Peter";
    
    s1.printRollNo();
    s1.printName();
    
    return 0;
}

Output

Roll No: 1
Name: Peter

Check out more –