例:
#include"stdio.h"
class console
{
public:
void operator << (int i)
{
printf("%d",i);
}
void operator << (char c)
{
printf("%c",c);
}
};
console cout;
int main()
{
cout << 1;
cout << '\n';
return 0;
}
输出结果:1
输出1+换行
例:连续传送
#include"stdio.h"
class console
{
public:
console& operator << (int i)
{
printf("%d",i);
return *this;
}
console& operator << (char c)
{
printf("%c",c);
return *this;
}
};
console cout;
int main()
{
cout << 1 << '\n';
return 0;
}
输出结果:1
例:
#include"stdio.h"
char endl = '\n';
class console
{
public:
console& operator << (int i)
{
printf("%d",i);
return *this;
}
console& operator << (char c)
{
printf("%c",c);
return *this;
}
console& operator << (const char* s)
{
printf("%s",s);
return *this;
}
console& operator << (double d)
{
printf("%f",d);
return *this;
}
};
console cout;
int main()
{
cout << 1 << endl;
cout << "zhangyingli"<< endl;
double a = 0.1;
double b= 0.2;
cout << a+b<< endl;
return 0;
}
输出结果:
1
zhangyingli
0.300000
C++标准库
C++标准库并不是C++语言的一部分
C++标准库是由类库和函数库组成的集合
C++标准库中定义的类和对象都位于std命名空间中
C++标准库的头文件都不带.h后缀
C++标准库涵盖了C库的功能
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
printf("Hello world!\n");
char* p = (char*)malloc(16);
strcpy(p, "D.T.Software");
printf("%s\n",p);
double a = 3;
double b = 4;
double c = sqrt(a * a + b * b);
printf("c = %f\n", c);
free(p);
return 0;
}
输出结果:
Hello world!
D.T.Software
c = 5.000000
例:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout <<"hello world!"<<endl;
double a = 0;
double b = 0;
cout <<"Input a:";
cin >> a;
cout <<"Input b:";
cin >> b;
double c = sqrt(a * a + b * b);
cout <<"c = "<< c << endl;
return 0;
}
输出:
hello world!
Input a:3
Input b:4
c = 5
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。