C++ Question on References and Function Overloading

22
Prepared by Mohammed Sikander Technical Lead Cranes Varsity

Transcript of C++ Question on References and Function Overloading

Prepared by Mohammed SikanderTechnical LeadCranes Varsity

This presentation consists of set of questions which on solving and reasoning will help you to understand the concepts clearly.

Topics include

Pointers

References

Function Overloading

Default arguments

Mohammed Sikander 2

void update(int *ptr){

*ptr++;}

int main( ){

int a = 5;update(&a);cout << a << endl;

}

Mohammed Sikander 3

void myswap(int *pa , int *pb){

int *temp = pa;pa = pb;pb = temp;printf("MySwap *pa = %d *pb = %d \n",*pa , *pb);

}

int main( ){

int a = 5 , b = 10;myswap(&a , &b);printf("Main a = %d b = %d \n",a , b);

}

Mohammed Sikander 4

Mohammed Sikander 5

Write the Outputvoid fun(int *ptr){

int b = 10;ptr = &b;printf(“Fun %d \n”,*ptr);

}int main(){

int a = 5;int *ptr = &a;fun(ptr);printf(“Main %d \n“ , *ptr);

}

Mohammed Sikander 6

Write the Outputvoid fun(int ref){

ref++;cout<<"Fun ref= "<<ref<<endl;

}int main(){

int a = 5;int &ref = a;fun(ref);cout<<"Main ref= "<<ref<<endl;

}

int main() {

int a = 4;const int b = 3;

1. int *ptr1 = &a;2. int *ptr2 = &b;3. int * const ptr3 = &b;4. const int * ptr4 = &b;5. int * const ptr5 = &a;6. const int * ptr6 = &a;

return 0;}

Mohammed Sikander 7

int main() {

int a = 4;const int b = 3;

1. int &ref1 = a;2. int &ref2 = b;3. const int &ref3 = a;4. const int &ref4 = b;

return 0;}

Mohammed Sikander 8

int main( ){

int a = 5 , b = 10 ;int c = a + b;int *ptr1 = &a + b;int *ptr2 = &(a + b);

}

Mohammed Sikander 9

int main( ){

int a = 5 , b = 10 ;int &r1 = a + b;int &r2 = 5;}

Identify the valid and invalid statements.void func(int &a){}int main( ){

int a = 5, b = 10;int c;

1. func(a);2. func(c);3. func(a + b);4. func(5);}

Mohammed Sikander 10

Identify the valid and invalid statements.void func(const int &a){}int main( ){

int a = 5, b = 10;int c;func(a); //statement 1func(c); //statement 2func(a + b); //statement 3func(5); //statement 4

}

Mohammed Sikander 11

Can we overload these two functionsvoid func(int *p){}void func(int p){}int main(){}

Mohammed Sikander 12

Can we overload these two functionsvoid func(int &a){}void func(int a){}int main(){}

Mohammed Sikander 13

Can we overload these two functionsvoid func(int &a){}void func(const int &a){}int main(){}

Mohammed Sikander 14

Can we overload these two functionschar func( ){

return 'a';}double func(){

return 10.5;}int main(){

char c = func();double d = func();

}

Mohammed Sikander 15

What is the outputvoid func(char a){

cout <<"Character " << a << endl;}int main(){

func('A');func('A' + ' '); //A + space

}

Mohammed Sikander 16

void func(char a){

cout <<"Character " << a << endl;}void func(int a){

cout <<"Integer " << a << endl;}int main(){

func('A'); //Statement 1func('A' + ' '); //Statement 2

}

Mohammed Sikander 17

int add(int a = 5 , int b = 10);

int main( ){

cout << add( 2 , 6) << endl;

cout << add( 2 ) << endl;

cout << add( ) << endl;}

int add(int a = 5 , int b = 10){

return a + b;}

Mohammed Sikander 18

Identify the valid and invalid Declarations

void func1(int a = 5 , int b = 10 ,int c = 20);void func2(int a ,int b ,int c = 20);void func3(int a = 5 ,int b ,int c );void func4(int a = 5 ,int b = 10 ,int c );

Mohammed Sikander 19

void add(int a , int b = 5){

cout << “Add with 2 parameter”;}void add(int x){

cout <<“Add with 1 parameter”;}

int main( ){

add(5);}

Mohammed Sikander 20

What is the header file to be included if you want to use malloc and free?

What is the header file to be included if you want to use new and delete?

Name some memory leakage detection tools?

Mohammed Sikander 21

int main( ){

int *ptr = new int(5);return 0;

}

Mohammed Sikander 22

int *ptr;

void func( )

{

ptr = new int[5];

}

int main( )

{

ptr = new int[5];

func( );

return 0;

}

int main( )

{

int *ptr = new int[5];

return 0;

}