Abstract class in C++





Pure Virtual Function :

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration.

Syntax:


virtual return-type functionName(arg_list)=0;
                    

The class containing pure virtual function is called abstract class. And we can not create object of abstract class.

Code Example:
#include<iostream>
using namespace std;
//Abstract class
class A {
public:
//pure virtual function
virtual void show() = 0;
};
class B : public A{
public:
void show( ){
cout<<"HELLO WORLD"<< endl;
}
};
int main (){
//this will cause error
A ob1; 
A *ob2 = new B();
B ob3;
ob2->show();
ob3.show();
return 0;
}            
OUTPUT:
HELLO WORLD
HELLO WORLD   

Abstract Class :

  1. A class containing one or more pure virtual function is called abstract class.

  2. The virtual function can not be declared static.

  3. Only useful as base class to be inherited into a useable derived class.

  4. Abstract class can only be used as base for another class.

  5. You can not create object of abstract class.
    Example:

    // pure virtual functions make a class abstract
    #include<iostream>
    using namespace std;
    class Test
    {
     int x;
     public:
    //pure virtual function
     virtual void show() = 0;
     int display() { return x; }
    };
     
    int main( )
    {
      Test ob;
      return 0;
    }              
    OUTPUT:
    Compiler Error: cannot declare variable 'ob' to be of abstract
     type 'Test' because the following virtual functions are pure 
    within 'Test': note: 	virtual void Test::show()

  6. You can create pointer variables and references of abstract class.
    Example:

    #include<iostream>
    using namespace std;
    class Base
    {
    public:
     virtual void show() = 0;
    };
     
    class Derived: public Base
    {
    public:
     void show()
      { 
       cout << "Derived class\n"; }
    };
     
    int main()
    {
        Base *bp = new Derived();
        bp->show();
        return 0;
    }    
    OUTPUT:
    Derived class
    

  7. Its derived class's responsibility to override all pure virtual function that comes from base class, otherwise derive class also becomes abstract class.
    Example:

    #include<iostream>
    using namespace std;
    class Base
    {
    public:
     virtual void show() = 0;
    };
     
    class Derived: public Base
    {
    public:
     void display()
      { 
      cout << "Display from Derived class"; 
      }
    };
     
    int main()
    {
        Derived ob;
        return 0;
    }    
    OUTPUT:
    Compiler Error: cannot declare variable 'ob' to be of abstract type 
    'Derived'  because the following virtual functions are pure within
    'Derived': virtual void Base::show() 

  8. Abstract class can have constructor, normal functions and variables along with a pure virtual function.
    Example:

    #include<iostream>
    using namespace std;
    class Base
    {
    private :
    int a;
    public:
     virtual void show() = 0;
    Base(int x)
     {
       a = x;
     }
    void display()
     {
      cout<<"Display from Base class";
      cout<< endl;
     }
    };                 

  9. If class contains only pure virtual function such class is called PURE ABSTRACT CLASS.
    Example:

    #include<iostream>
    using namespace std;
    class Base
    {
    private :
    int a;
    public:
     virtual void show() = 0;
     virtual void display() = 0;
    };                 

Next topic is Upcasting - Downcasting





 






Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved