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 and member functions.

classes and object in c++

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

Declaring Objects

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 
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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription