Question

In: Computer Science

Why would you use "using namespace" inside of your function instead of the above main?- Give...

  1. Why would you use "using namespace" inside of your function instead of the above main?- Give examples

Solutions

Expert Solution

/*If you have any query do comment in the comment section else like the solution*/

If you'll declare the using namespace above main, then while using any standard object you do not have to write std::, but if you will declare inside a function then the inbuild objects can be used only inside that function without writing std::

Ex: Using namespace above main, then there is no need to use std:: inside any function to use cout.

#include <iostream>

using namespace std;

void fun() {
    cout<<"Inside fun";
}
int main()
{
    cout<<"Hello World";
    fun();
}

Using name space inside a function, it has been declared inside fun() method so to use cout there is no need to prepend it with std::, but since it is local to function fun(), you have to use std::before cout to make it work properly

#include <iostream>

void fun() {
    using namespace std;
    cout<<"Inside fun";
}
int main()
{
    std::cout<<"Hello World";
    fun();
}

Related Solutions

In your own words, explain why you would use a nonparametric test instead of a parametric...
In your own words, explain why you would use a nonparametric test instead of a parametric test. How are they different? How are they the same? Come up with a research study example for each of the different kinds of Chi square tests: no difference, goodness of fit, and test of independence.
Give four reasons why system calls are typically implemented using interrupts instead of function calls.
Give four reasons why system calls are typically implemented using interrupts instead of function calls.
When would you use the HLOOKUP function instead of a VLOOKUP? What does the Subtotal dialog...
When would you use the HLOOKUP function instead of a VLOOKUP? What does the Subtotal dialog box do in addition to inserting subtotals? Why do you have to select a range first to use the Quick Analysis tool?
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate...
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate memory for at least 3 double elements //ensure all elements have been initialized //call function, replace 0 with the proper number of elements pointed to by dd ddAns = lookUpFirstNeg(dd, 0); if(ddAns == NULL) cout << "No negatives found.\n"; else cout << "First negative value is:" << ddAns << endl; //properly deallocate memory allocated above return 0; }
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
When and why would you use a mixed methods approach instead of only qualitative or quantitative?
When and why would you use a mixed methods approach instead of only qualitative or quantitative?
Give at least three examples of when you would use ANOVA. Why would you use ANOVA...
Give at least three examples of when you would use ANOVA. Why would you use ANOVA as opposed to t-Tests to determine the mean differences? Which method do you feel is easier to comprehend?
1 What is a pure virtual function? Why would you define a pure virtual function? Give...
1 What is a pure virtual function? Why would you define a pure virtual function? Give an example of a pure virtual function. 2 When a function has an object parameter that it doesn't modify, what is the best way to declare the parameter (in the function signature), and why? 3 Show an example using a function with a string object parameter. a) When a class has objects as data members, why should its constructors use initialization lists (member and...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT