In C++ programming, a Class is a fundamental block of a program that has its own set of methods and variables. You can access these methods and variables by creating an object or the instance of the class. For example, a class of movies may have different movies with different properties, like genres, ratings, length, etc. You can access these properties by creating an object of class movies.
What Are Classes in C++?
A class is a user-defined data type representing a group of similar objects, which holds member functions and variables together. In other words, a class is a collection of objects of the same kind. For example, Facebook, Instagram, Twitter, and Snapchat all come under social media class.
Now, have a look at its declaration and definition.
Declaration and Definition of Class in C++
You can define classes using the keyword ‘class’ followed by the name of the class.
Here, inside the class, there are access-modifiers, data variables, and member functions. Now, understand them in detail.
Access modifiers: These are the specifiers which provide or grant access for the members. They are of three types:
Private: Only members of the same class have access to private members.
Public: You can access the public members from within, as well as from outside of the class.
Protected: You can access the protected members from the same class members and members of the derived class. It is also accessible from outside the class but with the help of the friend function.
Member-function: Member functions are standard functions declared inside a class. You can invoke it with the help of the dot operator and object, which you will see later.
Note: It is necessary to put a semicolon at the end of the class.
Example of class:
You looked through the basics of Classes in C++, now it’s time to learn about the Objects of a class.
Objects of a Class
An object is a recognizable entity having a state and behavior, and these objects hold variables of a class in accordance with the access modifiers. It is also known as an instance of a class.
You can call a member function with the help object and use the dot operator.
During the declaration of the class, no memory is assigned, but when you create an object, then the memory is allocated.
For better understanding, take a look at the syntax below.
Example:
The example stated above shows you how an object is declared in a class. Here, ‘State’ is the name of the class, and st is the object name.
Now, have a look at Constructors.
Constructor
Whenever you create an object of a class, a constructor is invoked, and that is why it is called a special member function. The constructor's name is like that of the class, and it doesn’t have any return type. Even if you do not include a constructor in the class, the compiler creates a default constructor. These are generally used for assigning initial values to variables.
Generally, they are of three types:
- Parameterized constructor
- Non-parameterized constructor
- Copy constructor
Now, take a look at an example of a constructor.
Example:
In the above example, Billboard is the name of the constructor, which is parameterized. While creating the object, you pass a string of Top 3 songs of 2020 to the constructor. Inside the constructor, the setTitle() function is being called. You pass the string as an argument to the function. Now inside the setTitle() function, you assign the string to the variable title. From the getTitle() function, you return the value of the title variable, which is the Top 3 songs of 2020.
Below is the output of the above example.
Now, you will learn how to implement a class in C++.
Implementation of Classes in C++
This example has created a class ‘Franchise’, and inside that class, there are two functions, i.e., KFC() and BurgerKing() with access specifier as public.
Inside the main function, there is an object fran of class Franchise. You will call both the functions KFC() and BurgerKing() with the help of object fran using the dot operator. By calling both the functions, the message inside these functions is displayed.
Below is the output of the example stated above.
Conclusion
After reading this tutorial on Classes in C++, you would have understood why classes are important in C++, and the significance of their declaration, and definition. You also learned about the object of a class and how to implement classes in C++ that are required for a better understanding of this topic.
Do you have any questions regarding this tutorial on Classes in C++? If you do, then please put them in the comment section. Our experts will help you solve your queries. To learn more about Classes in C++, click on the following link: Classes in C++
If you are perhaps looking to go beyond C++ and learn some of today’s most in-demand programming languages and techniques used by top programmers in the world, you should explore Simplilearn’s Post Graduate Program in Full Stack Web Development. This global boot camp is in collaboration with Caltech CTME and features tons of great benefits. You will gain over 30 work-ready tools and skills used by top companies today and get to practice and perfect each one of them through multiple projects that are part of this program. Explore and enroll today.
Happy learning!