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

Classes

Classes are a fundamental concept in C++, providing a way to create user-defined types that encapsulate data and functions. They are a key feature of object-oriented programming in C++.

  1. Basic Structure:

    class ClassName {
    public:
        // Public members
    private:
        // Private members
    protected:
        // Protected members
    };
  2. Access Specifiers:

    • public: Accessible from outside the class

    • private: Accessible only within the class (default for classes)

    • protected: Accessible within the class and derived classes

  3. Data Members: Variables declared within the class.

  4. Member Functions: Functions declared within the class that operate on the class data.

  5. Constructors: Special member functions for initializing objects.

  6. Destructors: Special member functions for cleaning up when an object is destroyed.

  7. This Pointer: Implicit pointer to the current object instance.

  8. Static Members: Members shared by all instances of the class.

  9. Const Members: Members that can't modify the object's state.

  10. Friend Functions: Non-member functions given access to private members.

Example:

#include <iostream>
#include <string>

class Person {
private:
    std::string name;
    int age;
    static int count;  // Static member

public:
    // Constructor
    Person(const std::string& n, int a) : name(n), age(a) {
        count++;
    }

    // Destructor
    ~Person() {
        count--;
    }

    // Member function
    void introduce() const {
        std::cout << "Hi, I'm " << name << " and I'm " << age << " years old." << std::endl;
    }

    // Static member function
    static int getCount() {
        return count;
    }

    // Getter (const member function)
    std::string getName() const {
        return name;
    }

    // Setter
    void setAge(int a) {
        age = a;
    }

    // Friend function declaration
    friend void displayAge(const Person& p);
};

// Static member initialization
int Person::count = 0;

// Friend function definition
void displayAge(const Person& p) {
    std::cout << p.name << " is " << p.age << " years old." << std::endl;
}

int main() {
    Person alice("Alice", 30);
    Person bob("Bob", 25);

    alice.introduce();
    bob.introduce();

    std::cout << "Total persons: " << Person::getCount() << std::endl;

    displayAge(alice);  // Friend function call

    return 0;
}
PreviousPublic vs Private inheritanceNextFriend Function

Last updated 9 months ago

Run it .

🏢
here