In this lesson, we will learn about Objects and Classes in C++ and how to utilize them, with examples to understand the topic.
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.
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.
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; } };
Here, we defined a class named Student.
studentMathMarks
, studentEnglishMarks
, and studentTotalMarks
are double variables. studentName
, studentCollegeName are string variables. studentRollNo
is an integer variable.;
to the end of the class definition.studentName
, studentRollNo
, studentCollegeName
, studentMathMarks
, studentEnglishMarks
, and studentTotalMarks
are declared inside the class and are known as data members.calculateTotalMarks()
is the member function of a class.objectName
is the C++ syntax for denoting an object.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; }
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
The class’s functions are referred to as methods. You can define a class’s functions in one of two ways:
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.
// 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.
// 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.