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

Structure

Collection of different data types

  • Basic Definition: A struct is defined using the struct keyword, followed by the struct name and a block containing member variables.

    struct Point {
        int x;
        int y;
    };
  • Creating Instances: You can create instances of a struct like this:

    Point p1;
    p1.x = 10;
    p1.y = 20;
    
    // Or using an initializer list (C++11 and later)
    Point p2 = {30, 40};
  • Member Access: Access struct members using the dot (.) operator:

    std::cout << "p1: (" << p1.x << ", " << p1.y << ")" << std::endl;
  • Functions in Structs: Structs can also contain functions (methods):

    struct Rectangle {
        int width;
        int height;
    
        int area() {
            return width * height;
        }
    };
  • Constructors: You can define constructors for initialization:

    struct Person {
        std::string name;
        int age;
    
        Person(std::string n, int a) : name(n), age(a) {}
    };
    
    Person alice("Alice", 30);
  • Default Access Specifier: In a struct, members are public by default (unlike in classes where they're private by default).

  • Nested Structs: You can nest structs within other structs:

    struct Address {
        std::string street;
        std::string city;
    };
    
    struct Employee {
        std::string name;
        Address workAddress;
    };
  • Pointers to Structs: You can create pointers to structs and access members using the arrow (->) operator:

    Point* pPtr = &p1;
    std::cout << pPtr->x << ", " << pPtr->y << std::endl;
  • Struct vs Class: The main difference is the default access specifier. Structs are often used for simple data structures, while classes are used for more complex objects with behaviors.

  • Memory Alignment: Structs can have padding between members for memory alignment. You can use #pragma pack or __attribute__((packed)) to control this.

Example:

#include <iostream>
#include <string>

struct Date {
    int day;
    int month;
    int year;

    Date(int d, int m, int y) : day(d), month(m), year(y) {}

    void display() {
        std::cout << day << "/" << month << "/" << year << std::endl;
    }
};

struct Person {
    std::string name;
    Date birthDate;

    Person(std::string n, Date bd) : name(n), birthDate(bd) {}

    void introduce() {
        std::cout << "Hi, I'm " << name << ". I was born on ";
        birthDate.display();
    }
};

int main() {
    Date bobBirthDate(15, 6, 1990);
    Person bob("Bob", bobBirthDate);

    bob.introduce();

    return 0;
}
PreviousFor C++ ClassesNextStruct vs Class

Last updated 9 months ago

Run it .

🏗️
here