C++ Classes & Objects

In this lesson, we will learn about Objects and Classes in C++ and how to utilize them, with examples to understand the topic.


What is an object?

  • An object is defined as an entity with a state and behavior. It might stand in for a canine, a person, a car, a table, etc. In other words, anything physically that exists in the world is referred to as an object.
  • The object is the mix of data and programs that further represent an entity.
  • The object has a state (properties).
  • The object has well-defined behavior or tasks (operations).
  • The object is a unique identity.
  • The object is an instance of a C++ class.

In C++, a class is defined by using the keyword class and the class name. The class’s body is defined inside the curly braces following a comma.


What is a class?

A class can be considered the object’s blueprint, and an object is an instance of the class. It is a group of things that serve as building blocks. We create several objects from the same template (Number of objects)

For example,

Student Class has many objects, and each object should follow the same prototype defined by Student Class.

Data members (variables) and member functions are both aspects of a class. The class’s data members can be modified using these member functions.

You can compare a class to a rough draught or prototype of a student. It includes all of the information regarding the studentName, studentRollNo, studentCollegeName, studentMathMarks, studentEnglishMarks, studentTotalMarks etc. We construct the card of a student by these descriptions. The object is a student.


C++ Class Creation

In C++, a class is defined by using the keyword class and the class name. The class’ body is defined inside the curly braces and following a comma.

Basic Syntax of Class

class className {
  //Data members (variables)
  //Member functions
};

Example

class Student {

  public:
  
  string studentName;

  int studentRollNo;

  string studentCollegeName;
  double studentMathMarks;
  double studentEnglishMarks;
  double studentTotalMarks;
  
  double calculateTotalMarks() {
    return studentEnglishMarks * studentMathMarks;
  }
};

Example described

Here, we defined a class named Student.

  • A class ‘Student’ is created using the class keyword.
  • The public keyword signifies that a class’s members (attributes and methods) are accessible to users outside the class. Access specifiers will be covered in more detail later.
  • studentMathMarks, studentEnglishMarks, and studentTotalMarks are double variables. studentName, studentCollegeName are string variables. studentRollNo is an integer variable.
  • Attributes are used to describe variables that are declared inside a class.
  • Finally, add a semicolon ; to the end of the class definition.
  • The variables studentName, studentRollNo, studentCollegeName, studentMathMarks, studentEnglishMarks, and studentTotalMarks are declared inside the class and are known as data members.
  • And the function calculateTotalMarks() is the member function of a class.

C++ Objects Creation

  • No memory or storage is allocated when a class is defined; only the object’s specification is defined.
  • We must build objects to use the data and access the class’s defined functions.
  • ClassName objectName is the C++ syntax for denoting an object.
  • C++ uses the following syntax to define objects of the student class, as defined in the example above.

Basic Syntax of Object

className objectName;

As we can see, any program function allows us to generate objects of a class. In C++, we can create a class’s objects within the class or in other classes. Additionally, we can generate as many objects from a single class as we like. Here, the main creates three instances of the Student class, Peter, John, and Craig.

Example

int main() {
  // create objects
  Student Peter, John, Craig;
}

C++ Accessing Data members and member functions

Using the . (dot) operator, we may access a class’s data members and member functions.

Examples

myStudent.calculateTotalMarks();

The function myStudent.calculateTotalMarks() will invoke the function calculateTotalMarks() for object Peter inside the Student class.

In this instance, the studentRollNo variable for the Student is initialized to 1234.

myStudent. studentRollNo = 1234

C++ Object and Class Example:

// Program illustrate objects and class in C++ Programming
#include <iostream>

using namespace std;
// create a student class
class Student {
  public:
    string studentName;
  int studentRollNo;
  string studentCollegeName;
  double studentMathMarks;
  double studentEnglishMarks;
  double studentTotalMarks;

  double calculateTotalMarks() {
    return studentEnglishMarks + studentMathMarks;
  }
};
int main() {
  // create an object for Student class
  Student myStudent;
  // assign values to data members

  myStudent.studentName = "John Rambo";

  myStudent.studentRollNo = 123456;

  myStudent.studentCollegeName = "Harvard";
  myStudent.studentMathMarks = 100;

  myStudent.studentEnglishMarks = 99;

  myStudent.studentTotalMarks = 199;
  // calculate and display the total student marks
  cout << "Total marks of Student is " << myStudent.calculateTotalMarks() << endl;
  return 0;
}

Output

Total marks of Student is 199

C++Class Methods

The class’s functions are referred to as methods. You can define a class’s functions in one of two ways:

  • C++ method inside the Class Definition
  • C++ method outside the Class Definition

The following example creates a calculateTotalMarks() function inside the Student class. To access methods, create an object of the class and use the dot syntax (.) as you would access attributes.


The method inside the C++ Class Definition:

// Program illustrate objects and class in C++ Programming#include <iostream>

using namespace std;
class Student { // The class: create a student class
  public: // Access specifier
    string studentName;
  int studentRollNo;
  string studentCollegeName;
  double studentMathMarks;
  double studentEnglishMarks;
  double studentTotalMarks;
  //function:: calculateTotalMarks() defined inside the class
  double calculateTotalMarks() {
    return studentEnglishMarks + studentMathMarks;
  }
};
int main() {
  // create an object for the Student class
  Student myStudent;
  // assign values to data members

  myStudent.studentName = "John Rambo";
  myStudent.studentRollNo = 123456;

  myStudent.studentCollegeName = "Harvard";

  myStudent.studentMathMarks = 100;
  myStudent.studentEnglishMarks = 99;
  myStudent.studentTotalMarks = 199;
  // Call the method: calculate and display the total student marks
  cout << "Total marks of Student is" << myStudent.calculateTotalMarks() << endl;
  return 0;
}

Output

Total marks of Student is 199

You must first declare a function inside the class definition before defining it outside the class if you want to define it outside the class definition. To do this, specify the class name, the scope resolution”:: “operator, the function name, and then the function name.


Example of Method outside the C++ Class Definition

// Program illustrates the objects and class in C++ Programming outside the Class Definition
#include <iostream>

using namespace std;
class Student { // The class: create a student class

  public: // Access specifier
    string studentName;
  int studentRollNo;
  string studentCollegeName;
  double studentMathMarks;
  double studentEnglishMarks;
  double studentTotalMarks;
  double calculateTotalMarks(); //function declaration
};
double Student::calculateTotalMarks() { //function definition
  return studentEnglishMarks + studentMathMarks;
}
int main() {
  // create object for Student class
  Student myStudent;
  // assign values to data members
  myStudent.studentName = "John Rambo";
  myStudent.studentRollNo = 123456;
  myStudent.studentCollegeName = "Harvard";
  myStudent.studentMathMarks = 100;
  myStudent.studentEnglishMarks = 99;
  myStudent.studentTotalMarks = 199;
  // Call the method: calculate and display the total student marks
  cout << "Total marks of Student is " << Peter.calculateTotalMarks() << endl;
  return 0;
}

Output

Total marks of Student is 199

This concludes the C++ Classes & Objects lesson. In The next lesson, you will learn about Class Constructors in C++ and their usage.