C++ Abstract Classes

October 27, 2015 Posted by WithU Technologies
Pure Virtual Functions
·         Pure virtual functions are simply represented by the blank virtual function in the base class which does not have any body.

·         Virtual member function without definition is called pure virtual function.

And to represent it correctly we need to code it like below:
//Base Class

class Cars {

public:

 virtual void SetPower()= 0;

 }
·         They specify that the derived classes define that function on their own. Thus we replace their definition with 0.

·         Each derived class inheriting from the base class with a pure virtual function must override that function.

Abstract Classes
·         And when we declare a Base class function as Pure Virtual Function, that means the Base class is now called as Abstract Class which is different from the concept of data abstraction.

·         Abstract classes are used to create pointers and take advantage of its polymorphic abilities.

Example:

#include <iostream>
using namespace std;

//Abstract Base Class

class Base {

public:

 virtual void Display()= 0;   //Pure virtual Function

 };

//Derived Class

 class Derived:public Base

 {

 public:

       void Display(){

             cout<<"It override the pure virtual function of base class"<<endl;  //it will be in the Console or output screen

       }

 };

int main() {

  //  Base b;   Generate a Compile Time Error as the base class is an abstract class and thus commented

      Base *b; //instead of creating an object, we use a pointer

      Derived d;   //We can created an object for derived classes though

      b=&d;    //Pointer b is now addressed to derived class d object

      b->Display();  //Then it will call the function using arrow selection operator

      return 0;

}

/*

Output:

It override the pure virtual function of base class
*/ 


We cannot create objects of the base class with a pure virtual function.


· As creating an object under base class will generate a Compile Time Error.

· Base class with Pure virtual function are called abstract classes.

· Abstract class cannot be instantiated, but pointers and references of abstract class type can be created


C++ Virtual Function

October 27, 2015 Posted by WithU Technologies
The member function which is declared within the base class and then redefined by a derived class by using the same function name.

To create a virtual function, we need to precede the base class member function with the virtual keyword:

 //Base Class
class Cars {

public:

 virtual void SetPower(){

 }

 };

·         When the virtual function is inherited from the base class, the derived class redefined the virtual function to suit its own needs.

·         Normally without the virtual functions, the base class pointer points to derived class objects.

·         In this case, when we use base class pointer to call a function which is in both classes, then base class function is invoked rather than the derived class.

·         But if we want to invoke the derived classes using base class pointer, then we declare virtual function in the base class.

Example:

#include <iostream>
using namespace std;

//Base Class

class Cars {

public:

 virtual void SetPower(){

    cout<<"Cars is the Base Class and we can implement this message also"<<endl;

 }

 };

//Derived Class

 class HyundaiCreta:public Cars

 {

 public:

    void SetPower(){  //using the same function name, used as virtual function in base class

          cout<<"Hyundai's SUV is expected to come with a refined 1.6-litre diesel engine making 126bhp and 26.5kgm of torque."<<endl;

    }

 };

 //Derived Class

 class RenaultDuster:public Cars

 {

 public:

    void SetPower(){   //Using the same function name, which is used as virtual function

          cout<<"Renault offers a 108.5bhp, 1.5-litre petrol motor with a torque figure of 25.3kgm."<<endl;

    }

 };





int main() {

   //two objects created

   HyundaiCreta h;

   RenaultDuster r;

   Cars c;



   //Pointer address to the objects created by derived class

   Cars *c1 = &h;

   Cars *c2 = &r;

   Cars *c3 = &c;



   //Arrow selection operator is used to call the redefined function through pointer

   c1->SetPower();

   c2->SetPower();

   c3->SetPower();

   return 0;

}



/*

Output:

Hyundai's SUV is expected to come with a refined 1.6-litre diesel engine making 126bhp and 26.5kgm of torque.

Renault offers a 108.5bhp, 1.5-litre petrol motor with a torque figure of 25.3kgm.

Cars is the Base Class and we can implement this message also



 */

·         Here we redefined the base class function using the virtual function which invoke the derived class functions using base class pointers.

·         All the three functions declared here are with the exact same name which is essential to support virtual functions.

·         Here we get three sentences as output, first two for the derived classes and last one for the base class.

·         if we need to invoke the base class along with the derived classes then we can also do that using virtual functions.  

C++ Polymorphism

October 27, 2015 Posted by WithU Technologies
"Poly" means "many" and "morph" means "form".
·         Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.

·         Polymorphism is briefly described as "one interface, many implementations."

There are two types of polymorphism one is Compile-Time Polymorphism and the other is Run-Time Polymorphism.

Compile Time Polymorphism:
·         Operator Overloading,
·         Function Overloading

Run Time Polymorphism: 

·         Interface methods
·         abstract methods
·         Virtual member functions.
·         Inheritance

Example:

#include <iostream>
using namespace std;

// Base class

class Cars{

protected:                  //accessed by the derived class

 float Displacement;

 float Torque;

 float Power;

   public:

    void setDisplacement(float d){    //function to assign the value of d parameter to Displacement variable

      Displacement = d;

    }

    void setTorque(float t){        //function to assign the value of t parameter to Torque variable

      Torque = t;

    }

    void setPower(float p){        //function to assign the value of p parameter to Power variable

      Power = p;

    }

 };

// Derived class

class HyundaiCreta: public Cars    //HyundaiCreta is Derived Class and Cars is the Base class

{

   public:

    void SetHyundai(){   //All the three protected variable member are accessed and used within a sentence.

      cout<<"Hyundai's SUV is expected to come with a refined "<<Displacement<<"-litre diesel engine making "<<Power<<"bhp and "<<Torque<<"kgm of torque."<<endl;

    }

};

// Derived class

class MarutiS_cross: public Cars    //MarutiS_cross is Derived Class and Cars is the Base class

{

   public:

      void SetMaruti(){

            cout<<"The Maruti Suziki S-Cross will come with a Fiat-sourced "<<Power<<"bhp "<<Displacement<<"-litre, which develops "<<Torque<<"kgm of torque and will come."<<endl;

          }

};

// Derived class

class RenaultDuster: public Cars    //RenaultDuster is Derived Class and Cars is the Base class

{

   public:

      void SetRenault(){

                  cout<<"Renault offers a "<<Power<<"bhp, "<<Displacement<<"-litre petrol motor with a torque figure of "<<Torque<<"kgm."<<endl;

                }

};

int main()   //main function

{

      //Declared three Object for each class

    HyundaiCreta h;    

    MarutiS_cross m;   

    RenaultDuster r;

   

   //Three pointers of type Cars pointing them to the Derived Cars

   Cars *c1 = &h;     

   Cars *c2 = &m;

   Cars *c3 = &r;



   //Arrow selection operator is used

   //Each value is passed to its respective function

   //And then according to the pointer the values are used within their respective cars sentence

   c1->setDisplacement(1.6);

   c1->setTorque(26.5);

   c1->setPower(126);



   c2->setDisplacement(1.6);

   c2->setTorque(32.6);

   c2->setPower(118);



   c3->setDisplacement(1.5);

   c3->setTorque(25.3);

   c3->setPower(108.5);

  

   //To output the sentences the following functions are called by their classes through their respective objects

   h.SetHyundai();

   m.SetMaruti();

   r.SetRenault();



   return 0;    //return void

 }

/* Output:

Hyundai's SUV is expected to come with a refined 1.6-litre diesel engine making 126bhp and 26.5kgm of torque.

The Maruti Suziki S-Cross will come with a Fiat-sourced 118bhp 1.6-litre, which develops 32.6kgm of torque and will come.

Renault offers a 108.5bhp, 1.5-litre petrol motor with a torque figure of 25.3kgm.

 */

·         In the example shown above we created a class of Cars as the Base class and then derived three models of cars from that base class.

·         We initialized three float variables as protected member variables.

·         Those variables are then used by the base class to assign the parameter values to them. So that we use those values which are forwarded from the main function.

·         Then we use those values for each car to create a sentences which will use all three variables.

·          In the main function we declared three object and each object for each class.

·         Then we create three pointers and address them to each car models.

·         Then each pointer is used to set the values. Instead of calling the functions directly on the objects, it is faster and more efficient to use pointers.

·         After that we call each functions for each car models, which will display the sentence along with their respective values.

C++ Derived Constructor and Destructor

October 27, 2015 Posted by WithU Technologies
As we already learn in the exceptions of Inheritance that Constructor and Destructor are not inherited from the base class.

But when the object of the Derived class is being created or deleted, the Constructor and Destructor of Base class can be called from the main function().

Example:

#include <iostream>
using namespace std;

// Base class

class Cars{

   public:

      Cars()   //Base Class Constructor declared and defined

      {

       cout<<"Base Class Constructor Called"<<endl;

      }

      ~Cars()  //Base Class Destructor declared and defined

      {

        cout<<"Base Class Destructor Called"<<endl;

      }

};

// Derived class

class Bolero: public Cars    //Bolero is Derived Class and Cars is the Base class

{

   public:

      Bolero()   //Derived Class Constructor declared and defined

            {

             cout<<"Derived Class Constructor Called"<<endl;

            }

   ~Bolero()    //Derived Class Destructor declared and defined

            {

              cout<<"Derived Class Destructor Called"<<endl;

            }

};

int main()   //main function

{

   Bolero b;  //Derived Class Bolero is called

   return 0;    //return void

 }

/* Output:

Base Class Constructor Called

Derived Class Constructor Called

Derived Class Destructor Called

Base Class Destructor Called

 */

·         We first create the Base class Cars with its own Constructor and Destructor. And then we also do the same for the derived class.

·         After compilation we get the output in particular sequence which we need to understand.

·         As we can see here that the Base Class Constructor is called first and then the derived class Constructor.

·         But after that the derived class destructor is displayed before the Base class Destructor.

·         And secret behind this sequence is that the Derived class needs its base class in order to work. That is why the base class is set up first and deleted last.