C++ Classes and Objects

Supporting Object Oriented Programming (OOP) was one of the main reasons C++ was originally created as an extension of C. Object Oriented Programming is a software design paradigm around the objects and classes.

Objects are the instances of classes. Classes are kind of blueprints for the objects that define what type of data or functionality the objects will have. The classes and the objects represent the basic building blocks of an object oriented programming like C++.

Here we’ll learn the basics of classes and objects.

Defining Class

Here we defined a very simple class to represent students.

class Student {
public:
    Student(int id, string name) {
        id_ = id;
        name_ = name;
    }

    ~Student() {
    }

    string getName() {
        return name_;
    }
private:
    int id_;
    string name_;
};

This class has two properties or data, id and name. And one function, getName(). It also have a constructor and a destructor.

Important Class Components

A class can have different type of components. But here we’ll discuss about the most commonly used components.

  • Name: Every class must have a name. Here our class name is Student that comes right after the class keyword.
  • Access Specifier: Specifies who can access the class members. We used two access specifiers here – public and private. The public class members can be accessed both from inside or outside a member function. But the private members can only be accessed from a member function of the same class only.
  • Constructor: Constructor is a special method that is called automatically in time of object instantiation or creation from the class. Name of the constructor will be exactly the same as the class name. Constructor can take inputs but can’t return any value. Constructors are generally used to initialize the member variables (properties).
  • Destructor: Destructor is also a special kind of method that gets called automatically right before the object is destroyed. A destructor always starts with a “~” followed by the exact class name. Destructor neither takes any input nor returns any value. Destructors are generally used to clean up or release the resources held by the object.
  • Data or properties: Class members that hold some information. In our class, we have two properties – id_ and name_.
  • Functions: Member functions the functions that does something. We have one member function getName() that returns the name of the student. Members function generally operates on the member variables as well as the input parameters.

Apart from these components, a class can have static members, overloaded operators, friend functions among others.

Objects

Objects are defined as the class instances. Each object will have their own copy of the member variables.

#include <iostream>
#include <string>

using namespace std;

class Student {
public:
    Student(int id, string name) {
        id_ = id;
        name_ = name;
    }

    ~Student() {
    }

    string getName() {
        return name_;
    }
private:
    int id_;
    string name_;
};

int main() {

    Student s1(1, "Tom");
    Student s2(2, "Jack");

    cout << "First Student Name: " << s1.getName() << endl;
    cout << "Second Student Name: " << s2.getName() << endl;
    
    return 0;
}
$ g++ -o test test.cpp 
$ ./test 
First Student Name: Tom
Second Student Name: Jack

For example, we have created two instances s1 and s2 from out Student class – one for “Tom” and another for “Jack”. In time of creating the objects, we passed the student identifier and name as input. The constructor will be called with these parameters and initialize the member variables. Later we printed the student names. We called the method getName() of the corresponding objects to get the names.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

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

0
0
0
0
0
0