chevron_left
390 points
19 13 12

 Structure:

structure is a collection of simple variables. The variables in a structure can have different data types such as int, float, string, etc. The data items in a structure are called the members of the structure. Structures are one of the basic building blocks for understanding the concept of objects and classes. In order to use a structure, we need to declare it. The structure declaration serves only as of the blueprint for the creation of variables. The declaration does not set aside any space in the memory or even name any variable. 

Class:

Classes are the basic building blocks of object-oriented programming in C++. A class is an idea, a concept, a model, or a blueprint about the physical existence of something. It defines the structure of an object. It is a collection of data and functions. These data and functions are called members of a class. A class defines what its objects will consist of and what operations can be performed on those objects. It is also referred to as the user-defined data type. Just like a structure, the members of a class do not occupy any space in the memory unless its object is created.  

We will take a Human as the example of a class. All human beings are the objects of this Human class. The Human class has attributes such as eyes, nose, mouth, ears, hands, legs, and most importantly a brain. Now, each of these attributes has some function to perform, like eyes are used to see things, ears have the function of hearing, etc. So, these attributes and methods combinedly make a Human class. But this class has no physical existence unless its objects are created. 

Although, the concept of classes and structures is quite similar. They both have the same properties, they both can be inherited and all but there are some differences in both of them, which we are going to discuss now. 

Difference Between Class And Structure:

ClassStructure
1. Classes portray a way to group data and functions.Structures are mostly portrayed as a way to group data.
2. Member data and functions are private by default i.e. cannot be accessed from outside the class.Data is public by default i.e. can be accessed directly throughout the program.
3. In a class, the data is private by default so specifying the private keyword is not necessary.If you want to make the data private, you must specify the private keyword.
4. A class is secure i.e. it hides the implementation complexity from the user.A structure is not secure i.e. it does not hide the implementation complexity from the user.
5. A class does private inheritance if no access specifier is used while declaring a child class.A structure does public inheritance if no access specifier is specified.
6. The syntax for declaring a class is:
class class_name
{
   Access-specifier:
      class_data;
      class_functions() {}
};

 

The syntax for declaring a structure is:
struct structure_name {
data_type variable_name;
.
.
};

 

Now, we will see the examples of classes and structures:

Class Example:

#include<iostream>
#include<string.h>
using namespace std;

class Car
{
   private:     //member data
        string car_name;
        int car_model;
        string car_color;
        int acceleration;
    public:    //member functions
        Car(string name, int model, string color)
        {
            car_name = name;
            car_model = model;
            car_color = color;
            acceleration = 0;
        }
        
        void car_info()
        {
            cout<<"Car: "<<car_name<<endl<<"Model: "<<car_model<<endl<<"Color: "<<car_color<<endl;
        }
        
        void accelerator()
        {
            acceleration += 5;
            cout<<"Accelerating +5 : "<<acceleration<<endl;
        }
        
        void brake()
        {
            acceleration -= 4;
            cout<<"Decelerating -4 : "<<acceleration<<endl;
        }
};

    int main()
    {
        Car car1("Mercedes",2020,"Black");
        car1.car_info();
        car1.accelerator();
        car1.accelerator();
        car1.accelerator();
        car1.brake();
        return 0;
    }

Output:

Car: Mercedes                                                                                                                 
Model: 2020                                                                                                                   
Color: Black                                                                                                                  
Accelerating +5 : 5                                                                                                           
Accelerating +5 : 10                                                                                                          
Accelerating +5 : 15                                                                                                          
Decelerating -4 : 11 

Structure Example:

#include<iostream>
#include<string.h>
using namespace std;

struct Student  //structure declaration
{
   int student_id;
   string student_name;
   int student_age;
};

    int main()
    {
        Student student;  //creating structure object
        student.student_id = 3;   //initializing structure variables
        student.student_name = "XYZ";
        student.student_age = 4;
        
        cout<<"Student Information:\nId: "<<student.student_id<<"\nName: "<<student.student_name<<"\nAge: "<<student.student_age;
        return 0;
    }

Output:

Student Information:                                                                                                            
Id: 3                                                                                                                           
Name: XYZ                                                                                                                       
Age: 4

More Posts

A structure __ contain members of the same data types. Lalit Kumar - Sep 21, 2020
What is the difference between a class and an interface mhfaisal - Jun 12
What is the difference between id and class in css Sanjana Sagar - May 22
What is the difference between Class and ID in html ? Tushar Shuvro - May 1, 2020
the name of the structure is referred to as its: C++ Lalit Kumar - Sep 9, 2020
What is the difference between classes and ids in CSS? Hafsa_Rao - Jul 9
Difference between base class and derived class in c++ Jeevani - Jun 5
Difference between class method and static method in python Tania - Aug 9
Difference between abstract class and interface in php IvanLomakin - Aug 4
Difference between inner class and nested class Sanjana Sagar - May 29