Hello, I’m Yue Chuang.
Note: This article requires at least some knowledge of the concepts of classes and objects and overloaded operators.
Having been around C++ for so long, I want to try to write my own cout. Before implementation, a few concepts are pulled apart:
Who is COUT?
Isn’t that an output statement? ! The answer is too obvious.
Cout is an object of class ostream that has a member overloaded operator function: operator <<. Incidentally, the ostream class belongs to the iostream library, which is a standard C++ header file. In this case, the header file would look like this:
//iostream
class ostream
{
public:
ostream operator< < (intN){output n} ostreamoperator< < (doubleN){output n}... } ostream cout;Copy the code
There are multiple overloaded versions of ostream::operator <<, so the parameter can be of any type, including int, double, char, and so on. When we write cout<
The cout statement format is: cout<< expression 1<< expression 2<<… << expression n; , so we can only write things like cout<
Ostream ::operator << return *this Cout <
So it can be implemented like this:
ostream &operator< < (intN) {output n;return *this;
}
Copy the code
What is namespace?
We often start programs with using namespace STD; But have you ever wondered what it’s for?
As the name implies, namespace stands for namespace, which is used to organize and reuse code. So what exactly does this do?
Ok, so let’s imagine a situation in our head: You write a library file, and it has a function called fun in it, and unfortunately someone else’s library file also has a function called fun, and it conflicts.
// In your library file:
void fun(a);
// Other people's library file:
void fun(a);
// Use fun()
fun(a);// Which one should I use? Conflict!
Copy the code
At this time, we thought of a way:
- Put your fun in one of your namespaces, and someone else’s fun in another.
- Whenever someone asks to use your space, the fun he uses is your fun, not someone else’s.
- If that person says to use someone else’s space, then the fun he uses must be someone else’s fun, not yours.
// In your library file:
namespace you{void fun(a); }// Other people's library file:
namespace other{void fun(a); }// When someone uses your function:
you::fun(a);// When someone uses another person's function:
other::fun(a);// If someone asks to use your space, something like this:
using namespace you;
fun(a);// This is actually you::fun(); , won't be other::fun();
Copy the code
If you use a cout instead of a namespace, the system does not know whose Cout you are using.
In order to solve the problem of name conflicts, we introduced the concept of namespaces by using namespace XXX; The library functions or variables you use are defined in this namespace, so there are no unnecessary conflicts.
So why do we write using namespace STD; ? Early C defined library functions in global space, declared in header files with the.h suffix.
-
The C++ standard, in order to distinguish itself from C and to use namespaces correctly, does not use the.h suffix for header files. Thus, using
is equivalent to calling library functions in C, using the global namespace, which was also an early C++ implementation;
-
When using
, the header does not define a global namespace; namespace STD must be used; So you can use COUT correctly.
Using namespace STD; That would be STD ::cout<<. Using namespace STD; using namespace STD; .
The iostream library looks like this:
//iostream
namespace std{
class ostream
{
public:
ostream operator< < (intN){output n} ostreamoperator< < (doubleN){output n}... } } std::ostream cout;Copy the code
Implement COUT by yourself
Cout (cOUT) cout (cOUT)
#include<stdio.h>
namespace Mystd{ // Namespace
class MyOstream{
public:
const MyOstream& operator< < (const int integer) const
{
printf("%d", integer);
return *this;
}
const MyOstream& operator< < (const char *s) const
{
printf("%s", s);
return *this; }}; }void Print(a)
{
Mystd::MyOstream MyAnothercout;
MyAnothercout<<"Print";
}
int main(a)
{
Mystd::MyOstream Mycout;
int a = 30, b = 20;
Mycout<<"number:"<<a<<""<<b<<"\n";
Print(a);return 0;
}
Copy the code
This code has been compiled and output correctly. As we learn more about these features of C++, it is necessary to understand them.
AI yue chuang has launched tutorial classes, including “Python language tutorial, C++ tutorial, algorithm/data structure tutorial”, all one-to-one teaching: one-to-one tutoring + one-to-one q&a + assignment + project practice, etc. QQ, wechat online, response at any time!