Chapter Four: Introduction to Object-Oriented Programming in C++
The objective of the development of C++ was to add the concept of object-oriented programming to the C programming language. The classes used in C++ programming are the variables or structures used in object-oriented programming. These classes are often termed as user-defined data types. These concepts were discussed in brief in the previous chapter.
Classes are used to define the form and type of object used in the code. This data type uses both data methods and representation to manipulate the data present in the memory into one package. The functions and data within these classes are termed as class members.
Definition of Classes
Since classes are user-defined data types, you can define how the data type should be structured. When you do this, you do not define the data to be stored in the class, but you define the name of the class. You will also define the objects used in the class and the operations you can perform on the class's objects.
Use the keyword class when you define the class in your code. This keyword is followed by the class name and the body of the class. You enclose these data in curly braces. You can end a class declaration with a semicolon or list of data types, declarations, and functions. The following example defines a class named triangle.
class Triangle {
public:
double length;   // This variable denotes the length of the triangle
double height;   // This variable denotes the height of the triangle
double breadth;  // This variable denotes the breadth of the triangle
};
When you use the keyword ‘public,’ it denotes that different functions in the program can access the class's attributes or members. All you need to do is call the members accurately when you want to use the values in the function. If you do not want the values of the members in the class to change, you should use the keyword ‘private.’ We will discuss this in detail in this chapter.
Defining Class Objects
You can use a class to provide the detail of how an object should be defined in the program. Every object in the class is defined in the same way you define simple variables in the code. The following statements are examples of how to declare objects in a class. We are going to declare two objects for the class Triangle.
//The following lines of code are used to declare the objects Triangle1 and Triangle2 in the class Triangle
Triangle Triangle1;
Triangle Triangle2;
Now, each of these objects will have the same members (length, breadth, and height), which we declared while defining the class Triangle.
How to Access the Class Members
If the members in the class are public members, you can access them anywhere in the code using the access operator (.).
#include <iostream>
using namespace std;
class Triangle {
public:
double length;   // This variable denotes the length of the triangle
double height;   // This variable denotes the height of the triangle
double breadth;  // This variable denotes the breadth of the triangle
};
int main() {
//The following lines of code are used to declare the objects Triangle1 and Triangle2 in the class Triangle
Triangle Triangle1;
Triangle Triangle2;
//The objective of the code is to calculate the area of the triangle. We will now initialize a variable ‘area’ with the data type double and assign it the value 0.0
double area = 0.0
// We will now specify the parameter values for each of the class members
Triangle1.height = 5.0;
Triangle1.length = 6.0;
Triangle1.breadth = 7.0;
Triangle2.height = 10.0;
Triangle2.length = 12.0;
Triangle2.breadth = 13.0;
// We now define the function to us to calculate the area of the triangles.
volume = Triangle1.height * Triangle1.length * Triangle1.breadth;
cout << "Area of the first triangle: " << volume <<endl;
volume = Triangle2.height * Triangle2.length * Triangle2.breadth;
cout << "Area of the second triangle: " << volume <<endl;
return 0;
}
When you execute the above code, you receive the following output:
Area of the first triangle: 105
Area of the second triangle: 780
Note that you cannot access protected and private class members using the access operator (.). We will discuss how you can access protected and private class members in the code.
Classes and Objects
You now have a brief idea of what classes and objects in C++ are and how you can access the class members and objects. We will look at other aspects of object-oriented programming in further detail later in the book. Before we move onto the next chapter, let us look at some points you need to keep in mind when you work on object-oriented programming.
S. No.
Concept
Description
1
Class members and functions
You can define member functions in classes similar to the way you define member data types or variables.
2
Class access modifiers
The keywords public, private, and protected are termed as class access modifiers. If you have not defined the access modifier for the class members, the compiler takes the value as ‘private.’
3
Constructors and destructors
Class constructors are special functions in C++, and these can only be used within classes, especially when you create a new class object. A destructor is another function created when you delete an object from the class.
4
Copy constructor
This function creates another object in the class by initializing and declaring the object using another object in the same class.
5
Friend functions
If defined as such in the code, Friend functions can access protected and private members present in the class.
6
Inline functions
An inline function is one that instructs the compiler to expand the entire code in the class using the details of the function without using a call to access the function.
7
This pointer
Objects in classes are assigned pointers, and every object has only one pointer assigned to it. This pointer only points to the memory location of the object.
8
Pointer to classes
Any pointer in a class works the same as a pointer to a structure does. It is important to note that a class is only a structure with different members, objects, and functions.
9
Static class members
Both function members and data members defined in a class can always be static class members.