1. 本实例使用了 endl, 这将在每一行后插入一个换行符,<<运算符用于向屏幕传多个值. 我们也使用 sizeof() 函数来获取各种数据类型的大小.
- 2.
- enum color {red,green,blue} c;
- int main()
- {
- cout << "Hello, world!" << endl;
- cout << c <<endl;
- return 0;
- }
- 3.
C++ 中的变量声明
变量声明向编译器保证变量以给定的类型和名称存在, 这样编译器在不需要知道变量完整细节的情况下也能继续进一步的编译. 变量声明只在编译时有它的意义, 在程序连接时编译器需要实际的变量声明.
当您使用多个文件且只在其中一个文件中定义变量时(定义变量的文件在程序连接时是可用的), 变量声明就显得非常有用. 您可以使用 extern 关键字在任何地方声明一个变量. 虽然您可以在 C++ 程序中多次声明一个变量, 但变量只能在某个文件, 函数或代码块中被定义一次.
4.
在 C++ 中, 有两种简单的定义常量的方式:
使用 #define 预处理器.
使用 const 关键字.
- cout << area;
存储类定义 C++ 程序中变量 / 函数的范围 (可见性) 和生命周期. 这些说明符放置在它们所修饰的类型之前. 下面列出 C++ 程序中可用的存储类:
- auto
- register
- static
- extern
- mutable
- thread_local (C++11)
7. 随机数
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- int main ()
- {
- int i,j;
- // 设置种子
- srand( (unsigned)time( NULL ) );
- /* 生成 10 个随机数 */
- for( i = 0; i <10; i++ )
- {
- // 生成实际的随机数
- j= rand();
- cout <<"随机数:" << j << endl;
- }
- return 0;
- }
8. 所有指针的值的实际数据类型, 不管是整型, 浮点型, 字符型, 还是其他的数据类型, 都是一样的, 都是一个代表内存地址的长的十六进制数. 不同数据类型的指针之间唯一的不同是, 指针所指向的变量或常量的数据类型不同.
9. 指针
- #include <iostream>
- using namespace std;
- const int MAX = 3;
- int main ()
- {
- int var[MAX] = {10, 100, 200};
- int *ptr;
- // 指针中第一个元素的地址
- ptr = var;
- int i = 0;
- while ( ptr <= &var[MAX - 1] )
- {
- cout <<"Address of var[" << i << "] =";
- cout << ptr << endl;
- cout << "Value of var[" << i << "] =";
- cout << *ptr << endl;
- // 指向上一个位置
- ptr++;
- i++;
- }
- return 0;
- }
10. 引用
引用很容易与指针混淆, 它们之间有三个主要的不同:
不存在空引用. 引用必须连接到一块合法的内存.
一旦引用被初始化为一个对象, 就不能被指向到另一个对象. 指针可以在任何时候指向到另一个对象.
引用必须在创建时被初始化. 指针可以在任何时间被初始化.
- int& r = i;
- double& s = d;
- #include <iostream>
- using namespace std;
- double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
- double& setValues( int i )
- {
- return vals[i]; // 返回第 i 个元素的引用
- }
- // 要调用上面定义函数的主函数
- int main ()
- {
- cout <<"改变前的值" << endl;
- for ( int i = 0; i < 5; i++ )
- {
- cout << "vals[" << i << "] =";
- cout << vals[i] << endl;
- }
- setValues(1) = 20.23; // 改变第 2 个元素
- setValues(3) = 70.8; // 改变第 4 个元素
- cout << "改变后的值" << endl;
- for ( int i = 0; i < 5; i++ )
- {
- cout << "vals[" << i << "] =";
- cout << vals[i] << endl;
- }
- return 0;
- }
- #include <iostream>
- #include <ctime>
- using namespace std;
- int main( )
- {
- // 基于当前系统的当前日期 / 时间
- time_t now = time(0);
- cout <<"1970 到目前经过秒数:" << now << endl;
- tm *ltm = localtime(&now);
- // 输出 tm 结构的各个组成部分
- cout << "年:"<< 1900 + ltm->tm_year <<endl;
- cout << "月:"<< 1 + ltm->tm_mon<<endl;
- cout << "日:"<< ltm->tm_mday <<endl;
- cout << "时间:"<< ltm->tm_hour <<":";
- cout << ltm->tm_min <<":";
- cout << ltm->tm_sec <<endl;
- }
- 12.IO
- typedef struct Books
- {
- char title[50];
- char author[50];
- char subject[100];
- int book_id;
- }Books;
- 15 类
- double Box::getVolume(void)
- {
- return length * breadth * height;
- }
- #include <iostream>
- using namespace std;
- class Box
- {
- public:
- double length; // 长度
- double breadth; // 宽度
- double height; // 高度
- // 成员函数声明
- double getVolume(void);
- void setLength( double len );
- void setBreadth( double bre );
- void setHeight( double hei );
- };
- // 成员函数定义
- double Box::getVolume(void)
- {
- return length * breadth * height;
- }
- void Box::setLength( double len )
- {
- length = len;
- }
- void Box::setBreadth( double bre )
- {
- breadth = bre;
- }
- void Box::setHeight( double hei )
- {
- height = hei;
- }
- // 程序的主函数
- int main( )
- {
- Box Box1; // 声明 Box1, 类型为 Box
- Box Box2; // 声明 Box2, 类型为 Box
- double volume = 0.0; // 用于存储体积
- // box 1 详述
- Box1.setLength(6.0);
- Box1.setBreadth(7.0);
- Box1.setHeight(5.0);
- // box 2 详述
- Box2.setLength(12.0);
- Box2.setBreadth(13.0);
- Box2.setHeight(10.0);
- // box 1 的体积
- volume = Box1.getVolume();
- cout <<"Box1 的体积:" << volume <<endl;
- // box 2 的体积
- volume = Box2.getVolume();
- cout << "Box2 的体积:" << volume <<endl;
- return 0;
- }
- 16.
- #include <iostream>
- using namespace std;
- class Box
- {
- public:
- double getVolume(void)
- {
- return length * breadth * height;
- }
- void setLength( double len )
- {
- length = len;
- }
- void setBreadth( double bre )
- {
- breadth = bre;
- }
- void setHeight( double hei )
- {
- height = hei;
- }
- // 重载 + 运算符, 用于把两个 Box 对象相加
- Box operator+(const Box& b)
- {
- Box box;
- box.length = this->length + b.length;
- box.breadth = this->breadth + b.breadth;
- box.height = this->height + b.height;
- return box;
- }
- private:
- double length; // 长度
- double breadth; // 宽度
- double height; // 高度
- };
- // 程序的主函数
- int main( )
- {
- Box Box1; // 声明 Box1, 类型为 Box
- Box Box2; // 声明 Box2, 类型为 Box
- Box Box3; // 声明 Box3, 类型为 Box
- double volume = 0.0; // 把体积存储在该变量中
- // Box1 详述
- Box1.setLength(6.0);
- Box1.setBreadth(7.0);
- Box1.setHeight(5.0);
- // Box2 详述
- Box2.setLength(12.0);
- Box2.setBreadth(13.0);
- Box2.setHeight(10.0);
- // Box1 的体积
- volume = Box1.getVolume();
- cout <<"Volume of Box1 :" << volume <<endl;
- // Box2 的体积
- volume = Box2.getVolume();
- cout << "Volume of Box2 :" << volume <<endl;
- // 把两个对象相加, 得到 Box3
- Box3 = Box1 + Box2;
- // Box3 的体积
- volume = Box3.getVolume();
- cout << "Volume of Box3 :" << volume <<endl;
- return 0;
- }
- 19.
- class Shape {
- protected:
- int width, height;
- public:
- Shape( int a=0, int b=0)
- {
- width = a;
- height = b;
- }
- // pure virtual function
- virtual int area() = 0;
- };
- class Box
- {
- public:
- // 纯虚函数
- virtual double getVolume() = 0;
- private:
- double length; // 长度
- double breadth; // 宽度
- double height; // 高度
- };
- 21
- #include <iostream>
- using namespace std;
- // 基类
- class Shape
- {
- public:
- // 提供接口框架的纯虚函数
- virtual int getArea() = 0;
- void setWidth(int w)
- {
- width = w;
- }
- void setHeight(int h)
- {
- height = h;
- }
- protected:
- int width;
- int height;
- };
- // 派生类
- class Rectangle: public Shape
- {
- public:
- int getArea()
- {
- return (width * height);
- }
- };
- class Triangle: public Shape
- {
- public:
- int getArea()
- {
- return (width * height)/2;
- }
- };
- int main(void)
- {
- Rectangle Rect;
- Triangle Tri;
- Rect.setWidth(5);
- Rect.setHeight(7);
- // 输出对象的面积
- cout <<"Total Rectangle area:" << Rect.getArea() << endl;
- Tri.setWidth(5);
- Tri.setHeight(7);
- // 输出对象的面积
- cout << "Total Triangle area:" << Tri.getArea() << endl;
- return 0;
- }
- #include <iostream>
- using namespace std;
- int main ()
- {
- double* pvalue = NULL; // 初始化为 null 的指针
- pvalue = new double; // 为变量请求内存
- *pvalue = 29494.99; // 在分配的地址存储值
- cout <<"Value of pvalue :" << *pvalue << endl;
- delete pvalue; // 释放内存
- return 0;
- }
- #include <iostream>
- using namespace std;
- class Box
- {
- public:
- Box() {
- cout <<"调用构造函数!" <<endl;
- }
- ~Box() {
- cout << "调用析构函数!" <<endl;
- }
- };
- int main( )
- {
- Box* myBoxArray = new Box[4];
- delete [] myBoxArray; // 删除数组
- return 0;
- }
- #include <iostream>
- #include <string>
- using namespace std;
- template <typename T>
- inline T const& Max (T const& a, T const& b)
- {
- return a < b ? b:a;
- }
- int main ()
- {
- int i = 39;
- int j = 20;
- cout << "Max(i, j):" << Max(i, j) << endl;
- double f1 = 13.5;
- double f2 = 20.7;
- cout << "Max(f1, f2):" << Max(f1, f2) << endl;
- string s1 = "Hello";
- string s2 = "World";
- cout << "Max(s1, s2):" << Max(s1, s2) << endl;
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2656933.html