Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jun 13, 2024
Difficulty: Easy

Interface in C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In the world of object-oriented programming, interfaces play a crucial role in defining the behavior of classes & objects. An interface in C++ is a collection of pure virtual functions that provides a blueprint for derived classes to follow. It allows for the creation of flexible & modular code by separating the interface from the implementation. 

Interface in C++

In this article, we will explain the concept of interfaces in C++. We will learn about pure virtual functions, abstract classes, the importance of interfaces, rules to follow while using interfaces, with examples for better understanding.

Pure Virtual Functions

A pure virtual function is a virtual function declared in a base class that has no definition. It is denoted by assigning the value 0 to the virtual function. Here's an example:

class Shape {
public:
    virtual void draw() = 0;
};


In this code snippet, the draw() function is declared as a pure virtual function. Any class that inherits from the Shape class must provide an implementation for the draw() function. Pure virtual functions are used to create abstract classes, which cannot be instantiated on their own but serve as a base for other classes.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Abstract Class

An abstract class is a class that contains at least one pure virtual function. It is designed to be used as a base class for other classes, providing a common interface and shared functionality. An abstract class cannot be instantiated directly, but it can be used as a reference or pointer type. Here's an example:

class Shape {
public:
    virtual void draw() = 0;
    virtual double area() = 0;
};
class Circle : public Shape {
public:
    void draw() {
        // Implementation for drawing a circle
    }
    double area() {
        // Implementation for calculating the area of a circle
    }
};


In this example, the Shape class is an abstract class because it contains pure virtual functions (draw() & area()). The Circle class inherits from Shape & provides implementations for the pure virtual functions. By using an abstract class, we can define a common interface that derived classes must adhere to, ensuring a consistent structure & behavior.

Importance of Interfaces

  1. Abstraction: Interfaces allow for the separation of the interface from the implementation. They define a set of methods that a class must implement, without specifying how those methods should be implemented. This abstraction enables modular design & reduces dependencies between classes.
     
  2. Multiple Inheritance: C++ supports multiple inheritance, which means a class can inherit from multiple base classes. However, multiple inheritance can lead to ambiguity & complexity. Interfaces provide a clean way to achieve multiple inheritance without the associated problems. A class can inherit from multiple interfaces, implementing the required methods without the risk of conflicts.
     
  3. Polymorphism: Interfaces facilitate polymorphism, which allows objects of different classes to be treated as objects of a common interface. This enables code reusability & flexibility. By using interfaces, you can write generic code that works with any object that implements the interface, regardless of its specific class.
     
  4. Loose Coupling: Interfaces promote loose coupling between classes. When a class depends on an interface rather than a concrete class, it reduces the dependencies & makes the code more maintainable. Changes to the implementation of a class do not affect the code that uses the interface, as long as the interface remains unchanged.

Rules While Using Interfaces

When working with interfaces in C++, there are certain rules & guidelines to follow:

  1. Pure Virtual Functions: An interface must contain at least one pure virtual function. This ensures that the interface is abstract & cannot be instantiated on its own.
     
  2. No Implementation: Interfaces should not provide any implementation for the functions they declare. The implementation is the responsibility of the derived classes. Interfaces only define the function signatures.
     
  3. No Data Members: Interfaces should not contain any data members. They are intended to define a contract for behavior, not to store data. Data members should be part of the derived classes that implement the interface.
     
  4. No Constructor or Destructor: Interfaces do not have constructors or destructors. They are not responsible for object creation or destruction. The derived classes handle these aspects.
  5. Inheritance: A class can inherit from multiple interfaces using the public inheritance mode. This allows the class to implement the functions declared in the interfaces.
     
  6. Override Functions: When a class inherits from an interface, it must override all the pure virtual functions declared in the interface. Failure to do so will result in the derived class also being abstract.
     
  7. Pointer or Reference: Interfaces are typically used through pointers or references. This allows for runtime polymorphism, where the specific implementation of an interface can be determined dynamically.

Example

class Printable {
public:
    virtual void print() = 0;
};
class Document : public Printable {
public:
    void print() {
        // Implementation for printing a document
    }
};
void printItem(Printable* item) {
    item->print();
}


In this example, the Printable class is an interface with a pure virtual function print(). The Document class inherits from Printable & provides an implementation for the print() function. The printItem() function takes a pointer to Printable, allowing it to work with any object that implements the Printable interface.

Examples

Example 1: Shape Interface

class Shape {
public:
    virtual double area() = 0;
    virtual void draw() = 0;
};
class Circle : public Shape {
private:
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() {
        return 3.14 * radius * radius;
    }
    void draw() {
        // Code to draw a circle
    }
};
class Rectangle : public Shape {
private:
    double width;
    double height;
public:
    Rectangle(double w, double h) : width(w), height(h) {}
    double area() {
        return width * height;
    }
    void draw() {
        // Code to draw a rectangle
    }
};


In this example, we have a Shape interface that declares two pure virtual functions: area() & draw(). The Circle & Rectangle classes inherit from Shape & provide their own implementations for these functions. This allows us to treat Circle & Rectangle objects as Shape objects, enabling polymorphism.

Example 2: Animal Interface

class Animal {
public:
    virtual void makeSound() = 0;
};
class Dog : public Animal {
public:
    void makeSound() {
        cout << "Woof!" << endl;
    }
};
class Cat : public Animal {
public:
    void makeSound() {
        cout << "Meow!" << endl;
    }
};
void animalSound(Animal* animal) {
    animal->makeSound();
}


In this example, we have an Animal interface with a pure virtual function makeSound(). The Dog & Cat classes inherit from Animal & provide their own implementations for the makeSound() function. The animalSound() function takes a pointer to Animal, allowing it to work with any object that implements the Animal interface.

Frequently Asked Questions

Can an interface have a constructor or destructor?

No, interfaces cannot have constructors or destructors. They are not responsible for object creation or destruction.

Can an interface contain data members?

No, interfaces should not contain data members. They are intended to define behavior, not store data.

Can a class inherit from multiple interfaces?

Yes, a class can inherit from multiple interfaces using multiple inheritance. This allows the class to implement multiple interfaces.

Conclusion

In this article, we explained the concept of interfaces in C++. We learned about pure virtual functions, abstract classes, & the importance of interfaces in designing modular & flexible code. We also discussed the rules to follow while using interfaces & saw practical examples which helped us in clearing all the doubts. Interfaces provide a powerful mechanism for defining common behavior, achieving polymorphism, & promoting loose coupling between classes.

You can refer to our guided paths on Code 360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Topics covered
1.
Introduction
2.
Pure Virtual Functions
3.
Abstract Class
4.
Importance of Interfaces
5.
Rules While Using Interfaces
6.
Example
7.
Examples
7.1.
Example 1: Shape Interface
7.2.
Example 2: Animal Interface
8.
Frequently Asked Questions
8.1.
Can an interface have a constructor or destructor?
8.2.
Can an interface contain data members?
8.3.
Can a class inherit from multiple interfaces?
9.
Conclusion