4. 1. INTRODUCTION:
Abstract Methods:
• The Functions that are declared in a class
but not implemented in that class are
called abstract methods .
• They Serves as placeholder for methods
that must be implemented by subclasses.
• These methods are typically declared in
abstract classes or interfaces.(Abstract
methods are achieved by declaring
virtual functions without providing any
implementation) .
• They are useful for defining a common
interface that implemented in their own
way.
5. CONCRETE CLASS:
A concrete class is a class that provides
implementations for all its member functions,
including inherited abstract methods.
It can be instantiated to create objects. Concrete
classes can also have their own non-abstract
methods and member variables.
They serve as building blocks for creating
objects with specific behavior and attributes
6. PURE VIRTUAL
FUNCTION:
“Pure virtual function is a virtual function with no body
.”
A pure virtual function is a special type of virtual
function in C++ that is declared in a base class but has
no implementation. It is marked with = 0 at the end of its
declaration.
Classes containing pure virtual functions are abstract
classes, meaning they cannot be instantiated directly.
Instead, they serve as interfaces or blueprints for
derived classes to provide implementations for the pure
virtual functions.
Subclasses must override all pure virtual functions to
become concrete classes and be instantiated. Pure
virtual functions are a key feature of achieving
polymorphism and defining abstract interfaces in C++. 6
7. ABSTRACT
CLASSES:
• “The classes that contain pure virtual
functions are called abstract classes”
• These classes can not be instantiated
directly ; they are meant to be subclassed.
• Abstracts methods are declared but not
implemented in the abstract class itself.
• It serves as blueprints for other classes and
can contain one or more abstract methods.
7
8. SYNTAX
EXAMPLE
: #include <iostream>
// Abstract class
class Shape
{ public:
// Pure virtual function (abstract method)
virtual void draw() = 0;
};
int main() {
// Attempting to create an object of the abstract
class will result in a compilation error // Shape
shape;
return 0;}
.
8
9. EXAMPLE:
#include <iostream>// Abstract class
using namespace std;
class Shape {
public: // Abstract method
virtual void draw() = 0; };
// Concrete subclass implementing the abstract method
class Circle :
public Shape {public:
void draw() override {
cout << "Drawing Circlen"; }}
;// Concrete subclass implementing the abstract method
class Rectangle :
public Shape {public:
void draw() override {
cout << "Drawing Rectanglen"; }}; 9
10. EXAMPLE:
int main()
{ // Create objects of concrete subclasses
Circle circle;
Rectangle rectangle; // Call methods
circle.draw();
rectangle.draw();
return 0;
}
10
output:
Drawing Circle
Drawing Rectangle
11. INTERFACES:
• “An interface is a blueprint that defines a set of
methods and properties that a class must implement
,without providing the actual implementation”.
• interfaces are typically defined using abstract classes
with pure virtual functions.
• An abstract class is a class that cannot be instantiated
on its own and contains one or more pure virtual
functions, which are declared using the virtual keyword
followed by = 0; syntax.
11
13. RULE OF INTERFACES:
13
Interfaces in OOP define what classes can do
without providing any implementation details,
while abstract classes can provide both
method declarations and some
implementations but cannot be instantiated
directly.
14. ABSTRACT CLASS
a)Abstract class contain both
virtual methods and concrete
methods(with
implementations)
b)These cannot be instantiated
directly.
c) Abstract Classes Support
Single inheritance.
d)Abstract classes are used
when a base class needs to
provide some default
behavior along with abstract
methods that subclasses
must implement.
a) Interfaces contain only pure
virtual methods (method without
implementations).
b) These also cannot be
instantiated at all.
c) Interfaces support multiple
inheritance
d) Interfaces are used when
defining a common behavior of
multiple functions or methods.
INTERFACES
15. REAL WORLD EXAMPLE:
• .
Virtual void getName()=0;
Void getName()
{
}
Void getName()
{
}
Class Manager Class Accountant Class Costumer
Virtual void
getName()=0;
Class Database
16. ADVANTAGES IN OOP:
1. Defining a Common Interface: Abstract classes set a
blueprint for subclasses, ensuring they share common
methods.
2. Organizing Code: They help group related classes
under a common umbrella, improving code structure.
3. Extending Functionality: New features can be easily
added by creating new subclasses of abstract classes.
4. Enhancing Maintainability: Abstract classes make
code easier to understand and maintain by
encapsulating related behavior. 16