Interfaces (Abstract class) in C++ | Characteristics and rules | Example | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Thursday, July 30, 2020

Interfaces (Abstract class) in C++ | Characteristics and rules | Example | My CS Tutorial

Abstract class in C++ | C++ Abstract class rules and characteristics | Example of abstract class in C++ | C++ interfaces | My CS Tutorial

Interfaces in C++ | My CS Tutorial

Abstract class in C++ 


Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

Rules of Abstract Class


1) As we have seen that any class that has a pure virtual function is an abstract class.
2) We cannot create the instance of abstract class. For example: If I have written this line Animal obj; in the above program, it would have caused compilation error.
3) We can create pointer and reference of base abstract class points to the instance of child class. For example, this is valid:

Animal *obj = new Dog();
obj->sound();
4) Abstract class can have constructors.
5) If the derived class does not implement the pure virtual function of parent class then the derived class becomes abstract.

Characteristics of Abstract Class


Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created.
Abstract class can have normal functions and variables along with a pure virtual function.
Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.
Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.

Examples of Abstract Class in C++


Write one program to show the mechanism of the abstract class.
#include <iostream> 
using namespace std;
 class A
 { 
public:
 virtual void test() = 0; //declaring a virtual function
 }; 
class B : public A 
public: void test() 
cout << "Hello I am the virtual function running in derived class!! :)" << endl; 
};
 int main(void) 
B obj; 
obj.test(); 
return 0;
 }

Abstract class in C++ | C++ Abstract class rules and characteristics | Example of abstract class in C++ | C++ interfaces | My CS Tutorial 
_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment