Using scope resolution operator in C

0 votes
I'm studying C++ and I never know when I'll need to use::.

I am aware that I must use std:: in front of cout and cin.

Does this imply that the developers who built the iostream file established a namespace named std and placed the methods cin and cout in that namespace?

I had to add:: when I built a new class that wasn't in the same file as main() for some reason.

For example, if I construct a class called A, why do I have to add A:: in front of a function I write, even if I don't place it in a namespace?

For instance, void A::printStuff().

Why don't I have to add main::printStuff if I construct a function in main?

I realise my query is probably complex, but could someone please assist me?
Jun 22 in C++ by Nicholas
• 5,940 points
33 views

1 answer to this question.

0 votes

You're mostly correct regarding cout and cin. They are objects (rather than functions) specified under the std namespace. The following are their C++ standard declarations:

Header <iostream> synopsis

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

namespace std {
  extern istream cin;
  extern ostream cout;
  extern ostream cerr;
  extern ostream clog;

  extern wistream wcin;
  extern wostream wcout;
  extern wostream wcerr;
  extern wostream wclog;
}

The scope resolution operator is denoted by ::. 

Because the identifiers cout and cin are declared within std, we must qualify them with std::.

Classes function similarly to namespaces in that the names stated within the class belong to the class. 

As an example:

class foo
{
  public:
    foo();
    void bar();
};

The foo function Object() { [native code] } is a member of the foo class. 

They share the same name because the function Object() { [native code] } is the same. 

The function bar is also a foo member.

Because they are members of foo, we must qualify their names when referring to them outside of the class. 

After all, they are members of that group. 

So, if you want to declare the function Object() { [native code] } and bar outside of the class, you must do so as follows:

foo::foo()
{
  // Implement the constructor
}

void foo::bar()
{
  // Implement bar
}

This is due to the fact that they are specified outside of the class. 

If you hadn't included the foo:: qualifier to the names, you'd be introducing some new functions in the global scope rather than as foo members. 

For example, consider the following:

void bar()
{
  // Implement different bar
}

Because it is in a distinct scope, it is permitted to have the same name as the function in the foo class. 

This bar is global, whereas the other bar belongs to the foo class.

answered Jun 27 by Damon
• 4,960 points

Related Questions In C++

0 votes
1 answer

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

answered Jun 20 in C++ by Damon
• 4,960 points
31 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1 in C++ by Nicholas
• 5,940 points
26 views
+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

Jul 4 in C++ by Nicholas
• 5,940 points
50 views
0 votes
0 answers

Using "super" in C++

The following phrase is part of my coding approach: class Derived : public Base { ...READ MORE

Jul 13 in C++ by Nicholas
• 5,940 points
35 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
92 views
0 votes
1 answer

Explanation of function pointers

It's a little perplexing. Function type and pointer to function type are distinct kinds (no more similar than int and pointer to int).  However, in virtually all cases, a function type decays to a reference to a function type.  In this context, rotting roughly refers to conversion (there is a difference between type conversion and decaying, but you are probably not interested in it right now). What matters is that practically every time you use a function type, you end up with a reference to the function type.  But take note of the nearly - almost every time is not always! And there are rare circumstances where it does not. typedef void(functionPtr)(int); functionPtr fun = function; This code tries to clone one function to another (not the pointer! the function!)  However, this is not feasible since functions in C++ cannot be copied.  The compiler does not let this, and I'm surprised you got it compiled (you say you got linker errors?) Now for the code: typedef void(functionPtr)(int); functionPtr function; function(5); function does ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
25 views
0 votes
0 answers

How to pass reference-to-function into another function

Using function pointers as arguments for other ...READ MORE

Jul 11 in C++ by Nicholas
• 5,940 points
33 views
0 votes
0 answers

What type of members can I add in a c++ abstract class

Hello, let's say I have an abstract class with a few pure abstract functions and a few classes that derive from it, and all of the data from these classes eventually becomes similar, I was wondering if it would be wise, or even possible, to declare a vector under protected in the abstract class to collect the data so something like that. class A { protected: vector <string> ...READ MORE

Jul 27 in C++ by Nicholas
• 5,940 points
27 views
0 votes
1 answer

Why does C++ need the scope resolution operator?

No. There is no scope resolution operator ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
54 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
117 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP