Structure vs Class in C++
Structure vs Class
On this page we will discuss about difference between structure and class in C++. In C++, a structure is a user-defined data type that groups together variables of different data types, and a class is a more flexible version of a structure.
Both structures and classes allow you to define and create custom data types that can be used to represent real-world objects or concepts in your code.
Here we will discuss the key difference between structure in C and C++ despite syntactical similarities.
Key differences between Structure and Class in C++
Parameters | Structure | Class | |
---|---|---|---|
1. | Access level | By default, members of a structure have public access. | By default, members of a class have private access. |
2. | Syntax | struct structure_name { data_type member1; data_type member2; … data_type member_n; }; | class class_name { access_specifier: data_type member1; data_type member2; … data_type member_n; access_specifier: return_type member_function1(args); return_type member_function2(args); … }; |
3. | Inheritance | It does not support inheritance. | It supports inheritance. |
4. | Polymorphism | It does not support polymorphism. | It supports polymorphism. |
5. | Default constructor | It does not have default constructor. | It has default constructor if no other constructors are defined. |
6. | Size of object | It is smaller in size compared to classes. | It is generally larger in size compared to structures. |
7. | Member functions | It can’t have member functions. | It can have member functions. |
8. | Virtual functions | It can’t have virtual functions. | It can have virtual functions. |
9. | Semantic difference | It is used to represent a collection of related data. | It is used to represent objects with both data and behaviour. |
Note:
The differences are mainly due to the default access levels and some additional features classes has like Encapsulation and Polymorphism.
You can use both of them as you want, depending on the problem you're solving and the design choices you make.
Example of Structure
Run
#include <iostream> using namespace std; struct Point { int x; int y; }; int main() { Point point1; point1.x = 10; point1.y = 20; cout << "Point: (" << point1.x << ", " << point1.y << ")" << endl; return 0; }
Output:
Point: (10, 20)
Example of Class
Run
#include <iostream> using namespace std; class Point { private: int x_; int y_; public: Point(int x, int y):x_ (x), y_ (y) { } void setX(int x) { x_ = x; } void setY(int y) { y_ = y; } int getX() { return x_; } int getY() { return y_; } }; int main() { Point point1(10, 20); cout << "Point: (" << point1.getX() << ", " << point1.getY() << ")" << endl; point1.setX(5); point1.setY(15); cout << "Point: (" << point1.getX() << ", " << point1.getY() << ")" << endl; return 0; }
Output:
Point: (10, 20) Point: (5, 15)
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment