Difference between Class and Structure
Class and Structure support most of the same features. Still, they have some important differences between them, such as classes having the advantage of being reference types and Structures having the advantage of not storing the data in a global heap. In this article, we will discuss the difference between Class and Structure.
Classes and Structure are a way to create customized data types that can be further used to create instances. Both share some common features but have significant differences also. Such as, by default, the members of the class are private while the member of the structures is public. In this article, we will explore the difference between class and Structure.
Must Check: Top Data Structures and Algorithms Online Courses & Certifications
Must Check: Top Object Oriented Programming Online Courses & Certifications
Table of Content
Best-suited Data Structures and Algorithms courses for you
Learn Data Structures and Algorithms with these high-rated online courses
Class vs. Structure
Parameter | Class | Structure |
Definition | Class is a blueprint or a set of instructions to build a specific object type. | It is a user-defined data type combining data items of different types, such as char, int, etc., together. |
Keyword for Declaration | class | struct |
Purpose | Data abstraction and further inheritance. | Grouping of data. |
Nature | Reference type. | Value type. |
Null Values | Can have Null Values. | Can’t have Null values. |
Memory Allocation | Class is allocated in Heap memory. | Structures are stored in Stack memory. |
Constructor and Destructor | By default, the class, doesn’t have a constructor and a destructor, but we can declare a user-defined constructor. | Structure does not have a constructor and a destructor. |
Inheritance and Polymorphism | Class supports inheritance, and polymorphism. | Structures do not support inheritance and polymorphism. |
Must Read: Constructors in C++
Must Read: Constructors in Java
What is a Class?
Definition
Class is a blueprint or set of instructions to build specific types of objects. It consists of a list of data members, and a set of operations are performed on the class.
- It is a user-defined data type that holds its own data members.
- Class is a basic building block of object-oriented programming (OOP) languages.
- Class is a logical abstraction.
- It stores multiple types of data and is also used to store functions.
- To access the class members, you need to create an instance of the class called objects.
Syntax
class ClassName {private: member 1; member 2;public: member 3; member 4; ………. ………. member N;};
Example
Ques: Create a class ‘Bike’, that stores the record such as year of manufacturing as year, model name, and brand name.
Answer:
class Bike { public:int year; string model_name;string brand_name;};
What is a Structure?
Definition
The Structure is a collection of various types of variables, and these variables can be built-in or user-defined datatypes.
- Structure elements are stored at contiguous memory locations.
- A structure can have static data members, access specifiers, and support inheritance.
- Members of the Structure can be passed to a function
- Members in the Structure can be accessed using the dot (.) operator.
Syntax
struct StructureName { member 1; member 2; ………. ………. member N;};
Example
Ques: Create an employee record that has three members’ names, ages, and salaries.
Answer:
struct employee { char name[100]; int age[2]; float salary [10];};
Also Read: Classes and Object in Python
Also Read: Classes and Objects in C++
Key differences and similarities between Structure and Class
- Structure is a value type, while classes are reference types.
- Structure is not inheritable, while classes are.
- Classes use Heap allocation while Structure uses stack allocation.
- Structure must have at least one non-shared variable while class can be empty.
- By default, all structure elements are public, while in-class variables and constants are private, and the others are public.
- Both class and Structure are container type, i.e., they can contain others as a member.
- Both can implement interfaces and have shared constructors, with or without parameters.
Conclusion
In this article, we have briefly discussed Class and Structure, their differences, and examples.
Hope you will like the article.
Keep Learning!!
Keep Sharing!!
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio