C++

C++ Keyword

Comprehensive list of C++ keywords organized by which C++ standard introduced them.
This will help you see which keyword came from C++98, C++11, C++14, C++17, C++20, or C++23.

Summary:

  • C++98: Base set of about 63 keywords
  • C++11: Added alignas, alignof, char16_t, char32_t, constexpr, decltype, noexcept, nullptr, static_assert, thread_local
  • C++14: Minor standard, mostly library enhancements
  • C++17: No new keywords
  • C++20: Added char8_t, concept, consteval, constinit, co_await, co_return, co_yield, requires, import, module
  • C++23: No new keywords

Note: Contextual keywords like override, final, import, module only gain special meaning in particular contexts, so they won’t cause errors as variable names outside those contexts.

C++98 (original keywords)

asm       auto       bool       break     case      catch
char      class      const      const_cast continue  default
delete    do         double     dynamic_cast else     enum
explicit  extern     false      float     for       friend
goto      if         inline     int       long      mutable
namespace new        operator   private   protected public
register  reinterpret_cast return     short     signed    sizeof
static    static_cast struct     switch    template  this
throw     true       try        typedef   typeid    typename
union     unsigned   using      virtual   void      volatile
wchar_t   while

C++11 additions

alignas      alignof      char16_t     char32_t     constexpr
decltype     noexcept     nullptr      static_assert thread_local

C++11 contextual keywords

override      final

C++14 additions

(No new primary keywords; C++14 mostly relaxed some C++11 rules.)

C++17 additions

(No new primary keywords. Mostly new features and library additions.)

C++20 additions

char8_t     concept      consteval    constinit    co_await
co_return   co_yield     requires     import       module

C++23 additions

(No new primary keywords in C++23 — mostly library and feature updates.)

Inline

Encapsulation

  • Encapsulation means wrapping data (variables) and methods (functions) into a single unit — typically a class — and restricting direct access to some of the object’s components.
  • The main goals:
    • Hide the implementation details (data hiding)
    • Provide controlled access through public methods (getters/setters)
  • Why use Encapsulation?
    • Protects your data from unauthorized access or invalid modifications
    • Makes your code easier to maintain and understand
    • Reduces dependency between different parts of the program

  • Encapsulation = data + methods in a class
  • Hides implementation details using private
  • Provides a controlled interface using public
  • Protects integrity of your data

Access Specifiers in C++:

AccessMeaning
privateAccessible only inside the class
protectedAccessible inside the class and its subclasses
publicAccessible from outside the class

Here’s a simple example that encapsulates data (balance) inside a BankAccount class:

#include <iostream>
using namespace std;

class BankAccount {
private:
    double balance;  // hidden data

public:
    // Constructor
    BankAccount(double initialBalance) {
        balance = (initialBalance > 0) ? initialBalance : 0;
    }

    // Public method to deposit money
    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    // Public method to withdraw money
    bool withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            return true;
        }
        return false;
    }

    // Getter to check balance
    double getBalance() const {
        return balance;
    }
};

int main() {
    BankAccount account(1000.0);  // balance = 1000
    account.deposit(500);         // balance = 1500
    account.withdraw(200);        // balance = 1300

    cout << "Current Balance: " << account.getBalance() << endl;
    return 0;
}
  • balance is private: cannot be accessed directly outside the class.
  • You use public methods (deposit, withdraw, getBalance) to control access to balance.

Difference between class and object.

Class

  • Class is a blueprint  with entities and Attributes
  • Class is a template of Objects
  • Class is a blueprint  and Object is a mirror image of class

Object

  • Objects – is a an instance for a class

Namespace

If there are two or more functions with the same name defined in different libraries then how will the compiler know which one to refer to? Thus namespace came to picture. A namespace defines a scope and differentiates functions, classes, variables etc. with the same name available in different libraries.

#include <iostream>
#include <string>

namespace Company {
    // Nested namespace
    namespace Project {
        void showVersion() {
            std::cout << "Project version: 2.5.1\n";
        }

        int versionNumber = 251;
    }

    // Another namespace in the same company
    namespace Utils {
        std::string toUpper(const std::string &text) {
            std::string result = text;
            for (char &c : result) {
                c = std::toupper(c); // convert to uppercase
            }
            return result;
        }
    }
}

// Create a namespace alias
namespace CP = Company::Project;

int main() {
    // Fully qualified name
    Company::Project::showVersion();

    // Using alias CP
    std::cout << "Version number: " << CP::versionNumber << "\n";

    // Fully qualified call
    std::string name = "arun";
    std::cout << "Uppercase: " << Company::Utils::toUpper(name) << "\n";

    // Bring one namespace into scope
    using namespace Company::Utils;
    std::cout << "Uppercase again: " << toUpper("hello world") << "\n";

    return 0;
}
Project version: 2.5.1
Version number: 251
Uppercase: ARUN
Uppercase again: HELLO WORLD

Inheritance

Inheritance Type

  • Single – One derived inherits one base class.
  • Multiple – One derived inherits multiple base classes.
  • Multilevel – Derived inherits from a derived class.
  • Hierarchical – Multiple derived classes inherit one base class.
  • Hybrid – A mix of multiple types (often multiple + multilevel).
#include <iostream>
using namespace std;

// Base class
class Animal {
public:
    void eat() { cout << "Animal eats\n"; }
};

// Multilevel 1: Mammal derived from Animal
class Mammal : public Animal {
public:
    void walk() { cout << "Mammal walks\n"; }
};

// Multilevel 2: Bird derived from Animal
class Bird : public Animal {
public:
    void fly() { cout << "Bird flies\n"; }
};

// Hierarchical 1: Dog derived from Mammal
class Dog : public Mammal {
public:
    void bark() { cout << "Dog barks\n"; }
};

// Hierarchical 2: Eagle derived from Bird
class Eagle : public Bird {
public:
    void hunt() { cout << "Eagle hunts\n"; }
};

// Multiple + Hybrid: Bat derived from both Mammal and Bird
class Bat : public Mammal, public Bird {
public:
    void navigate() { cout << "Bat navigates using echolocation\n"; }
};

int main() {
    cout << "=== Dog (Hierarchical + Multilevel) ===\n";
    Dog d;
    d.eat();   // From Animal
    d.walk();  // From Mammal
    d.bark();  // From Dog

    cout << "\n=== Eagle (Hierarchical + Multilevel) ===\n";
    Eagle e;
    e.eat();   // From Animal
    e.fly();   // From Bird
    e.hunt();  // From Eagle

    cout << "\n=== Bat (Hybrid: Multiple + Multilevel) ===\n";
    Bat b;
    b.Mammal::eat(); // Disambiguate: specify path to Animal's eat
    b.walk();        // From Mammal
    b.fly();         // From Bird
    b.navigate();    // From Bat

    return 0;
}

Override Function: Can be override of any function in base class function and execute the child class function

polymorphism

Polymorphism means “many forms.”
In C++, it refers to the ability to treat different types of objects using the same interface (same method call) and have the correct behavior happen at runtime.

  • Enables you to write flexible and reusable code.
  • Makes it easy to add new types without changing existing code.
  • Lets you work with base class pointers or references and execute the correct behavior.

Type

Compile-time (static)Run-time (dynamic)
Function/Operator overloadingVirtual functions & Inheritance
Resolved by compiler at compile time.Resolved by the compiler at runtime.

Compile-time polymorphism:

When the compiler knows which function to call.

Function overloading:

You have multiple functions with the same name but different parameters:

int add(int a, int b)    { return a+b; }
double add(double a, double b) { return a+b; }

Operator overloading:

You define custom behavior for operators (+, ==, etc.):

class Vector {
  int x, y;
public:
  Vector(int x, int y): x(x), y(y) {}
  Vector operator+(const Vector &other) {
    return Vector(x + other.x, y + other.y);
  }
};

Run-time polymorphism:

When the decision happens at run time through inheritance + virtual functions.

#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() { cout << "Drawing a shape\n"; }
};

class Circle : public Shape {
public:
    void draw() override { cout << "Drawing a circle\n"; }
};

class Square : public Shape {
public:
    void draw() override { cout << "Drawing a square\n"; }
};

int main() {
    Shape *s1 = new Circle();
    Shape *s2 = new Square();

    s1->draw(); // "Drawing a circle"
    s2->draw(); // "Drawing a square"

    delete s1;
    delete s2;
    return 0;
}

virtual keyword tells the compiler to use dynamic dispatch, so the correct function is called based on the actual object type at runtime.

<Add Soon>

C++ Example Programs

Here table table you can find many C++ examples.

IP to Hex Convertor

using namespace std; 

// function for reverse hexadecimal number 
void reverse(char* str) 
{ 
	// l for swap with index 2 
	int l = 2; 
	int r = strlen(str) - 2; 

	// swap with in two-2 pair 
	while (l &lt; r) { 
		swap(str&#91;l++], str&#91;r++]); 
		swap(str&#91;l++], str&#91;r]); 
		r = r - 3; 
	} 
} 

// function to conversion and print 
// the hexadecimal value 
void ipToHexa(int addr) 
{ 
	char str&#91;15]; 

	// convert integer to string for reverse 
	sprintf(str, "0x%08x", addr); 

	// reverse for get actual hexadecimal 
	// number without reverse it will 
	// print 0x0100007f for 127.0.0.1 
	reverse(str); 

	// print string 
	cout &lt;&lt; str &lt;&lt; "\n"; 
} 

// Driver code 
int main() 
{ 
	// The inet_addr() function convert string 
	// in to standard IPv4 dotted decimal notation 
	int addr = inet_addr("127.0.0.1"); 

	ipToHexa(addr); 

	return 0; 
}

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off
Index