CLASSES vs STRUCTS in C++
Summary
TLDRIn this video, The Cherno explains the difference between structs and classes in C++, focusing on the key distinction: default member visibility. While classes have private members by default, structs have public members. He explores the practical implications of this, sharing his personal preference for using structs for simple data containers and classes for more complex structures that include methods and inheritance. The video also delves into the historical context of structs in C++ and offers insights into when to choose one over the other based on data structure complexity and design needs.
Takeaways
- π A struct and a class in C++ are very similar but differ in one important way: visibility of members.
- π A class has private members by default, while a struct has public members by default.
- π To make members public in a class, you need to explicitly use the 'public' keyword.
- π To make members private in a struct, you need to explicitly use the 'private' keyword.
- π The primary difference between a struct and a class is semantic, not technical. Both can serve the same purpose in C++.
- π Structs were originally introduced for backward compatibility with C, which lacks classes.
- π C++ developers sometimes use structs when dealing with simple data structures (Plain Old Data, POD).
- π Complex data structures, especially those involving inheritance or significant functionality, should use classes.
- π The choice between struct and class often comes down to coding style and the purpose of the data structure.
- π If you need inheritance, always use a class instead of a struct, as structs are not typically designed for complex relationships.
- π The C++ compiler allows using structs and classes interchangeably, but it's important to understand their conceptual distinctions in usage.
Q & A
What is the primary difference between a struct and a class in C++?
-The primary difference between a struct and a class in C++ is the default visibility of members. In a class, members are private by default, while in a struct, members are public by default.
Can you change the default visibility of members in a struct or class?
-Yes, you can change the default visibility by explicitly using access modifiers like 'private' and 'public'. In a struct, you can add 'private' to make members private, and similarly in a class, you can add 'public' to make members public.
Why does C++ have structs in the first place if they seem similar to classes?
-Structs exist in C++ for backward compatibility with C, as C does not have classes but has structures. This ensures that C++ code can still work with C code that uses structs.
What happens if you replace 'struct' with 'class' using a preprocessor directive in C++?
-If you replace 'struct' with 'class' using a preprocessor directive (like #define), the code will treat the struct as a class, making its members private by default. The code will then behave as though a class was used instead of a struct.
When should I use a struct over a class in C++?
-You should use a struct when you need to represent plain old data (POD) or simple data structures that only store variables. Structs are ideal when you don't need the added complexity of private members or inheritance.
When is it appropriate to use a class in C++?
-Use a class when you need to encapsulate complex functionality, such as handling game logic or managing objects with methods and inheritance. Classes are ideal for scenarios where you need to control access to data and manage interactions between different parts of a program.
Can you use inheritance with structs in C++?
-Yes, you can use inheritance with structs, but it is not recommended. Inheritance typically introduces complexity that is better suited for classes rather than structs. Additionally, some compilers may give warnings if you mix classes and structs in an inheritance hierarchy.
What is a good example of when to use a struct?
-A good example of when to use a struct is for a simple data structure like a 2D vector (Vec2) that holds two variables, such as 'x' and 'y'. Since the structure only represents data and doesn't involve complex behavior or inheritance, a struct is a fitting choice.
Can you add methods to a struct in C++?
-Yes, you can add methods to a struct in C++, just like in a class. The only difference is that the members of a struct are public by default, so methods can directly access those members unless otherwise specified.
Is there a performance difference between using a struct and a class in C++?
-No, there is no inherent performance difference between structs and classes in C++. The performance is determined by how you use them, not whether they are structs or classes. The difference lies mainly in visibility defaults and intended usage.
Outlines

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowMindmap

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowKeywords

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowHighlights

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowTranscripts

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowBrowse More Related Video

Pertemuan 7 Pemrograman Terstruktur 2025

Object Oriented Programming Features Part 3 | C ++ Tutorial | Mr. Kishore

Features of Object Oriented Programming Part 2 | C ++ Tutorial | Mr. Kishore

01 - [Classes] C++ Intermediate Programming Tutorial

Mengenal Array dan Struct Dalam Algoritma Struktur Data

Variables in C++
5.0 / 5 (0 votes)