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
• 2,520 points
10 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 3 days ago by Damon
• 4,240 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,240 points
13 views
0 votes
1 answer

Declare abstract class in c++

An abstract class is one that is intended to be used as a base class .  At least one pure virtual function exists in an abstract class.  A pure virtual function is declared in the class declaration by using a pure specifier (= 0) in the declaration of a virtual member function. Here is an example of an abstract class: class AB { public: virtual void f() ...READ MORE

answered May 31 in C++ by Damon
• 4,240 points
20 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31 in C++ by Damon
• 4,240 points
21 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
25 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,240 points
35 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,240 points
8 views
0 votes
2 answers

How can we pass a function as a parameter in java?

Java 8 and above Using Java 8+ lambda ...READ MORE

answered Aug 28, 2018 in Java by Daisy
• 8,120 points
383 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
191 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,240 points
16 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,240 points
17 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