🟣 OOP  ·  Lesson 49

Abstract Classes and Pure Virtual Functions

Abstract Classes and Pure Virtual Functions

What is Abstract Classes and Pure Virtual Functions?

Abstract Classes and Pure Virtual Functions
An abstract class contains at least one pure virtual function and cannot be directly instantiated. It defines a common interface for derived classes.
Level
🟣 Object-Oriented Programming
Example File
abstract-classes.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
abstract classAbstract Classes and Pure Virtual Functions programming में Abstract class use होता है।
pure virtual functionAbstract Classes and Pure Virtual Functions programming में Pure virtual function use होता है।
interfaceAbstract Classes and Pure Virtual Functions programming में Interface use होता है।
overrideAbstract Classes and Pure Virtual Functions programming में Override use होता है।

Syntax / Pattern

C++
virtual void show() = 0;

Example Program

main.cpp
#include <iostream>
using namespace std;
class Shape{ public: virtual double area() = 0; };
class Square: public Shape{ public: double area(){ return 25; } };
int main(){ Square s; cout << s.area(); }

Expected Output

25

Program Explanation

  • Shape has pure virtual area().
  • Square must implement area().
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • interfaces
  • framework design
  • common behavior rules

Common Mistakes

  • Trying to create object of abstract class.
  • Forgetting to override pure virtual function.

Practice Tasks

  1. Create abstract class Payment.
  2. Implement two derived classes.

Summary

Abstract Classes and Pure Virtual Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

Abstract Classes and Pure Virtual Functions क्या है?

Abstract Classes and Pure Virtual Functions
An abstract class contains at least one pure virtual function and cannot be directly instantiated. It defines a common interface for derived classes.
Level
🟣 Object-Oriented Programming
Example File
abstract-classes.cpp
मुख्य फोकस
Concept + syntax + practical C++ program

इसे क्यों सीखें?

  • यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
  • यह school practicals, projects और competitive programming में बार-बार use होता है।
  • यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।

ज़रूरी Terms

Termअर्थ / उपयोग
abstract classAbstract class Abstract Classes and Pure Virtual Functions programming में use होता है।
pure virtual functionPure virtual function Abstract Classes and Pure Virtual Functions programming में use होता है।
interfaceInterface Abstract Classes and Pure Virtual Functions programming में use होता है।
overrideOverride Abstract Classes and Pure Virtual Functions programming में use होता है।

Syntax / Pattern

C++
virtual void show() = 0;

Example Program

main.cpp
#include <iostream>
using namespace std;
class Shape{ public: virtual double area() = 0; };
class Square: public Shape{ public: double area(){ return 25; } };
int main(){ Square s; cout << s.area(); }

Expected Output

25

Program Explanation

  • Shape has pure virtual area().
  • Square must implement area().
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • interfaces
  • framework design
  • common behavior rules

आम गलतियाँ (Common Mistakes)

  • Trying to create object of abstract class.
  • Forgetting to override pure virtual function.

अभ्यास (Practice Tasks)

  1. Create abstract class Payment.
  2. Implement two derived classes.

सारांश

Abstract Classes and Pure Virtual Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 Live Code Editor

Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें. Agar load na ho to naye tab mein kholें.