Difference between Struct and Class in C+

Hey guys,
Today I will be discussing the difference between struct and class in C++.
So read on…

Before I get down to the difference, lets us see what a struct and class exactly is.

What is a Struct?

Struct is shorthand for structure. It is a user-defined data type that can have many different variables of different data types in it.

It allocates space for these variables which are the sum of all the sizes of all these variables.

Since each variable is getting its own space, we can call these variables simultaneously.
We cannot initialize variables in a struct, only declare them.
The keyword “struct” is used for defining a structure.

Syntax:
struct <structure_name>
{
<data_type> <variable>;
<data_type> <variable>;
<data_type> <variable>;
};

We call it by defining a variable to the struct in the main function. Its syntax is:
<struct_name> <struct_variable>;

We can access the variables defined in the struct by the following syntax:
<struct_variable>.<variable>;

Example:

#include<iostream>
using namespace std;
struct divide
{
   int a;
   float b,quo;
};

int main()
{
   divide d;
   d.a=10;
   d.b=5;
   d.quo=d.a/d.b;
   cout<<d.quo;
}

Hence the output 2.

What is a Class?

Class is a blueprint for an object
It is a user-defined data type that can have different variables and methods in it.
It requires an access specifier i.e. public, private or protected for the variables and methods inside it to be accessed outside.
A class can have many different instances.
The keyword “class” is used to define a class.
Variables can be initialized in a class

Syntax:
class <class_name>
{
<access_specifier>:
<data_type> <variable> = <value>;
<return_type> <function_name>();
};

We call it by defining a variable to the class in the main function. Its syntax is:
<class_name> <instance>;
We can have multiple instances

We can access the variables defined in the struct by the following syntax:
<instance>.<variable>;

Example:

#include<iostream>
using namespace std;
class divide
{
    public:
   int a=10;
   float b=5.0,quo=0.0;
};

int main()
{
   divide d;
   d.quo = d.a/d.b;
   cout<<d.quo;
}

As you can see we have already initialised a,b and quo inside the class.

Difference between Struct and Class:

Also, read: Move all the files from one directory to another with C++

 

Struct and class are mostly the same. The only difference between them is:

  1. The need for access specifier in class.
    We can use a struct with or without an access specifier since the default is “public” i.e. accessible all over the program.
    Whereas in class, the default access specifier is “private” hence the contents are not available outside the class.
  2. Deriving problems.
    If we derive a struct from a class that has the public access specifier, the derived struct is available all over the program since it is public.
    Whereas in a derived class from a class with a public access specifier, the derived class is private.

Hence we have covered the difference between struct and class.
If you have any doubts, feel free to ask it in the comment section

Leave a Reply

Your email address will not be published. Required fields are marked *