Monday, 26 April 2021

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;  

 

 

}

Virtual Function in C++ Qus - 2

Consider the situation when we don't use the virtual keyword.

 


class A

{

public:

   

void show()

  {

    

      cout<<"Hello"<<endl;

  }

  

};


class B: public A

{

    public:

   

   void show()

  {

    

      cout<<"India"<<endl;

  }

    

 

};

int main() {

 

    A *a;  

    B b;  

    a = &b;  

   a->show();  

    return 0;  

 

 

}


Output:

Hello


Now, when we  use the virtual keyword.


class A

{

public:

   

 virtual void show()

  {

    

      cout<<"Hello"<<endl;

  }

  

};


class B: public A

{

    public:

   

   void show()

  {

    

      cout<<"India"<<endl;

  }

    

 

};

int main() {

 

    A *a;  

    B b;  

    a = &b;  

   a->show();  

    return 0;  

 

 

}


Output:

India

Virtual Function in C++ Qus - 1

 class A

{

public:

   

 virtual void show()

  {

    

      cout<<"Hello"<<endl;

  }

  

};


class B: public A

{

    public:

   

   void show()

  {

    

      cout<<"India"<<endl;

  }

    

 

};

int main() {

    B *b=new B;

    b->show();

    A *b1=new B;

     b1->show();

 

}



Output:


India

Hello

सत्यनिष्ट बनिए

 इमानदारी अपने आप में एक ब्रांड होती है | आज के समय में 90% इमानदार नहीं है , जिसको जहा  लाभ  के लिए जिसको भी धोखा देने लूटने का मौका मिलता है वो लूटता है |

पर एक बात समझनी चाहिए कोई कितना भी बड़ा नीच, धोकेबाज, बईमान क्यों न हो , वो भी अपना लेन देन किसी इमानदार व्यक्ति के साथ ही करना चाहता है |

अगर आप बेईमान है तो आप हर कदम पर अपने दुश्मन बढाते जाते है, अगर इमानदार है तो दोस्त बनाते है |

बेईमानी शोर्ट टर्म तो फायदा देती है पर लॉन्ग टर्म में हमेशा नुक्सान | इमानदारी शोर्ट टर्म में नुक्सान दे सकती है पर लॉन्ग टर्म में हमेशा लाभ ही देती है |

अगर आप व्यापारी है, तो इमानदारी ही आपकी सबसे बड़ी पूंजी होती है | आपका व्यापर कितना लम्बा चलेगा वो इसी पूंजी पर निर्भर करता है |

सत्यनिष्ट बनिए 

बड़ी विडम्बना है

 हर व्यक्ति चाहता है की उसके पास जो जमीन, प्लाट, प्रॉपर्टी, सोना है उसकी कीमत बढती ही जाए |


किसान चाहता है की उसके उगाये हुए फल सब्जी अनाज की कीमत बढे |


डॉक्टर, वकील  चाहते है की उनकी फीस बढे 


गाडिया बनाने वाले चाहते है गाडियों की कीमत बढे 


कपडे बेचने वाले चाहते है कपडे महगे बिके |


चाहे कोई भी उत्पाद हो, कोई भी सेवा हो , उससे जुड़ा हुआ हर व्यक्ति येही चाहता  है की कीमत बढती रहे |


पर सब महगाई पर रोते भी  है |


येही माया है , सब चाहते है और इसी प्रयास में लगे रहते है उनसे जुडी हुई हर चीज महगी होती रहे और बाकी सब सस्ता |


बड़ी विडम्बना है