C++ Classes and Objects

Class – Class is a user-defined datatype having data member and member functions. These data members and member functions can be accessed by the instance of the class. Class is the building block of object-oriented programming. Class is basically a blueprint of an object.

Let us see the example of the class

Let’s consider the class of Animals where every animal has different names and different characteristics but all of them share common properties like – 4 legs, 1 tail, their breed, their color, etc.

So, Animal is a class, and legs, tails, breed, and color are their properties.

Let’s see some properties of the class

  1. A class is a user-defined data type that has data members and member functions.
  2. Member functions are used to change the variables or access the variables when the data members are private.
  3. A constructor can be created to initialize the variables.
  4. A destructor is created to deinitialize the variables. They are deallocated by default.

Object

An object is an instance of a class. When a class is defined, no memory is allocated while when the object of the class is created i.e it is instantiated memory allocated to that particular object.

How to create a class?

A class is created by using the class keyword with the name of the class followed b the curly braces and a semicolon at the end of the curly brace.

Objects Declaration :

For creating the object of the class, We need to allocate the memory. Here, class is our newly created user-defined datatype, and the object we are creating is a variable.

Syntax:

className ObjectName;
For eg - for above class
Object can be as
Animal Elephant; 
This means  we are instantiating a animal called Elephant of the class.

Accessing data members and member functions:

The data members and member functions of the class can be accessed using the dot(‘.’) operator.

For Eg, to print the color of an Elephant we can access it as 

Elephant.Color();  // This will return the grey color of Elephant.

Now we have some Access Specifiers in C++ of the class – public, private, and protected.

C++ Program to demonstrate the access of data members

C++ Code

#include<bits/stdc++.h>
using namespace std;
//  Animal Class
class Animal
{
    // Access specifiers
    // private:
    // protected:
    public:
 
    // Data members
    string color;
 
    // Member functions - used to access the data member of the class or modify
   // the value of the data members
    void printcolor()
    {
        cout<<"Color of given Animal is :"<<color<<endl;
    }
};
// DRIVER Function
int main()
{
    // Declaring the object of Animal as Elephant
    Animal Elephant;
    
    // assigning the value of data member
    Elephant.color="grey";
 
    // accessing member functions
    Elephant.printcolor();
    return 0;
}

Output:

Color of given Animal is :grey

Member Functions in Classes

Member functions can be defined inside the class and outside the class

For defining member functions outside the class, we need the scope resolution operator(::) along with the class name.

The syntax for defining member functions outside the class –

return_type classname :: Function()
{
}

Let us see the C++ Program to show member functions outside the class

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;
class Animal
{
    public:
    string color;
    // Declaration of Member function should be done inside the class
    void printcolor();
};
// Member function outside the class
void Animal::printcolor()
{
     cout<<"Color of given Animal is :"<<color<<endl;
}
 
// DRIVER Function
int main()
{
    Animal Elephant;
    Elephant.color="grey";
 
    // accessing member functions
    Elephant.printcolor();
    return 0;
}

Output:

Color of given Animal is :grey

NOTE – Member functions of the class are by default inline which is advantageous for reducing the call overhead of the function.

Constructors in C++

A constructor in C++ is a special method that is automatically called when an object of a class is created.

In class-based object-oriented programming, The constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object.

A constructor is a special member function of the class because it does not have any return type. Constructor has the same name as the name of a class.

They are of three types – copy, parameterized, and default constructor.

For their detailed analysis, refer to this article – Constructors in C++

Destructors in C++

Destructor is another member function that is called automatically by the compiler when the scope of the object terminates. It basically represented by the tilt operator(~) with the class name as the function.

C++ Program to demonstrate the destructor in C++

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;
class Animal
{
    public:
    string color;
 
    // Destructor syntax
    ~Animal()
    {
        cout<<"Destructor called for given object"<<endl;
    }
};
int main()
{
    Animal Elephant;
    Elephant.color="grey";
    // Since the Elephant object is created in the main scope
    // so scope of the Elephant object ends at the end of main function
    return 0;
}

Output:

Destructor called for given object

Special thanks to Gurmeet Singh for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article