> For the complete documentation index, see [llms.txt](https://notes.tejpratapsingh.com/_/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.tejpratapsingh.com/_/cpp/code/structure/struct-vs-class.md).

# 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:

```cpp
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:

```cpp
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notes.tejpratapsingh.com/_/cpp/code/structure/struct-vs-class.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
