What changes you can do in the header files to avoid the redefinition that compiler will give when both the header files are included in the same program keeping the declaration of both the functions same?
Content of h1.h
------------------------------------------------
h1.h
#include <iostream>
using namespace std;
int func(int a){
cout<<"Multiplied by 2";
return 2*a;
}
------------------------------------------------
Content of h2.h
------------------------------------------------
h2.h
#include <iostream>
using namespace std;
int func(int a){
cout<<"divided by 2";
return a/2;
}
------------------------------------------------
Content of h1.h
------------------------------------------------
h1.h
#include <iostream>
using namespace std;
int func(int a){
cout<<"Multiplied by 2";
return 2*a;
}
------------------------------------------------
Content of h2.h
------------------------------------------------
h2.h
#include <iostream>
using namespace std;
int func(int a){
cout<<"divided by 2";
return a/2;
}
------------------------------------------------
A. Cannot be handled because C++ does not allow this
B. Declare both the function inside different namespaces
C. Include one header files where they are needed so that no clashes occur
D. Make the header files name same
Answer: Option B
What is the correct syntax for defining a function in C++?
A. returnType functionName(parameters) { body; }
B. functionName(parameters) { returnType body; }
C. functionName(returnType, parameters) { body; }
D. returnType functionName(parameters, body) { }
What is the purpose of a function prototype in C++?
A. Determines the parameters of the function
B. Specifies the return type of the function
C. Provides the implementation of the function
D. Declares the function before it is defined
Which keyword is used to define a function in C++ that does not return any value?
A. return
B. null
C. void
D. None of the above
What is the correct way to call a function named "add" that takes two parameters in C++?
A. add(a, b);
B. add(int a, int b);
C. functionName = add(a, b);
D. add.functionName(a, b);
Join The Discussion