Posts C/C++ Tricky Programs (Set 3)
Post
Cancel

C/C++ Tricky Programs (Set 3)


  • What is the purpose of the Extern Storage Specifier?

    “Extern” specifier is used to resolve the scope of a global symbol.
1
2
3
4
5
6
7
8
#include <iostream >
 using namespace std;
 main()
 {
extern int i;
 cout<<i<<endl;
 }
 int i=20;

Output

1
20

  • Explain Register Storage Specifier.

    “Register” variable should be used whenever the variable is used. When a variable is declared with a “register” specifier, then the compiler gives CPU register for its storage to speed up the lookup of the variable.
1
2
3
4
5
6
void registerStorageClass() 
{ 
    // declaring a register variable 
    register char b = 'G';

} 

  • What is Namespace?

    Namespace allows us to group a set of global classes, objects and/or functions under a specific name.
    The general form to use namespaces is:
    namespace identifier { namespace-body }
    Where identifier is any valid identifier and the namespace-body is the set of classes, objects, and functions that are included within the namespace. Namespaces are especially useful in the case where there is a possibility for more than one object to have the same name, resulting in name clashes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Here we can see that more than one variables 
// are being used without reporting any error. 
// That is because they are declared in the 
// different namespaces and scopes. 
#include <iostream> 
using namespace std; 
  
// Variable created inside namespace 
namespace first 
{ 
    int val = 500; 
} 
  
// Global variable 
int val = 100; 
  
int main() 
{ 
    // Local variable 
    int val = 200; 
  
    // These variables can be accessed from 
    // outside the namespace using the scope 
    // operator :: 
    cout << first::val << '\n'; 
  
    return 0; 
} 

Output

1
500

Very Intersting topic, do explore further

To explore something more interesting follow me.

Now you are in postion to explore the importance of using namespace std;

1
2
3
4
So, What is the use of ‘using' declaration?

Using Declaration is used to refer a name from the 
namespace without the scope resolution operator.

  • What is Name Mangling?

    C++ compiler encodes the parameter types with function/method into a unique name. This process is called name mangling. The inverse process is called as demangling.
1
2
3
4
5
A::b(int, long) const is mangled as ‘b__C3Ail'.

For a constructor, the method name is left out.

That is A:: A(int, long) const is mangled as ‘C3Ail’.

  • Ways in which constructors are called.

    Implicitly: Constructors are implicitly called by the compiler when an object of the class is created. This creates an object on a Stack.
    Explicit Calling: When the object of a class is created using new, constructors are called explicitly. This usually creates an object on a Heap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A{
 int x; int y; 
//default (no argument) constructor
 public A() : x(0) , y(0) {} 
 };
 main()
 {
// Implicit Constructor call. 
//In order to allocate memory on stack.
//the default constructor is implicitly called.
	A Myobj; 

// Explicit Constructor call. In order to allocate
//memory on HEAP we call the default constructor.
A * pPoint = new A(); 
 }


  • What is a COPY CONSTRUCTOR and when is it called?

    A copy constructor is a constructor that accepts an object of the same class as its parameter and copies its data members to the object on the left part of the assignment. It is useful when we need to construct a new object of the same class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class A{
             int x; int y;
             public int color;
			//default (no argument) constructor
             public A() : x(0) , y(0) {} 
             public A( const A& ) ;
};
A::A( const A & p )
{
              this->x = p.x;
              this->y = p.y;
              this->color = p.color;
}
main()
{
            A Myobj;
            Myobj.color = 345;
			// now Anotherobj has color = 345
            A Anotherobj = A( Myobj ); 
}

Also, do explore “Use of Explicit Keyword in C++”


  • Role of the Static keyword for a class member variable.

    The static member variable shares a common memory across all the objects created for the respective class. We need not refer to the static member variable using an object. However, it can be accessed using the class name itself.

  • Static Member Function.

    A static member function can access only the static member variable of the class. Same as the static member variables, a static member function can also be accessed using the class name.

  • C++ polymorphism.

    Do explore this bonous section.
1
2
3
4
5
1. Compile time Polymorphism
		a. Method Overriding
		b. Operator Overriding
2. Runtime Polymorphism
		a. Virtual Functions
Polymorphism
More on Operator Overriding
When should we write our own assignment operator in C++?
Why can’t . (dot), ::, ?: and sizeof be overloaded?

  • What is the difference between a Copy Constructor and an Overloaded Assignment Operator?

    A copy constructor and an overloaded assignment operator basically serve the same purpose i.e. assigning the content of one object to another. But still, there is a difference between the two.
1
2
3
complex c1,c2;
c1=c2; //this is assignment
complex c3=c2; //copy constructor

In the above example, the second statement c1 = c2 is an overloaded assignment statement.
Here, both c1 and c2 are already existing objects and the contents of c2 are assigned to the object c1. Hence, for overloaded assignment statement both the objects need to be created already.
Next statement, complex c3 = c2 is an example of the copy constructor. Here, the contents of c2 are assigned to a new object c3, which means the copy constructor creates a new object every time when it executes.


Source

GeeksforGeeks
Quora
Software Testing Help
Tutorialspoint
This post is licensed under Ravi Jain by the author.