C++ Class: The Complete Guide
C++ is one of the most famous programming languages. Do you know why? Because it is an object-oriented, intermediated, and pre-compiled language. It has a market share of 0.10% and several developers use it for creating complex applications. In this blog, we will learn about an important concept of C++, i.e., C++ class. We will also learn how to implement these classes in C++.
What is C++ Class?
A C++ class is a user-defined data type. This data type consists of member functions and data members. The data members are functions used to manipulate variables together. These member functions and data members define the behavior and properties of the objects in the class.
Users can access these members and functions by creating a class instance. So, a C++ class acts like a blueprint of an object. One example is a class of movies. Different movies have different properties, such as length, ratings, genre, etc., and one can access them by creating an object of class movies. Similarly, another example is social media. Instagram, Facebook, Twitter, etc., all come under the social media class.
How to Declare and Define Class in C++?
You can define a class in C++ with the help of a keyword followed by the class’s name. The curly brackets define the body with a semicolon at the end.
The syntax for class in C++ is as follows:
class className
{
Access-specifier
Dara_variables;
Member-function
{
Body
}
};
As you can see, there are access modifiers, data variables, and member functions present inside the class.
Access Modifier
- Three types of access modifiers exist:
- Private: It is accessible only to members of the same class.
- Public: One can access the public members from outside and within the class.
- Protected: You can access the protected members, but they must be of the same class. They can be from inside or outside but will require a friend function.
Member Function
These are the standard functions.
Check out the example below.
class size {
public:
double length;
double breadth;
double height;
double calculateVolume(){
return length * breadth * height;
}
double calculateArea(){
return length * breadth;
}
};
Here, the class is defined by size, and the variable is length, breadth, and height. These variables declared inside the class are data members, and calculateVolume and calculateArea are member functions.
Object in C++
When a class is defined, only the specification for the object is defined and no memory or storage is allocated. You need to create objects to use the data and access functions defined in the class.
Syntax:
ClassName ObjectName;
You can create objects of size, as seen in the above example.
// sample function
void sampleFunction() {
// create objects
Size Size2, Size3;
}
int main(){
// create objects
Size Size4, Size5;
}
Here, there are two objects: Size2 and Size3. You can see that we have created these objects in the sampleFunction(). Similarly, the other two objects, Size4 and Size5, are created in main().
To learn more about objects and class in C++, consider opting for this in-depth C++ training course.
What are Constructors?
Constructors are invoked when you create an object. They are known as the special member function, and there is no return type. When you do not add constructor, a default constructor will be added. These are used to assign the value of the initial variable.
There are three types of constructors:
- Parameterized constructors
- Copy constructors
- Non-parameterized constructors
Let us understand with an example.
#include <bits/stdc++.h>
using namespace std;
class Intern
{
public:
int id;
//Default Constructor
Intern()
{
cout << "Default Constructor called" << endl;
id=-2;
}
//Parameterized Constructor
Intern(int x)
{
cout <<"Parameterized Constructor called "<< endl;
id=x;
}
};
int main() {
// obj1 will call Default Constructor
Intern obj1;
cout <<"Intern id is: "<<obj1.id << endl;
// obj2 will call Parameterized Constructor
Intern obj2(11);
cout <<"Intern id is: " <<obj2.id << endl;
return 0;
}
The above code will show the output as:
Default Constructor called
Intern id is: -2
Parameterized Constructor called
Intern id is: 11
What are Destructors?
The object’s memory is released by the object’s destructor. It merely destroys the target. The symbol that denotes a destructor is: ~ (tilde)
class Room{
//..
public:
//constructor
//..
//destructor
~Room(){
//destroy object here
}
};
Implementing Classes in C++
Let us take an example to understand the implementation of classes in C++. There are two functions, i.e. Zara() and HM(), with the access specifier as public.
The object Fashion is inside the main function. You will call both functions Zara() and HM() with the help of object fash and the dot operator. When you call both functions, the message inside these functions will be displayed.
#include <iostream>
using namespace std;
class Fashion {
public:
void Zara() {
cout << "the best jeans" << endl;
}
void HM() {
cout << "the best dresses" << endl;
}
};
int main() {
Fashion fash;
fash.Zara();
fash.HM();
return 0;
}
The above code will show the output as:
the best jeans
the best dresses
What is the Difference Between Structure and Class?
There are some similarities between structure and C++ class. However, there are some notable differences too.
The structure is a grouping of values that might not be the same. Basically, it is a user-defined data type. You can say the structure is a sort of record-keeping. It keeps a record of the data related to the entity. On the other hand, a class does the same thing in C++, but with more features, such as it allows to define methods/functions inside them.
If you have a huge memory usage, it is recommended to use class. However, you can use structure if you have a short memory field and want to initialize default values.
Conclusion
We hope by now you have understood what are objects in C++. C++ class might have a steep learning curve, but it can do wonders for your career once you learn this language. Having a hold of this language will also set you apart from the competition. It is no doubt this programming language will be vital in the future because of its wide market share.