1.有趣的重载
操作符<<的原生意义是按位左移
重载左移操作符,将变量或者常量左移到一个对象中
//重载<<的演示#include<stdio.h>const char endl = '\n';class Console{public:int operator <<(int i){printf("%d", i);return i;}Console& operator <<(char c){printf("%c", c);return *this;}};Console cout;int main(){cout << 1 << endl;//cout << "\n";return 0;}
这种很好的方法,前人已经实现在c++的标准库了
2.c++标准库
—-c++标准库并不是c++语言的一部分
—-c++标准库是由类库和函数库组成的集合
—-c++标准库中定义的类和对象都位于std命名空间中
—-c++标准库的头文件都不带.h后缀
—-c++标准库涵盖了c库的功能


3.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");double a = 3;double b = 4;double c = sqrt(a * a + b * b);printf("c = %f\n", c);free(p);return 0;}
显示器——————-》cout c++标准库中的显示器对象
键盘———————-》cin c++标准库中的键盘对象
实验:完全由c++标准库实现
#include<iostream>#include<cmath>using namespace std;int main(){cout << "Hello World" <<endl;double a = 0;double b = 0;cout << "inputa a: ";cin >> a;cout << "inputa b: ";cin >> b;double c = sqrt( a * a + b * b);cout << "c = " << c << endl;return 0;}
from now on!
用c++标准库!!!!!!
小结:
1.c++标准库由类库和函数库组成的集合
2.c++标准库包含经典算法和数据结构的实现
3.c++标准库涵盖了c库的功能
4.c++标准库位于std命名空间
