In C++, the beguilingly enigmatic class is a custom-made data type that allows programmers to encapsulate data and functions that bear upon that data.
This important concept of OOP represents a veritable game-changer, empowering the creation of objects, essentially viewed as class instances.
Table of Contents
Class Definition
A mysterious phenomenon known as a class is forged using a curious keyword called “class” in C++, that is pursued by a confounding name for the class, as well as the heart of the class, which includes data members and member functions. An enclosure seeks the class name in fascinating curly braces. To illustrate this peculiar concept, examine this perplexing code that establishes an introductory class called “Person”:
Data Members
Data members, variables that belong to a class and define an object’s state, can be classified as private, public, or protected. Their classification determines the accessibility of these members from outside the class. One can access public data members from anywhere. In contrast, one can only access private data members from within the class in C++, and protected data members can be accessed from within the class and its subclasses. Here’s an example to help you understand this concept better:
In this example, name and age are public data members, while ssn is a private data member.
Member Functions
Member functions are essential functions that act on the data members of a class. These functions come in three flavors – private, public, or protected, and their level of accessibility from outside the class depends on which flavor they belong to. Public member functions are universally accessible from anywhere. Private member functions can only be accessed from within the class, and protected member functions can be accessed from within the class and its subclasses. To elaborate, let’s consider the following example.
In this instance, the print() member function can be utilized from anywhere in the codebase since it is public, while the get SSN () function can only be accessed privately within the class.
Constructors
Constructors are member functions that serve the exclusive purpose of initializing the data members of a class object during its creation. These functions bear the identical name as the class they belong to and do not yield any output. Constructors can be categorized into two types: default constructors and parameterized constructors. Default constructors require no arguments, whereas parameterized constructors necessitate one or more parameters. To acquire a more profound insight into constructors, let us delve into the following example, which will elaborate on this significant concept.
In this example, Person() is a default constructor that initializes name and age to empty strings and zero, respectively. Person(string n, int a) is a parameterized constructor that takes a name and an age as parameters and initializes the corresponding data members.
Destructors
Destructors are special member functions that are called when an object is destroyed. They perform necessary cleanup tasks, such as releasing memory or closing files. Destructors have the same name as the class preceded by a tilde (~) and no parameters or return type. For example:
In this example, the destructor of the “Person” class outputs a message indicating that it has been called.
Access Specifiers
Access specifiers are curious keywords that establish the visibility of data members and member functions. These keywords can have a private, public, or protected nature. Private data members and member functions are merely accessible from inside the class, while public ones can be accessed from anywhere. As for protected ones, they lie somewhere in the middle, being accessible from within the class and its subclasses. For example:
In this example, name and age are public data members, print() is a public member function, ssn is a private data member, and getAddress() is a protected member function.
Inheritance
Inheritance is a crucial mechanism in object-oriented programming that allows a class to inherit the properties of another class. The base class or parent class is inherited, while the derived class or child class is the class that inherits from it. The derived class can access the public and protected members of the base class, promoting code reuse and making it easier to create new classes. For example:
In this example, Employee is a derived class inherited from the Person base class. It adds a new data member called salary and a new member function called printSalary().
Polymorphism
Polymorphism is characteristic of objects that allows them to manifest diverse behaviors or functions based on the context in which they are used. This property facilitates code extensibility and modularity by enabling the creation of generic classes and methods that can operate on multiple object types. One can achieve it through function overloading and virtual functions. Function overloading is the ability to define various functions with the same name but different parameters. Virtual functions are functions that are declared in the base class and described in the derived class. They allow a derived class to override the behavior of a base class function. For example:
In this example, Shape is a base class that declares a virtual function called draw(). Derived classes, Circle and Square, override the draw() function.
Encapsulation
Encapsulation is the intricately complex procedure of concealing the fine-grained implementation intricacies of a class from the external universe. This immensely aids in thwarting any inadvertent alterations to the data members and member functions. Encapsulation accomplishment happens via access specifiers in the magnificent programming language, C++. Private data members and member functions are exclusively reachable from within the class, whereas public data members and member functions are reachable from all corners of the cosmos. Example:
In this example, the implementation details of the BankAccount class, such as the account number and `balance
Operator Overloading
The intricacy of operator overloading lies in power to tweak the behavior of an operator as it interacts with objects of a class in C++. By doing so, programming enthusiasts can bask in the delight of more natural and intuitive syntax. C++ delivers a bunch of already defined operators, including but not limited to +, -, *, /, =, ==, !=, <=, >=, <, and >. These operators can overload while operating on objects of a class. This implies that a solitary operator can execute distinct operations depending on the data type applied. For example:
In this example, there is an overload of the + operator to work with objects of the Point class. The operator+() function takes another Point object as a parameter, adds its x and y values to the corresponding values of the current object, and returns a new Point object that represents the result.
Conclusion
A C++ class is a customized data type encapsulating data members and member functions. It furnishes a means to systematize and shape code and encourages reuse via inheritance. Access specifiers ascertain the perceptibility of data members and member functions. In contrast, polymorphism endows objects with the capability to assume diverse forms. Encapsulation assists in concealing the implementation minutiae of a class from external entities, and operator overloading permits more innate and discernible syntax. You can write proficient and sustainable C++ code by adeptly comprehending these principles.
Hi, I’m Geoff. I design. I develop. I do lots of things in between. What that really boils down to is that I make websites