Site icon DataFlair

Interfaces in C++ (Abstract Class) – Explore How Pure Virtual Function Works

Interfaces in C++ (Abstract Class)

Free C++ course with real-time projects Start Now!!

Interfaces in C++ are nothing but a way to describe the behavior of a class without any commitment to any specific implementation of that class. It is the supplement feature offered by C++ classes and objects. Here, we will help you move a step forward to explore the depth of object-oriented programing using classes and objects. It is important to note that there’s no difference between Interfaces and abstract classes in C++.

So let’s not waste time and start our journey for C++ Interfaces.

Interfaces in C++

In programming terminology, a class with a pure virtual function can be termed as an abstract class in C++.

We can implement Interfaces in C++ with the help of abstract classes. Interfaces are closely associated with classes and objects. Therefore, it is safe is to say that the term “Interfaces” and “Abstract Classes” more or less convey the same idea.

Key takeaway: It is important to note that abstract classes and data abstraction in C++ are two different concepts as data abstraction simply separates the essential data from its implementation techniques.

Pure Virtual Functions in C++

As already discussed, an abstract class in C++ is nothing but a class with a pure virtual function. Now, let’s understand what are virtual and pure virtual function.

A virtual function in C++ is nothing but a member function in a class that we declare within the base class and redefine in a derived class.

A pure virtual function in C++ is nothing but a virtual function that we know exists but cannot be implemented. It is simply declared, not implemented.

Its declaration is as follows:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

/* 0 does not indicate that we initialize the function to a null or zero value */

virtual void function() = 0;

Key takeaway: It is important to note that a pure virtual function can only be declared, not defined.

Importance of Interfaces in C++

After developing an understanding of pure virtual functions, let us acknowledge its significance by considering a simple problem at hand. Suppose you have created a class named Apple with data members iPhoneX, iPad_Pro, and iPod with member functions price(), ringtone() and alarm().

Let us consider that the price of each product is fixed and cannot be altered. We know that Apple products have different prices like iPhoneX costs 65,500 and an iPad_Pro costs 48,000.

Implementation of the class Apple for the function price() can be done only in one correct way, that is, by making this function pure abstract so that, we need not give implementation in the class Apple but all the classes that inherit Apple class must give implementation to this function. In this way, we can ensure that all Apple products have a fixed and unique price.

2 Ways to Implement Friend Function in C++ (Including Practical Example)

Here is a program in C++ that would help you solve this above-stated problem:

#include<iostream>
using namespace std;

class Apple
{
public:
// Pure Virtual Function declaration
virtual void price() = 0;

// Member functions
void ringtone() 
{
cout<<"The ringtone is: Reflection"<<endl;
}
};
class iPhoneX: public Apple
{
public:
void price() 
{
cout<<"The price is: 65,500"<<endl;
}
};

int main()
{

cout<<"Welcome to DataFlair tutorials"<<endl<<endl;

iPhoneX i;
i.price();
i.ringtone();
return 0;
}

From the above program, it is pretty clear how we can implement abstract classes in C++ with the help of pure virtual functions.

Output-

Rules Associated with Interfaces in C++

There are certain rules that we need to keep in mind while working with interfaces/ abstract classes in C++.

For instance, with reference to the above program, you can achieve this task by writing the following statements:

Apple *object = new iPhoneX();
object -> price();

Quiz on Interfaces in C++

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    What is the output of following program?

    #include <iostream>

     

    using namespace std;

    class A {

       public:

          int get() {return a*b;}

          void set(int x,int y) {

             a=x;

             b=y;

          }

       

       protected:

          int a;

          int b;

    };

     

    class B: public A {

     

    };

     

    int main(void) {

       B b1;

     

       b1.set(5,3);

       cout <<b1.get()<< endl;

     

       return 0;

    }

     

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    What is the output of following program?

    #include <iostream>

     

    using namespace std;

    class A {

       public:

          virtual int get()=0;

          void set(int x,int y) {

             a=x;

             b=y;

          }

       

       protected:

          int a;

          int b;

    };

     

    class B: public A {

     

    };

     

    int main(void) {

       B b1;

     

       b1.set(5,3);

       cout <<b1.get()<< endl;

     

       return 0;

    }

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    What is the output of following program?

    #include <iostream>

     

    using namespace std;

    class A {

       public:

          virtual int get()=0;

          void set(int x,int y) {

             a=x;

             b=y;

          }

       

       protected:

          int a;

          int b;

    };

     

    class B: public A {

    public:

        int get(){ return a*b;}

    };

     

    int main(void) {

       B b1;

     

       b1.set(2,3);

       cout <<b1.get()<< endl;

     

       return 0;

    }

     

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    What is the output of following program?

    #include <iostream>

     

    using namespace std;

    class A {

       public:

          virtual int get() = 0;

          void set(int x) {

             a=x;

          }

       

          void sett(int y) {

             b=y;

          }

       

       protected:

          int a;

          int b;

    };

     

    class B: public A {

       public:

          int get() { 

             return (a * b); 

          }

    };

     

    int main(void) {

       B b1;

       C c1;

     

       b1.set(5);

       b1.sett(7);

       cout <<b1.get()<< endl;

     

       return 0;

    }

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    What is the output of following program?

    #include <iostream>

     

    using namespace std;

    class A {

       public:

          virtual int get() = 0;

          void set(int x) {

             a=x;

          }

       

          void sett(int y) {

             b=y;

          }

       

       protected:

          int a;

          int b;

    };

     

    class B: public A {

       public:

          int get() { 

             return (a * b); 

          }

    };

     

    class C: public A {

       public:

          int get() { 

             return (a * b)/2; 

          }

    };

     

    int main(void) {

       C c1;

       c1.set(5);

       c1.sett(7);

       cout << c1.get(); 

       return 0;

    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    What is the output of following program?

    #include <iostream>  

    using namespace std;  

     class X    

    {    

        public:   

        virtual void print()=0;    

    };    

     class Y : X    

    {    

        public:  

         void print()    

        {    

            cout <<“A”;    

        }    

    };    

    class Z : X   

    {    

        public:  

         void print()    

        {    

            cout <<“B”;    

        }    

    };    

    int main( ) {  

        Y y;  

        Z z;  

        y.print();    

        z.print();   

       return 0;  

    }  

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    What is the output of following program?

    #include <iostream>  

    using namespace std;  

     class X    

    {    

        public:   

        virtual void print()=0;    

    };    

     class Y : X    

    {    

        public:  

         void print()    

        {    

            cout <<“A”;    

        }    

    };    

    class Z : Y   

    {   

    };    

    int main( ) {  

        Y y;  

        Z z;  

        y.print();    

        z.print();   

       return 0;  

    }

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    What is the output of following program?

    #include<iostream>

    using namespace std;

     

    class B

    {

       int a;

    public:

        virtual void f() = 0;

        int get() { return a; }

    };

     

    class D: public B

    {

        int y;

    public:

        void f() { cout << “D”; }

    };

     

    int main(void)

    {

        D d;

        d.f();

        return 0;

    }  

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    What is the output of following program?

    #include<iostream>

    using namespace std;

     

    class T

    {

       int a;

    public:

        virtual void get() = 0;

        int gett() { return a; }

    };

     

    int main(void)

    {

        T t;

        return 0;

    }

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    What is the output of following program?

    #include<iostream>

    using namespace std;

     

    class B

    {

    public:

        virtual void f() = 0;

    };

     

    class D: public B

    {

    public:

        void f() { cout << “fun”; }

    };

     

    int main(void)

    {

        B *bp = new D();

        bp->f();

        return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    How are c++ interfaces implemented?

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    How is an abstract class made?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    How to define a pure virtual function?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    Which among the following is not the function of abstract class

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    What is a concrete class?

    Correct
    Incorrect

Summary

Congratulations, you have finally completed interfaces in C++. Hope, now you are clear with the difference between interfaces and abstract classes in C++. In addition, we saw how to implement pure virtual functions with the help of classes through a real-world example. Finally, we discussed some of the rules associated with abstract classes leaving no room for any doubts.

If you have any queries, do let us know in the comment section below.

Exit mobile version