0% found this document useful (0 votes)
6 views

4 Abstract Class and Interface

The document discusses abstract classes and interfaces in C++. Abstract classes can contain implemented methods and variables while interfaces contain only pure virtual method declarations. The document provides examples of calculating area using abstract classes and interfaces to inherit shape classes for squares and circles.

Uploaded by

Saurav Sarmah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

4 Abstract Class and Interface

The document discusses abstract classes and interfaces in C++. Abstract classes can contain implemented methods and variables while interfaces contain only pure virtual method declarations. The document provides examples of calculating area using abstract classes and interfaces to inherit shape classes for squares and circles.

Uploaded by

Saurav Sarmah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Abstract Classes and Interfaces

Abstract Class
• An abstract class is, conceptually
– A class that cannot be instantiated and is usually implemented as
a class that has one or more pure virtual (abstract) functions

– A virtual method in C++ is


• A non-static member function in the base class which is
redefined in a derived class
• Must be declared by using the keyword virtual

– A pure virtual function is one


• Which must be overridden by any concrete (i.e., non-abstract)
derived class
• Declaration with the syntax " = 0" in the member function's
declaration
Abstract Class
• An abstract class in C++
– Must have at least one pure virtual method
– Can have implemented methods and variables declaration
class AbstractClass
{
private:
int a, b;
public:
virtual void AbstractMemberFunction() = 0; // Pure virtual function makes
// this class Abstract class
virtual void NonAbstractMemberFunction1() // Virtual function
{
.............................
}
Void NonAbstractMemberFunction2()
{
....................
}
};
Example :: Abstract Class
Example :: C++ program to calculate the area of a square and a circle
#include <iostream> // Derived class Square
using namespace std; class Square : public Shape
{
class Shape // Abstract class public:
{ float calculateArea()
protected: {
float dimension; return dimension * dimension;
public: }
void getDimension() };
{
cin >> dimension; // Derived class Circle
} class Circle : public Shape
{
// pure virtual Function public:
virtual float calculateArea() = 0; float calculateArea()
}; {
return 3.14 * dimension * dimension;
}
};
Example :: Abstract Class
Example :: C++ program to calculate the area of a square and a circle
int main()
{
Square square;
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;
}

Output:

Enter the length of the square: 4


Area of square: 16

Enter radius of the circle: 5


Area of circle: 78.5
Interface
• An interface class, in C++
– Has all methods declared as pure virtual methods
– Has a virtual destructor
– Has no variable declaration
– Syntax
class InterfaceClass
{
public:
virtual ~ InterfaceClass();
virtual void method_first() = 0 ;
virtual void method_second() = 0;
...............
...............
...............
};
Example :: Interface
Example :: C++ program to calculate the area of a square and a circle
class Shape // Interface class class Circle : public Shape // Derived class
{ {
public: private:
virtual void getDimension() = 0; float dimension;
virtual float calculateArea() = 0; public:
}; void getDimension()
class Square : public Shape // Derived class {
{ cin >> dimension;
private: }
float dimension; float calculateArea()
public: {
void getDimension() return 3.14 * dimension * dimension;
{ }
cin >> dimension; };
}
float calculateArea()
{
return dimension * dimension;
}
};
Example :: Interface
int main()
{
Shape *s;
Square square;
Circle circle;
s = &square;
cout << "Enter the length of the square: ";
s->getDimension();
cout << "Area of square: " << s->calculateArea() << endl;
s = &circle;
cout << "\nEnter radius of the circle: ";
s->getDimension();
cout << "Area of circle: " << s->calculateArea() << endl;
return 0;
}
Output:
Enter the length of the square: 4
Area of square: 16
Enter radius of the circle: 5 Area of circle: 78.5
Differences
• Interface class does not have any method implementation. It only has
method declarations and the class that implements an interface must
implement all the methods of the interface.

• Interface does not have defined variables. Abstract class can have variable
declaration and method implementation/declarations. Moreover an
abstract class may be inherited without implementing the abstract
methods.

• If the derived class does not implement the pure virtual function of parent
class then the derived class becomes abstract

• An abstract class cannot be instantiated but rather inherited by another


class. Instantiating an abstract class will give compilation error.
Using Abstract Class and Interface
• When the features you are implementing are not related to each
other
– Interface is used
– Every object is kind of separate

• When there is need to create multiple versions of a feature


– Abstract class is used
– There is some relation between the created objects

You might also like