Methods in C++

In C++, a method is just a function inside a class. There are different types of methods depending on whether they are implemented or not:

  1. Concrete Method (Normal Method)

    • Fully defined inside the class.
    Copyclass A {
    public:
        void display() { cout << "Hello" << endl; }  // normal method
    };
    
  2. Virtual Method

    • Declared with virtual, can be overridden in derived classes.
    Copyclass A {
    public:
        virtual void show() { cout << "A’s show" << endl; }
    };
    
  3. Pure Virtual Method (Abstract Method)

    • Declared with = 0, has no body in the base class.

    • Must be implemented in the derived class.

    Copyclass A {
    public:
        virtual void draw() = 0;  // pure virtual (abstract method)
    };
    
Updated on