Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code
  2. Structure

Struct vs Class

tldr. both are almost similar with default visibility of data and functions being public in Struct and private in Class.

key differences:

  1. Default Access Specifier:

    • In a class, members are private by default.

    • In a struct, members are public by default.

  2. Inheritance:

    • For a class, the default inheritance is private.

    • For a struct, the default inheritance is public.

  3. Purpose and Convention:

    • Classes are typically used for objects with both data and methods, often implementing data hiding.

    • Structs are conventionally used for passive objects with public data and few or no methods.

  4. Functionality:

    • Functionally, classes and structs are almost identical in C++.

    • You can add methods, constructors, and use inheritance with both.

Here's a brief example to illustrate these differences:

struct MyStruct {
    int x;  // public by default
    void print() { std::cout << x << std::endl; }
};

class MyClass {
    int x;  // private by default
public:
    void print() { std::cout << x << std::endl; }
};

int main() {
    MyStruct s;
    s.x = 5;  // OK, x is public

    MyClass c;
    // c.x = 5;  // Error, x is private
    
    return 0;
}

In this example:

  • The struct's member x is accessible directly.

  • The class's member x is private and not accessible outside the class.

Inheritance example:

struct BaseStruct {};
struct DerivedStruct : BaseStruct {};  // public inheritance by default

class BaseClass {};
class DerivedClass : BaseClass {};  // private inheritance by default

It's worth noting that in practice, the choice between struct and class often comes down to coding style and convention rather than functionality. Many C++ programmers use structs for simple data structures and classes for more complex objects with behavior.

PreviousStructureNextPublic vs Private inheritance

Last updated 9 months ago

🏗️