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. Classes

Other function declaration

  1. Access Modifiers:

class MyClass {
public:
    void publicFunc() { /* Accessible from anywhere */ }
protected:
    void protectedFunc() { /* Accessible within the class and derived classes */ }
private:
    void privateFunc() { /* Accessible only within the class */ }
};
  1. Const Correctness

class MyClass {
public:
    int getValue() const { return value_; } // Can be called on constant objects
    void setValue(int newValue) { value_ = newValue; } // Cannot be called on constant objects
private:
    int value_;
};
  1. Virtual Functions

class Base {
public:
    virtual void func() { std::cout << "Base::func()\n"; }
};

class Derived : public Base {
public:
    void func() override { std::cout << "Derived::func()\n"; }
};
  1. Override and Final

class Base {
public:
    virtual void func() = 0; // Pure virtual function
};

class Derived : public Base {
public:
    void func() override { /* Implementation */ }
};

class FinalClass : public Base {
public:
    void func() final override { /* Implementation */ } // Prevent further overriding
};
  1. Static Functions

class MyClass {
public:
    static int getCount() { return count_; }
    static void incrementCount() { count_++; }
private:
    static int count_;
};
  1. Inline Functions

inline int square(int x) { return x * x; }
  1. Explicit Constructor

class MyClass {
public:
    explicit MyClass(int value) : value_(value) {}
private:
    int value_;
};
  1. Default Arguments

void printMessage(std::string message, int count = 1) {
    for (int i = 0; i < count; ++i) {
        std::cout << message << std::endl;
    }
}
  1. Function Overloading

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
  1. Function Templates

template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

// the call it using
int main() {
    int x = 10, y = 20;
    double a = 3.14, b = 2.718;
    std::string s1 = "hello", s2 = "world";

    std::cout << max(x, y) << std::endl; // Calls max<int>(x, y)
    std::cout << max(a, b) << std::endl; // Calls max<double>(a, b)
    std::cout << max(s1, s2) << std::endl; // Calls max<std::string>(s1, s2)
    return 0;
}
PreviousPure virtual functionNextconst function vs final function

Last updated 9 months ago

Run it .

🏢
here