Abstract Class
An abstract
class is a class that cannot be instantiated (i.e., you cannot create
objects of it directly).
It is used as a
base class for other classes.
The purposeà provide a common interface and
functionality that can be inherited and implemented by its derived classes.
An abstract class
contains at least one pure virtual function, which is a function that
must be implemented by any derived class.
Features of an
Abstract Class
- Pure
Virtual Functions: These are functions declared in the abstract
class with no implementation. They are declared using the syntax = 0 at
the end of the function declaration.
- Example: virtual void display() = 0;
- Cannot
Be Instantiated:
You cannot create objects of an abstract class directly. However, it can
have pointers or references pointing to its derived class objects.
- Can
Have Data Members: An abstract class can have member variables
(data members) like any other class.
- Can
Have Constructors and Destructors: An abstract class can have
constructors and destructors to initialize and clean up resources for its
derived classes.
- Can
Have Concrete Methods: An abstract class can contain methods with
implementations, which can be used directly or overridden in derived
classes.
Advantages of Abstract Classes
- Encapsulation: Abstract
classes help in hiding implementation details while providing a common
interface for all derived classes.
- Code
Reusability:
Shared functionality in the base class can be reused in derived classes.
- Polymorphism: They enable
polymorphism, where the same function name can be used in different
derived classes, allowing dynamic method calls.
- Improved
Maintainability:
Changes in the common functionality need to be made only in the abstract
class, which automatically applies to all derived classes.
Syntax of Abstract Class in C++
class
AbstractClass {
public:
// Pure virtual function
virtual void pureVirtualFunction() = 0;
// Constructor
AbstractClass() {}
// Concrete function
void concreteFunction() {
cout << "This is a concrete
function." << endl;
}
// Virtual destructor
virtual ~AbstractClass() {}
};
Types of Abstract
Classes
- Abstract
Class with Only Pure Virtual Functions:
- All functions in the abstract class
are pure virtual, and the derived classes must implement all of them.
class Shape {
public:
virtual void draw() = 0;
virtual void area() = 0;
};
- Abstract
Class with Both Pure Virtual and Concrete Functions:
- The abstract class may contain a mix
of pure virtual functions and normal functions with implementations.
class Vehicle {
public:
virtual void start() = 0; // Pure virtual
void stop() {
cout << "Vehicle stopped." << endl; // Concrete
}
};
Drawbacks of
Abstract Classes
- Cannot
Create Instances:
You cannot create objects of an abstract class directly, which might limit
certain use cases.
- Increased
Complexity:
If not used properly, abstract classes can lead to complex and
hard-to-maintain code, especially when there are many derived classes.
- Dependency: Derived
classes are tightly coupled to the abstract class, and any changes in the
base class can have cascading effects on all derived classes.
Example 1: Abstract Class with Pure Virtual Functions
#include
<iostream>
using namespace
std;
class Shape {
public:
// Pure virtual function
virtual void draw() = 0;
virtual void area() = 0;
virtual ~Shape() {} // Virtual destructor
};
class Circle :
public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
void draw() override {
cout << "Drawing a
Circle" << endl;
}
void area() override {
cout << "Area of Circle:
" << 3.14 * radius * radius << endl;
}
};
class Square :
public Shape {
private:
double side;
public:
Square(double s) : side(s) {}
void draw() override {
cout << "Drawing a
Square" << endl;
}
void area() override {
cout << "Area of Square:
" << side * side << endl;
}
};
int main() {
Shape* shape1 = new Circle(5);
Shape* shape2 = new Square(4);
shape1->draw();
shape1->area();
shape2->draw();
shape2->area();
delete shape1;
delete shape2;
return 0;
}
Output:
Drawing a Circle
Area of Circle:
78.5
Drawing a Square
Area of Square: 16
Example 2:
Abstract Class with Both Pure Virtual and Concrete Functions
#include
<iostream>
using namespace
std;
class Vehicle {
public:
virtual void start() = 0; // Pure virtual
function
void stop() {
cout << "Vehicle
stopped." << endl; // Concrete function
}
virtual ~Vehicle() {}
};
class Car : public
Vehicle {
public:
void start() override {
cout << "Car started."
<< endl;
}
};
int main() {
Car myCar;
myCar.start();
myCar.stop();
return 0;
}
Output:
Car started.
Vehicle stopped.
=======================================================================
0 Comments