Friday, July 27, 2012

C++ Quiz Programs Part - 1

Q : is the following program valid? Yes/No give the reason.

 #include <iostream>  
 using namespace std;  
 int main()  
 {  
      int ____123 =123;  
      cout <<"Hello World! "<<____123<<"\n";  
      return 0;  
 }  
A : Yes the above program is valid. in C & C++ identifier name can/should start with _ or [a-zA-Z]. It cant contain any space or special characters in it.



Q : Write a program to add two numbers without using assignment operator.

A : Remember when we were kids we used to add numbers one by one counting the fingers. Example 5 + 4 = 5 + 1 = 6 + 1 = 7 + 1 = 8 + 1 = 9 . Simple problem solved :) Add one by one in a loop using ++ operator.
 #include<iostream>  
 using namespace std;  
 int main()  
 {  
      int a,b;  
      cout<<"Enter Two Numbers :";  
      cin >>a>>b;  
      while(b--)++a;  
      cout<<"Result :"<<a<<endl;  
 }  



Q : in C++ can a have Anonymous class?

A : In C++ classes can be anonymous ie., they can be declared without an identifier.
 #include<iostream>  
 using std::cout;  
 class{  
      private:  
           int a;  
      public:  
           void init()  
           {  
                a=10;  
           }  
           void display()  
           {  
                cout<<"i m display() - member function "<<a<<"\n";  
           }  
 }A,B;  
 int main()  
 {  
      A.init();  
      A.display();  
      B.display();  
 }  

Output:
i m display() - member function 10
i m display() - member function 0

Here A and B are the instance of the anonymous class. Member functions of anonymous classes can be called like normal class member functions.

Anonymous classes comes with some restrictions.

  • They can't have constructor or destructor [ofcourse they don't have a name].
  • They can't be passed as a function argument. Even if we pass them as an argument to a function with Ellipses, it will abort at runtime. But the compiler wont complain any error as Ellipses skips type checking.
  • They cannot be returned as return values from functions.
  • No class can inherit from anonymous class, Remember they don't have name



  • Q : Inheritance and Anonymous class is it valid?

    A : Yes. Check the below program
     #include<iostream>  
     #include <stdarg.h>  
     using std::cout;  
     class Base  
     {  
        public:  
            int a;  
            Base():a(7){}  
            void parentDisplay(){  
                   cout<<"Parent Class value of a :"<<a<<"\n";  
              }  
     };  
     class : public Base  
     {  
        public:  
           void childDisplay()  
           {  
              cout<<"Child Class - ";  
              parentDisplay();  
           }  
     } AnonymousInherited;  
     int main()  
     {  
          AnonymousInherited.parentDisplay();  
          AnonymousInherited.childDisplay();  
     }  
    

    Output:
    Parent Class value of a :7
    Child Class - Parent Class value of a :7



    Q : Tell something about anonymous union?

    A : Union can be anonymous. But we should know the restrictions of anonymous unions. They are
  • They can't have constructor and destructor.
  • Anonymous unions can't have member functions.
  • Anonymous unions can't be inherited from any other union.

  • Example:
     #include<iostream>  
     using std::cout;  
     int main()  
     {  
       union Base  
       {  
         int x;  
       };  
          union : Base // Error here  
          {  
               int a;  
               char c;  
               void display(){cout<<" a : "<<a<<"\n";} // Error here  
          };  
          a = 97;  
          cout<<" c : "<<c<<"\n";  
     }  
    



    1 comment:

    1. I owe you a world full of thanks for the post. It was really nice and amazing post.
      ____________________
      Employee Engagement Programs

      ReplyDelete