The philosophy of Object-Oriented Programming revolves around the concept of classes and objects.

The Object-Oriented Programming approach considers data as the most critical element. A class helps in binding the related data and functions together. It also restricts access to sensitive data and functions it contains.

In this tutorial, you’ll learn about classes and objects with simple C++ coding samples.

What is a class

If you need to build a new data type, you need to write a class for it. This class serves as a blueprint or template for your new data type.

A class contains the data members and member functions that work on those data members. Thus, a class groups data and functions related to each other. We call it data encapsulation in technical language.

Classes support another object-oriented philosophy, the data abstraction. A class insists on providing only essential information to outside. It uses access specifiers such as public, private, and protected to impose restrictions.

The private members are inaccessible outside the class. The public members are accessible outside the class. The protected members are accessible outside the class but only within inherited classes.

Let’s consider, you need to create a class to store student data like - student id, student name, and course name. You need to have two member functions, to set and display data. You need to define the class as below -

class student {

  private:

  int id;
  string studentName;
  string courseName;

  public:

    //Function to set data
    void setStudentData(int idNo, string fName, string course) {
      id = idNo;
      studentName = fName;
      courseName = course;
    }

  // Function to display data
  void displayStudentData() {
    cout << "------------------" << "\n";
    cout << "Student ID: " << id << "\n";
    cout << "Student Name: " << studentName << "\n";
    cout << "------------------" << "\n";
  }
};

In the above example, we have a class, student.

The class has data members - id, studentName, and courseName. It has member functions setStudentData() and displayStudentData().

You have crafted the blueprint for your new data type. In the next section, you will learn about objects.

What is an Object

You can access the data and function members of your class after you create an object of the class.

Objects are nothing but instances of a class.

When you write a class, you create a template for your new data type. The system allocates memory for your data type only when you create at least an object of your class.

Let’s consider you need to insert and display data about two students. Thus you need to create two objects student1 and student2 of the student class as below -

student student1, student2;

Now you will be able to access class members to set and display data as below

//Setting data in objects
student1.setStudentData(1, "Steve Parkar", "Networking");
student2.setStudentData(2, "Mark Key", "Web Development");

//Displaying data
student1.displayStudentData();
student2.displayStudentData();

A code sample in C++

You already have written the class definition. You have learned how to create and use objects. Now let’s write the complete program for better understanding.

#include <iostream>

#include <string>

using namespace std;

class student {

  private:

   int id;   
  string studentName;
  string courseName;

  public:

    //Function to set data
    void setStudentData(int idNo, string fName, string course) {
      id = idNo;
      studentName = fName;
      courseName = course;
    }

  // Function to display data
  void displayStudentData() {
    cout << "------------------" << "\n";
    cout << "Student ID: " << id << "\n";
    cout << "Student Name: " << studentName << "\n";
    cout << "------------------" << "\n";
  }
};

int main() {

  student student1, student2; // Creating objects

  //Setting data in objects
  student1.setStudentData(1, "Steve Parkar", "Networking");
  student2.setStudentData(2, "Mark Key", "Web Development");

  //Displaying data
  student1.displayStudentData();
  student2.displayStudentData();

  return 0;
}

The above program defines a class, student. It creates two objects, student1 and student2 of the class to set and display data. The program produces below output-

------------------
Student ID:1
Student Name: Steve Parker
------------------
------------------
Student ID:2
Student Name: Mark Key
------------------

Conclusion

To sum up what you have learned so far -

  • A class is nothing but a template or a blueprint for a data type.
  • The concept of class helps in implementing data encapsulation and data abstraction.
  • Objects of a class are nothing but instances of the class
  • You can access the data and functions of a class through its objects.