Difference Between struct and class in C++ with Examples

Introduction to struct and class in C++

In C++, both struct and class are user-defined data types used to combine data members and member functions into a single unit. They support important object-oriented programming features such as constructors, member functions, inheritance and access specifiers. The main difference between struct and class in C++ is their default access specifier and default inheritance access.

C++ में struct और class दोनों user-defined data types हैं, जिनका उपयोग data members और member functions को एक unit में combine करने के लिए किया जाता है। दोनों में constructors, member functions, inheritance और access specifiers जैसी सुविधाएँ उपलब्ध हैं। C++ में struct और class के बीच मुख्य अंतर इनके default access specifier और default inheritance access का है।

1. Definition of struct in C++

A struct is a user-defined data type that groups related data members and member functions together. In C++, members of a structure are public by default.

struct एक user-defined data type है, जो संबंधित data members और member functions को एक साथ group करता है। C++ में structure के members default रूप से public होते हैं।

Syntax:

struct Student {
int rollNo;
float marks;
void display() {
cout << rollNo << " " << marks;
}
};

2. Definition of class in C++

A class is a user-defined data type that combines data members and member functions. In C++, members of a class are private by default.

class एक user-defined data type है, जो data members और member functions को combine करता है। C++ में class के members default रूप से private होते हैं।

Syntax:

class Student {
int rollNo;
float marks;
public:
void display() {
cout << rollNo << " " << marks;
}
};

3. Example of struct in C++

In the following program, the data members of the structure are accessed directly because they are public by default.

निम्न program में structure के data members को सीधे access किया गया है क्योंकि वे default रूप से public होते हैं।

#include <iostream>
using namespace std;
struct Student {
int rollNo;
float marks;
};
int main() {
Student s1;
s1.rollNo = 101;
s1.marks = 85.5;
cout << "Roll Number: " << s1.rollNo << endl;
cout << "Marks: " << s1.marks << endl;
return 0;
}

Output:

Roll Number: 101
Marks: 85.5

4. Example of class in C++

In a class, members are private by default. Therefore, public member functions are used to access and modify private data members.

Class में members default रूप से private होते हैं। इसलिए private data members को access और modify करने के लिए public member functions का उपयोग किया जाता है।

#include <iostream>
using namespace std;
class Student {
int rollNo;
float marks;
public:
void setData(int r, float m) {
rollNo = r;
marks = m;
}
void display() {
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};
int main() {
Student s1;
s1.setData(101, 85.5);
s1.display();
return 0;
}

Output:

Roll Number: 101
Marks: 85.5

5. Difference Between struct and class in C++

The following table explains the major differences between struct and class in C++.

निम्न table C++ में struct और class के बीच मुख्य अंतर स्पष्ट करती है।

Basisstructclass
Default AccessMembers are public by default.Members are private by default.
Default InheritanceInheritance is public by default.Inheritance is private by default.
KeywordDeclared using struct.Declared using class.
Data MembersCan contain data members.Can contain data members.
Member FunctionsCan contain member functions.Can contain member functions.
ConstructorsSupports constructors.Supports constructors.
DestructorsSupports destructors.Supports destructors.
InheritanceSupports inheritance.Supports inheritance.
PolymorphismSupports polymorphism.Supports polymorphism.
Object CreationObjects can be created in the same way.Objects can be created in the same way.

6. Default Access Specifier Example

The following example demonstrates the difference between the default access specifiers of struct and class.

निम्न example struct और class के default access specifiers के बीच अंतर दिखाता है।

#include <iostream>
using namespace std;
struct A {
int x = 10;
};
class B {
int y = 20;
};
int main() {
A obj1;
cout << "Struct value: " << obj1.x << endl;
B obj2;
// cout << obj2.y; // Error: y is private
return 0;
}

Output:

Struct value: 10

Explanation:

  • In struct A, the member x is public by default, so it can be accessed directly.

  • In class B, the member y is private by default, so it cannot be accessed directly from main().

व्याख्या:

  • struct A में member x default रूप से public होता है, इसलिए इसे सीधे access किया जा सकता है।

  • class B में member y default रूप से private होता है, इसलिए इसे main() से सीधे access नहीं किया जा सकता।

7. Difference in Default Inheritance

C++ also defines different default inheritance access for structures and classes.

C++ में structures और classes के लिए default inheritance access भी अलग-अलग होता है।

Structure Example:

struct Base {
int x = 10;
};
struct Derived : Base {
};

Here, inheritance is public by default.

यहाँ inheritance default रूप से public होता है।

Class Example:

class Base {
public:
int x = 10;
};
class Derived : Base {
};

Here, inheritance is private by default.

यहाँ inheritance default रूप से private होता है।

8. When to Use struct and class in C++?

Both struct and class can perform almost the same tasks in C++. However, programmers often follow a common convention:

C++ में struct और class लगभग समान कार्य कर सकते हैं। फिर भी programmers सामान्यतः निम्न convention का उपयोग करते हैं:

  • Use struct when you want to group related data with simple public access.

  • जब संबंधित data को सरल public access के साथ group करना हो, तब struct का उपयोग किया जाता है।

  • Use class when you want to implement encapsulation and control access to data through public member functions.

  • जब encapsulation लागू करना हो और public member functions के माध्यम से data access नियंत्रित करना हो, तब class का उपयोग किया जाता है।

In C++, struct and class are almost identical in their capabilities. Both support data members, member functions, constructors, destructors, inheritance and polymorphism. The primary differences are that structure members are public by default, while class members are private by default. Similarly, structure inheritance is public by default, whereas class inheritance is private by default.

C++ में struct और class की capabilities लगभग समान होती हैं। दोनों data members, member functions, constructors, destructors, inheritance और polymorphism को support करते हैं। मुख्य अंतर यह है कि structure के members default रूप से public होते हैं, जबकि class के members default रूप से private होते हैं। इसी प्रकार structure में inheritance default रूप से public और class में private होता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)