Saturday, 20 August 2016

Item 1

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 2

Item 2.1

Item 5.1

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5.2

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5.3

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 2.2

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 2.3

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 3

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 4

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5

Item 5.1

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5.2

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5.3

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 1

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 2

Item 2.1

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 2.2

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 2.3

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 3

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 4

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5

Item 5.1

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5.2

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Item 5.3

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Thursday, 18 August 2016

Wednesday, 17 August 2016

RETURNING FUNCTION POINTER:

Syntax for returning function pointer is as follows

Return_type_of_returning_function (*function_name(returning_function_arguments))(actual function arguments);

For Eg:

int (*myFunc(int))(char)

Here myFunc is a function which returns the function of type int (*)(char). 

Tuesday, 16 August 2016

Wednesday, 10 August 2016

Tuesday, 9 August 2016

# will convert the argument of the macro function in to string literal.
For example,

#define foo(arg) #arg
std::cout<<foo(helloWorld);

During preprocessing foo(helloWorld) will be expanded as “helloWorld” due to the foo macro function.


## is used to merge two literal in macro function.
For example,

#define foo(arg1,arg2) arg1##arg2
std::cout<<foo(hello,ArrayNotFound);


During preprocessing foo(hello,ArrayNotFound)  will be expanded as “helloArrayNotFound” due to the foo macro function.

Monday, 8 August 2016

Sunday, 7 August 2016

There are few differences between class and structure in C++.

1.       When access specifier is not specified manually then all members of the class is private, where as in structure all members are public.

Consider the following example

 
class myClass
{
 int a; // a is private
};

class myStruct
{
 int a; // a is public 
};


2.        During inheritance use of class and struct will make difference in access specifier.
a.       When derived one is specified as class, then public member of the base class is private to the derived class.


   
class myClass
{
 public:
 int a; 
};

class Derived : myClass
{
 int b; 
};

int main()
{
 Derived d;
 d.a=10; // Compilation error, a is private because Derived is mentioned as class
}



b.       When derived one is specified as struct, then public member of the base class is public to the derived class.

class myClass
{
 public:
 int a; 
};

struct Derived : myClass
{
 int b; 
};

int main()
{
 Derived d;
 d.a=10; // a is public because Derived is mentioned as struct
}


3.       Keyword ‘class’ can be used in place of typename for templates, where as struct cannot be used in place of typename.


template <class T>
class myClass
{
 T a;
};

Saturday, 6 August 2016

Friday, 5 August 2016

Thursday, 4 August 2016

Sunday, 31 July 2016

C++ Interview questions

Cracking the C++ interview is not so tough. This page contains the C++ interview questions for each category.
Click on each category and respective sub-category to load the C++ interview questions.



C++ Interview questions on Array


What is the size of the two dimensional array when some integer is added to it?
GUESS THE OUTPUT OF THE PROGRAM?
 

/*
Assume the following size 
int - 4 bytes
pointer 4 bytes
*/

#include<iostream>
int main()
{
 int b[10][10];
  
 std::cout<<sizeof(b)<<":"<<sizeof(b+1)<<std::endl;
  
 return 0;
}


400:4
Array will be decomposing to pointer at many situations. This is one of the situation here. Since we have added one integer value with array, array was decomposed to pointer in the second print, so it is returning the size of pointer instead of size of array.


C++ Interview questions on general C++ operators

Priority of comma operator between equal operator and brackets?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include <iostream>
int main()
{
   int k,m;
   k = 1, 2, 3;
   m = (1, 2, 3);
   std::cout<<k<<m<<std::endl;
   return 0;
}

13
Comma operator executes from left to right and return the right most element.

Consider the below statement
   m = (1, 2, 3);

Since brackets takes higher precedence than equal operator, first (1,2,3) will get executed. Here, it returns the 3, so m is stored with value 3.

So, why it is not happened for the below statement?
   k = 1, 2, 3;

Here equal takes the higher operator precedence than comma operator.

Look here for operator precedence table.

http://en.cppreference.com/w/cpp/language/operator_precedence
Can we place the array variable inside the array subscript operator?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
int main()
{
 int a[]={0,1,2,3,4,5}; 
 std::cout<<a[5]<<5[a]<<std::endl;
 return 0;
}


55
Array subscript operator can be used in any of the following way,
a[5] or 5[a]

Both forms are valid syntax.

How it works?
Array subscript operator internally decomposes in to following form.
Consider a[5] in statement. Internally a[5] will be converted to a+5. Since a is a pointer adding 5 will point to 6th (Remember array starts with zero) element in the array.


Similarly 5[a] will be converted in to 5+a.

Can we use the array subscript operator for char literal without assigning in variable?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
int main()
{
 std::cout<<5["ArrayNotFound"]<<"ArrayNotFound"[5]<<std::endl;
 return 0;
}

NN
Even though it seems to be very new think, concept behind it is very simple.
Here,

std::cout<<5["ArrayNotFound"]<<"ArrayNotFound"[5]<<std::endl;


The above statement is equal to the following one,

char *s=”ArrayNotFound”
std::cout<<5[s]<<s[5]<<std::endl;


Oh. Hope you got it. Instead of using the char pointer variable, directly used the string literal in the statement.



C++ Interview questions on Objects


What is the size of empty class?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>

class ArrayNotFound
{
};

int main()
{
 ArrayNotFound b;
 std::cout<<sizeof(b)<<":"<<sizeof(ArrayNotFound)<<std::endl;
 return 0;
}


1:1
Size of the empty object is 1 even though no variable is declared inside the class. This is because when programmer takes the address of the object it should not return the invalid address. To avoid these concern compilers will allocate dummy memory of implementation defined size to the class with no data members and member functions.


C++ Interview questions on enums


What happens when we use the negative value in the enum value?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>

enum NegativeEnum
{
 MinValue=-5,
 MaxValue, 
};

int main()
{
 std::cout<<"MaxValue: " <<NegativeEnum::MaxValue<<std::endl;
 return 0;
}


-4
Enum in C++ is allowed to initialize with negative values.
Now, what happens to next enum value when we initialize the enum with negative value?
Simple, it will be incremented by one.

Here MinValue value is -5, now next value MaxValue is (-5+1)=-4


C++ Interview questions on Miscellaneous Class Features


Whether public members of base class is accessible when derived one is struct and no access specifier is specified during inheritance?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
class BaseClass
{
 public:
 int a; 
};

struct DerivedClass : BaseClass // Note here struct
{
 int b; 
};

int main()
{
 DerivedClass d;
 d.a=10;
 std::cout<<d.a<<std::endl; 
}


10
When struct is specified while deriving the base class , then all public member of  base class will be public to the derived class.
Refer Difference between class and struct in c++  for more difference between class and struct


C++ Interview questions on inheritance


what is the size of the derived class when it inherits the classes which have common base class?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>


/*
Assume the following size 
int - 4 bytes
pointer 4 bytes
char 1 byte
*/


class Language
{
 char a;
};

class C : public Language
{
  
};

class CPP : public Language
{
 
};

class Program : C,CPP
{
 
};

class SpecialProgram : virtual C, CPP
{
 
};

int main()
{
 std::cout<<sizeof(Program)<<":"<<sizeof(SpecialProgram)<<std::endl;
 return 0;
}

2:8
This is called as diamond problem in OOPS concept.

sizeof(Program) returns 2 because Program inherits C and CPP, which have common base class and it is not informed to the compiler.

How to solve this problem?

By using the virtual inheritance.

Even though we used virtual inheritance why it have returned the size as 8 for SpecialProgram?

Since we used the virtual inheritance it will add one hidden virtual pointer inside the SpecialProgram class. Eventhough pointer size is 4, it returned as 8 because we have one char variable in C’s base class and size will become 5 now. Due to the structure padding size is returned as 8. One char variable in CPP’s base class will be ignored since Language is inherited as part of C’s inheritance


What happens when we miss the virtual keyword for one class in diamond problem?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>


/*
Assume the following size 
int - 4 bytes
pointer 4 bytes
char 1 byte
Structure padding 4 Bytes
*/

class m
{
 int b;
 int c;
 int d;
};

class n: virtual m{};

class o:  m{};

class x: n, o {};


int main()
{
 std::cout<<sizeof(x)<<std::endl;
 return 0;
}

28
During diamond problem, when virtual inheritance is missed for one class then virtual inheritance will not be applied properly. So size is returned as 28.
For class n, because of class m size is 4+4+4+4=16.  (3 int and 1 hidden virtual pointer)
For class o, since virtual keyword is missed, class m size is 4+4+4=12 (3 int)
Ultimately class x size is 16+12=28
When virtual keyword is used in class o then size of x will be 20.

What is size of the empty class which has diamond problem?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>


/*
Assume the following size 
int - 4 bytes
pointer 4 bytes
char 1 byte
*/

class Language{};

class C : public Language{};

class CPP : public Language{};

class Program : C,CPP{};

int main()
{
 std::cout<<sizeof(Program)<<std::endl;
 return 0;
}

2
Usually when class has no members, then size of the class will be 1. Here due to the diamond problem for Program class, size of the Program class is 2(1 byte for C class due to inheriting the Language class and 1 byte for CPP class due to inheriting the Language class). 


C++ Interview questions on Function pointers


What happen when we dereference the function pointer?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
int callme()
{
 std::cout<<"#"<<std::endl;
}
int main()
{
 int (*functionPtr)()=callme;
 (************functionPtr)();
 return 0;
}

#
Dereferencing the function pointer has no effect. It means it remains the same after dereferencing, unlike other pointers will get its value in the specified location.

How to return the function?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>

int* PostInArrayNotFound(int *p)
{
 return p; 
}

int *(*broker(int*))(int* p)
{
 return PostInArrayNotFound;
}

int main()
{
 int i=40; 
 int j=10;
 
 std::cout<< *((*broker(&j))(&i)) << ":" << *broker(&j)(&i) << std::endl;  
 
 return 0;
}


40:40
Program just returns the function  pointer. Further study here FUNCTION POINTERS



C++ Interview questions on General Standard template library (STL)


How capacity of the standard template library is incremented?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
#include<vector>

int main()
{
 std::vector<int> a(10);
 std::cout<<a.size()<<":"<<a.capacity()<<std::endl;
 return 0;
}


10:<Implementation defined value>
Size of the vector will grow based on the implementation defined value.

Standard haven’t said about how much vector size should be incremented when it reaches the maximum capacity. It depends on the implementation, how size has to be incremented



C++ Interview questions on Template utility


Whether objects can be used to compare the two things in is_same template class?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
#include<utility>

template<typename T1,typename T2>
class ArrayNotFound
{
 public:
 
 static void callme()
 {
  int a;
  float b;
  std::cout<<std::is_same<T1,T2>::value<<":"<<std::is_same<a,b>::value<<std::endl;
 }
};

int main()
{
 ArrayNotFound<int,int>::callme();
 
 return 0;
}


Compilation Error

error: type/value mismatch at argument 1 in template parameter list for 'template<class, class> struct std::is_same'
Only data type should be used to compare two things in is_same utility. So, why objects should not be used in the is_same utility template class.  Possible implementation of is_class can be

template<typename T1,typename T2>
class is_same
{
};
Since objects cannot be passed as template parameter, it will give compilation error.



C++ Interview questions on Macro


Difference between # and ## in macro expansion?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
#define expand(a,b) a##b
#define singles(bs) (#bs)

int main()
{ 
 int expand(hello,world)=10;
 int object =11;
 std::cout<<helloworld<<":"<<singles(object)<<std::endl;
 return 0;
}


10:object
# will get expanded as string literal. Here singles(object) will expand as “object”.
## is used to merge two literals in macro.

Refer C++ - STRINGNIFY OPERATION IN MACRO for more information.

Whether stringnification operation will be converted to string literal?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>
#define myFunc(a,b) a##b

int main()
{ 
 std::cout<<myFunc(hello,vinoth); 
 return 0;
}


Compilation error
macro_expansions.cpp:6:20: error: 'hellovinoth' was not declared in this scope
## will merge two literal and it will convert to string. Since it is not converted to string literal it is throwing error like 'hellovinoth' was not declared in scope.

Where as, use of single # will be convert to string literals.

Whether macros used after # will expand further?
GUESS THE OUTPUT OF THE PROGRAM?
 

#include<iostream>

#define inner(a,b) 10
#define outer(a) #a


int main()
{ 
 std::cout<<outer(inner(a,b))<<std::endl;
 return 0;
}

inner(a,b)
Macro which is used after # will not expand further. Unlike functions, outer macro will expand first and inner macro will expand next.

Saturday, 23 July 2016

Today's Program for the "Learn something new" Initiative. Guess the output for the following C++ program? Comment your answer. For answer refer the following link. Strongly it is not recommended to look the answer before guessing the output for program

Answer Link






GUESS THE OUTPUT OF THE PROGRAM
 


#include<iostream>
#include<array>

class Fruit
{
	public:
		virtual void getColour()=0;
};

class Apple : public Fruit
{
	private: 
		virtual void getColour()
		{
			std::cout<<"My Colour is red"<<std::endl;
		}
};

int main()
{
	Fruit *anyFruitPtr=new Apple;
	// NOTE getColour is under private section of Apple
	anyFruitPtr->getColour();
	return 0;
}


My Colour is red
Even though getColour is placed under private section of the Apple class it will be allowed to call when it is a virtual function.

So , why compiler can’t show the compiler error?
Checking the access specifier is part of compilation process and not an run time process.

Just take the statement below,

anyFruitPtr->getColour();

At run time, anyFruitPtr variable can hold any class object which is child of Fruit class.  Since compiler was not aware of actual class in the anyFruitPtr variable, it cannot perform the access specifier validation for the virtual function.

Thursday, 21 July 2016

Today's Program for the "Learn something new" Initiative. Guess the output for the following C++ program? Comment your answer. For answer refer the following link. Strongly it is not recommended to look the answer before guessing the output for program


http://www.arraynotfound.com/2016/07/what-happens-when-virtual-function-is-called-from-base-class-member-function.html