🟣 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.
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
🟣 Object-Oriented Programming
Example File
abstract-classes.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| abstract class | Abstract Classes and Pure Virtual Functions programming में Abstract class use होता है। |
| pure virtual function | Abstract Classes and Pure Virtual Functions programming में Pure virtual function use होता है। |
| interface | Abstract Classes and Pure Virtual Functions programming में Interface use होता है। |
| override | Abstract 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
- Create abstract class Payment.
- 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.
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
🟣 Object-Oriented Programming
Example File
abstract-classes.cppमुख्य फोकस
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
इसे क्यों सीखें?
- यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
- यह school practicals, projects और competitive programming में बार-बार use होता है।
- यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।
ज़रूरी Terms
| Term | अर्थ / उपयोग |
|---|---|
| abstract class | Abstract class Abstract Classes and Pure Virtual Functions programming में use होता है। |
| pure virtual function | Pure virtual function Abstract Classes and Pure Virtual Functions programming में use होता है। |
| interface | Interface Abstract Classes and Pure Virtual Functions programming में use होता है। |
| override | Override 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)
- Create abstract class Payment.
- Implement two derived classes.
सारांश
Abstract Classes and Pure Virtual Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
💻 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ें.