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++.
- A pure virtual function can only be declared, it cannot be defined.
- You cannot assign any value other than 0 to the pure virtual function.
- It is not possible to create an instance of a class. For instance, with reference to the above program, you cannot create an object of Apple type. It would result in a compilation error.
- In case the derived class cannot implement the pure virtual function of the base class, then the derived class acts as an abstract class.
- It is possible to create a pointer with a reference of base abstract class points to the instance of the derived class.
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++
0 of 15 Questions completed Questions: 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:
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) 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; } 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; } 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; } 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; } 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; } 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; } 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; } 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; } 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; } 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; } How are c++ interfaces implemented? How is an abstract class made? How to define a pure virtual function? Which among the following is not the function of abstract class What is a concrete class?
Quiz Summary
Information
Results
Results
0 Essay(s) Pending (Possible Point(s): 0)
Categories
1. Question
2. Question
3. Question
4. Question
5. Question
6. Question
7. Question
8. Question
9. Question
10. Question
11. Question
12. Question
13. Question
14. Question
15. Question
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.