Other function declaration
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 */ }
};
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_;
};
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"; }
};
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
};
Static Functions
class MyClass {
public:
static int getCount() { return count_; }
static void incrementCount() { count_++; }
private:
static int count_;
};
Inline Functions
inline int square(int x) { return x * x; }
Explicit Constructor
class MyClass {
public:
explicit MyClass(int value) : value_(value) {}
private:
int value_;
};
Default Arguments
void printMessage(std::string message, int count = 1) {
for (int i = 0; i < count; ++i) {
std::cout << message << std::endl;
}
}
Function Overloading
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
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;
}
Run it here.
Last updated