Monday 26 April 2021

Virtual Destructor in C++

 #include<iostream.h>

#include<conio.h>

class A

{

public:

A()

{

 cout<<"C-A"<<endl;

}

virtual ~A()

{

cout<<"D-A"<<endl;

}

};

class B:public A

{

public:

B()

{

cout<<"C-B"<<endl;

}

~B()

{

cout<<"D-B"<<endl;

}


};

int main()

{

clrscr();

{

A *a=new B;

/*

A *a;

B b;

a=&b;

*/

delete a;

}

getch();


}

Inherit a class virtually in C++

 #include<iostream.h>

#include<conio.h>

class A

{

public:

 A()

 {

 cout<<"C-A"<<endl;

 }


};

class B

{

public:

B()

{

cout<<"C-B"<<endl;

}


};


class C:  A, virtual B

{

public:

C()

{

cout<<"C-C"<<endl;

}


};



int main()

{

clrscr();

C ob;


getch();

return 0;


}


Output:

C-B

C-A

C-C

Virtual with data members, constructor and destructor in C++

 #include<iostream.h>

#include<conio.h>

class A

{

 public:

 int a;  //Virtual can not be used with data members

 A()      //constructor can not be virtual

 {

 a=10;

 }

virtual ~A(){}     //Desct can be virtual

};

class B : public A

{

 public:

 int a;

 B()

 {

  a=5;

 }


};

int main()

{

clrscr();

B b;

cout<<b.a<<endl;


A *a1=new B();

cout<<a1->a<<endl;

B *b1=new B();

cout<<b1->a<<endl;

getch();

return 0;

}

Virtual Function in C++ Qus - 3

 #include<iostream.h>

#include<conio.h>

class A

{

public:


virtual void show()

{

cout<<"Hello"<<endl;

}

void show1()

{

cout<<"show-1"<<endl;

}

};

class B:public A  //public visibility mode of inhe.

{

public:

  void show()

  {

  cout<<"India"<<endl;

  }

  void display()

  {

  cout<<"Display"<<endl;

  }

};

int main()

{

clrscr();

A *a1=new B;    //Super class or base class pointer can point subclass object

a1->show();


A *a;

B b;

a=&b;

a->show();

//a->display();

a->show1();

B *b1=new B;

b1->show();

b1->show1();

b1->display();

getch();

return 0;


}

Inline Function of a class in C++ Qus: 2

 Inline function and classes in C++:


All the functions defined inside the class are implicitly inline. 



class A

{

public:

    inline int square(int a) 

    {

       return a*a;

    }

};

int main() {

    A ob;

   cout<<ob.square(6);

}



Explicitly Defined:


class A

{

public:

    int square(int a) ;

    

};


inline int A:: square(int a)

{

     return a*a;

}

int main() {

    A ob;

   cout<<ob.square(6);

}

Inline Function in C++ Qus - 1

 C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time. 

inline return-type function-name(parameters)

{

    // function code

}  

Inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining. Compiler may not perform inlining in following conditions:

1) If a function contains a loop. (for, while, do-while)

2) If a function contains static variables.

3) If a function is recursive.

4) If a function return type is other than void, and the return statement doesn’t exist in function body.

5) If a function contains switch or goto statement.

Inline function may increase compile time overhead if someone changes the code inside the inline function then all the calling location has to be recompiled because compiler would require to replace all the code once again to reflect the changes, otherwise it will continue with old functionality.


inline int square(int a)

{

  return a*a;

}

int main() {

  clrscr();

cout<<square(5)<<endl;

cout<<square(6)<<endl;

getch();

}




Referencing Subclass objects with Subclass and Superclass reference in C++ Qus - 1

  class A


{


public:

  

  void show()


  {


      cout<<"Hello"<<endl;


  }


};

class B: public A           //Inheritance Visibility Mode must be public for this qus


{


    public:


     void show()


  {


      cout<<"India"<<endl;


  }


   };


int main() {


    B *b=new B;


    b->show();


    A *b1=new B;


     b1->show();


   //  B *b1=new A;

 //Error -  Derived class pointer object cannot point to a base class object, Derived class pointer cannot point to base class.


}




Output:


India


Hello


Note:

//Super class or base class pointer can point subclass object

//Subclass dynamic or pointer object can not point to the base class object. Subclass pointer can not point to base class


Friend Function in C++ Qus - 2

  class A

{

  private:

  int a;

  void display()

  {

      a=10;

      cout<<"Display"<<endl;

  }

  friend void show();

};


void show()

{

    A ob;

    cout<<ob.a<<endl;

    ob.display();

     cout<<ob.a<<endl;

}

int main() {

   show();

}



Output


Garbage Value

Display

10

Friend Function in C++ Qus - 1

 

class A

{

  private:

  int a;

  A():a(10)

  {

      

  }

  friend void show();

};


void show()

{

    A ob;

    cout<<ob.a;

}

int main() {

   show();

}



Pure Virtual Function in C++

  • A class containing the pure virtual function cannot be used to declare the objects of its own, such classes are known as abstract base classes.
  • Pure virtual function can be defined as:
  • virtual void show() = 0;   

 

#include <iostream>


using namespace std;

class A

{

public:

   

 virtual void show()=0;

 

  

};


class B: public A

{

    public:

   

   void show()

  {

    

      cout<<"India"<<endl;

  }

    

 

};

int main() {

 

    A *a;  

    B b;  

    a = &b;  

   a->show();  

//   A a1;   Error

//A *a2=new A;  Error

    return 0;  

 

 

}