Difference between Class and Structure

Difference between Class and Structure

3 mins read4.7K Views Comment
clickHere
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Mar 6, 2023 15:32 IST

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.

2022_09_MicrosoftTeams-image-44.jpg

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

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;
};
Copy code

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;
};
Copy code
Understanding OOPs Concepts in Java
Understanding OOPs Concepts in Java
Object oriented programming (OOP) concepts are the fundamental pillars of programming. The article below explains the OOPs concept in Java. It covers introductions to OOPs, class, objects, inheritance, abstraction, encapsulation,...read more
Object-Oriented Programming with Python
Object-Oriented Programming with Python
A good understanding of OOPs concepts can help you make your Python programming journey smoother. In this article, we will explore the basics of object-oriented programming in Python.
All About OOPs Concepts in C++
All About OOPs Concepts in C++
Have you ever wondered how complex software systems are so efficiently organized and managed? This is where the principles of Object-Oriented Programming (OOP) in C++ come into play. OOP in...read more

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;
};
Copy code

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];
};
Copy code

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!!

About the Author
author-image
Vikram Singh
Assistant Manager - Content

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

Comments