C++ 基础&中级

   1 #include <iostream>
   2 #include <limits>
   3 #include <iomanip>
   4 #include <cstring>
   5 #include <ctime>
   6 #include<fstream>
   7 #include<assert.h>
   8 
   9 
  10 #if 0
  11 //< iostream > 该文件定义了 cin、cout、cerr 和 clog 对象,分别对应于标准输入流、标准输出流、非缓冲标准错误流和缓冲标准错误流。
  12 
  13 //<iomanip>该文件通过所谓的参数化的流操纵器(比如 setw 和 setprecision),来声明对执行标准化 I / O 有用的服务。
  14 
  15 //<fstream>该文件为用户控制的文件处理声明服务。我们将在文件和流的相关章节讨论它的细节。
  16 
  17 #endif
  18 //-----------------------------------------------------------------------------------//
  19 using namespace std; //告诉编译器使用 std 命名空间。命名空间是 C++ 中一个相对新的概念。
  20                      //所谓命名空间,实际上就是一个程序设计者命名的内存空间,设计者可以
  21                      //根据需要指定一些有名字的空间域,把一些全局实体分别放在各个命名空
  22                      //间中,从而与其他全局实体分隔开来。
  23 //-----------------------------------------------------------------------------------//
  24 
  25 
  26 #if 0
  27     //code
  28     typedef type newname;    //  typedef 为一个已有的类型取一个新的名字
  29     typedef int feet;        //  告诉编译器,feet 是 int 的另一个名称
  30     feet distance;           //  下面的声明是完全合法的,它创建了一个整型变量 distance
  31 #endif
  32 
  33 //----------------------------------------------------------------------------------//
  34 //define identifier value
  35 #define     LENGTH      (10)
  36 #define     WIDTH       (5)
  37 #define     NEWLINE     ('\n')
  38 
  39 
  40 // const type variable = value;  //const 类型的对象在程序执行期间不能被修改改变。
  41 // 修饰符 volatile 告诉编译器不需要优化volatile声明的变量,让程序可以直接从内存中
  42 // 读取变量。对于一般的变量编译器会对变量进行优化,将内存中的变量值放在寄存器中以
  43 // 加快读写效率。 由 restrict 修饰的指针是唯一一种访问它所指向的对象的方式。
  44 // 只有 C99 增加了新的类型限定符 restrict。
  45 
  46 
  47 //-----------------------------------------------------------------------------------//
  48 //extern    int    color_name;  // 使用 extern 关键字在任何地方声明一个变量
  49 enum color { red, green, blue } color_name;  // 枚举
  50 
  51 
  52 static int ZXC_count = 10; /* 全局变量 */
  53 
  54 
  55 
  56 static  unsigned long  sec_seconds;
  57 
  58 
  59 static  int    rt_balance[5] = { 1000, 2, 3, 17, 50 };  // 带有 5 个元素的整型数组
  60 static  double rt_avg;
  61 
  62 
  63 // 声明一个结构体类型 Books 
  64 static  struct  Books
  65 {
  66     char  title[50];
  67     char  author[50];
  68     char  subject[100];
  69     int   book_id;
  70 }_matcher;
  71 
  72 //-----------------------------------------------------------------------------------//
  73 
  74 
  75 
  76 //--------------------------------------senior---------------------------------------//
  77 
  78 
  79 
  80 
  81 
  82 
  83 
  84 
  85 
  86 
  87 
  88 
  89 
  90 
  91 
  92 
  93 
  94 void    display_fstream_data()
  95 {
  96     /*
  97     //-----------------------------------------------------------------------------//
  98     // ofstream :该数据类型表示输出文件流,用于创建文件并向文件写入信息。
  99     // ifstream: 该数据类型表示输入文件流,用于从文件读取信息。
 100     // fstream : 该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,
 101     //            这意味着它可以创建文件,向文件写入信息,从文件读取信息。
 102     // 要在 C++ 中进行文件处理,必须在 C++ 源代码文件中包含头文件 <iostream> 和 <fstream>。
 103     //
 104     // void open(const char *filename, ios::openmode mode);
 105     // open() 成员函数的第一参数指定要打开的文件的名称和位置,第二个参数定义文件被打开的模式。
 106     // ios::app      追加模式。所有写入都追加到文件末尾。
 107     // ios::ate      文件打开后定位到文件末尾。
 108     // ios::in       打开文件用于读取。
 109     // ios::out      打开文件用于写入。
 110     // ios::trunc    如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。
 111     //
 112     // void close();
 113     //
 114     //使用流插入运算符( << )向文件写入信息---写入文件
 115     //
 116     //使用流提取运算符( >> )从文件读取信息---读取文件
 117 
 118 
 119     //-----------------------------------------------------------------------------//
 120     */
 121     
 122     char data_file[100];
 123 
 124     // 以写模式打开文件
 125     ofstream    outfile1;
 126     //outfile.open("afile.dat");
 127     cout << "*---------------开始--------------*" << "\n" << endl;
 128     outfile1.open("afile.txt");
 129     cout << "1---Writing to the file" << "\n" << endl;
 130     cout << "\t 2---  " << "Enter your name: ";
 131     cin.getline(data_file, 100);
 132 
 133     // 向文件写入用户输入的数据
 134     outfile1 <<  data_file << endl;
 135     cout     << "\t 4---  " << "Enter your age : ";
 136     cin >> data_file;
 137     cin.ignore();
 138 
 139     // 再次向文件写入用户输入的数据
 140     outfile1 << data_file << endl;
 141 
 142     // 关闭打开的文件
 143     outfile1.close();
 144 
 145     // 以读模式打开文件
 146     ifstream infile2;
 147     infile2.open("afile.txt");
 148 
 149     cout << "6---" << "Reading from the file" << endl;
 150     infile2 >> data_file;
 151 
 152     // 在屏幕上写入数据
 153     cout << "\t 7---  " << data_file << endl;
 154 
 155     // 再次从文件读取数据,并显示它
 156     infile2 >> data_file;
 157     cout << "\t 8---  " << data_file << endl;
 158 
 159     // 关闭打开的文件
 160     infile2.close();
 161 
 162 }
 163 
 164 
 165 //--------------------------------------中级---------------------------------------//
 166 
 167 class Shape_polymorphic
 168 {
 169     protected:
 170         int width_polymorphic, height_polymorphic;
 171     public:
 172         Shape_polymorphic(int a_polymorphic = 0, int b_polymorphic = 0)
 173         {
 174             width_polymorphic = a_polymorphic;
 175             height_polymorphic = b_polymorphic;
 176             cout << "* 1---Shape_polymorphic* " << endl;
 177             cout << width_polymorphic<< height_polymorphic << " \n" << endl;
 178         }
 179         virtual int area_polymorphic()
 180         {
 181             cout << "2---Parent class area :" << endl;
 182             return 0;
 183         }
 184 };
 185 class Rectangle_polymorphic : public Shape_polymorphic
 186 {
 187     public:
 188         Rectangle_polymorphic(int a_polymorphic = 0, int b_polymorphic = 0) :Shape_polymorphic(a_polymorphic, b_polymorphic) { }
 189         int area_polymorphic()
 190         {
 191             cout << "Rectangle class area :" << endl;
 192             return (width_polymorphic * height_polymorphic);
 193         }
 194 };
 195 class Triangle_polymorphic : public Shape_polymorphic 
 196 {
 197     public:
 198         Triangle_polymorphic(int a_polymorphic = 0, int b_polymorphic = 0) :Shape_polymorphic(a_polymorphic, b_polymorphic) { }
 199         int area_polymorphic()
 200         {
 201             cout << "Triangle class area :" << endl;
 202             return (width_polymorphic * height_polymorphic / 2);
 203         }
 204 };
 205 
 206 
 207 
 208 void display_polymorphic()
 209 {
 210     // 多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,
 211     // 就会用到多态。C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。
 212     cout << "* -------------开始------------ * " << " \n" << endl;
 213     Shape_polymorphic       *shape_polymorphic;
 214     cout << "* -------shape_polymorphic----- * " << " \n" << endl;
 215     Rectangle_polymorphic   rec(10, 7);
 216     cout << "* -------rec----- * " << " \n" << endl;
 217     Triangle_polymorphic    tri(10, 5);
 218     cout << "* -------tri----- * " << " \n" << endl;
 219 
 220     // 存储矩形的地址
 221     shape_polymorphic = &rec;
 222     cout << "* ----存储矩形的地址---------- * " << " \n" << endl;
 223     // 调用矩形的求面积函数 area
 224     shape_polymorphic->area_polymorphic();
 225     cout << "* ----调用矩形的求面积函数---- * " << " \n" << endl;
 226 
 227     // 存储三角形的地址
 228     shape_polymorphic = &tri;
 229     cout << "* -------存储三角形的地址----------- * " << " \n" << endl;
 230     // 调用三角形的求面积函数 area
 231     shape_polymorphic->area_polymorphic();
 232     cout << "* -------调用三角形的求面积函数----- * " << " \n" << endl;
 233 
 234 }
 235 
 236 class printData_heavy_load
 237 {
 238     public:
 239         void print(int i) 
 240         {
 241             cout << "整数为: " << i << endl;
 242         }
 243         void print(double  f) 
 244         {
 245             cout << "浮点数为: " << f << endl;
 246         }
 247         void print(char c[]) 
 248         {
 249             cout << "字符串为: " << c << endl;
 250         }
 251 };
 252 
 253 
 254 void display_heavy_load()
 255 {
 256 // C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。
 257 // 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,
 258 // 但是它们的参数列表和定义(实现)不相同。当您调用一个重载函数或重载运算符时,
 259 // 编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。
 260 // 选择最合适的重载函数或重载运算符的过程,称为重载决策。
 261 
 262 // 在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数
 263 //(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。
 264 // 下面的实例中,同名函数 print() 被用于输出不同的数据类型:
 265 
 266     printData_heavy_load     pd1;
 267 
 268     // 输出整数
 269     pd1.print(5);
 270     // 输出浮点数
 271     pd1.print(500.263);
 272     // 输出字符串
 273     char c[] = "Hello C++";
 274     pd1.print(c);
 275 }
 276 
 277 
 278 class Shape_inherit
 279 {
 280         // 基类
 281     public:
 282         void setWidth_inherit(int w_inherit)
 283         {
 284             width_inherit = w_inherit;
 285             cout << "1--width_inherit: \t" << width_inherit << "\n"<< endl;
 286         }
 287         void setHeight_inherit(int h_inherit)
 288         {
 289             height_inherit = h_inherit;
 290             cout << "2--height_inherit: \t" << height_inherit << "\n" << endl;
 291         }
 292     protected:
 293         int width_inherit;
 294         int height_inherit;
 295 };
 296 
 297 class PaintCost_inherit
 298 {
 299     // 基类 PaintCost
 300     public:
 301         int getCost_inherit(int area_inherit)
 302         {
 303             return area_inherit * 70;
 304         }
 305 };
 306 
 307 class Rectangle_inherit : public Shape_inherit
 308 {
 309     // 派生类
 310     public:
 311         int getArea_inherit()
 312         {
 313             cout << "3--getArea_inherit: \t" << width_inherit * height_inherit << "\n" << endl;
 314             return (width_inherit * height_inherit);
 315         }
 316 };
 317 
 318 class Rectangle_inherit2 : public Shape_inherit, public PaintCost_inherit
 319 {
 320     // 派生类
 321     public:
 322         int getArea_inherit()
 323         {
 324             return (width_inherit * height_inherit);
 325         }
 326 };
 327 
 328 void display_inherit()
 329 {
 330 // 一个类可以派生自多个类,这意味着,它可以从多个基类继承数据和函数。
 331 // 定义一个派生类,我们使用一个类派生列表来指定基类。类派生列表以一个或多个基类命名
 332 // 333 // class derived-class: access-specifier base-class
 334 
 335     Rectangle_inherit Rect1;
 336 
 337     Rect1.setWidth_inherit(15);
 338     Rect1.setHeight_inherit(17);
 339 
 340     // 输出对象的面积
 341     cout << "Total area: \t" << Rect1.getArea_inherit() << endl;
 342 
 343 //继承类型
 344 // 当一个类派生自基类,该基类可以被继承为 public、protected 或 private 几种类型。
 345 // 继承类型是通过上面讲解的访问修饰符 access-specifier 来指定的。
 346 // 我们几乎不使用 protected 或 private 继承,通常使用 public 继承。
 347 // 当使用不同类型的继承时,遵循以下几个规则:public/protected/private
 348 // 公有继承:当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护
 349 //           成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问。
 350 // 保护继承: 当一个类派生自保护基类时,基类的公有和保护成员将成为派生类的保护成员。
 351 // 私有继承:当一个类派生自私有基类时,基类的公有和保护成员将成为派生类的私有成员。
 352 
 353     //多继承即一个子类可以有多个父类,它继承了多个父类的特性。
 354     //class <派生类名> :<继承方式1><基类名1>, <继承方式2><基类名2>, …
 355     //{
 356     //    <派生类类体>
 357     //};
 358 
 359     Rectangle_inherit2       Rect2;
 360     int area2;
 361 
 362     Rect2.setWidth_inherit(5);
 363     Rect2.setHeight_inherit(7);
 364 
 365     area2 = Rect2.getArea_inherit();
 366 
 367     // 输出对象的面积
 368     cout << "Total area: " << Rect2.getArea_inherit() << endl;
 369 
 370     // 输出总花费
 371     cout << "Total paint cost: $" << Rect2.getCost_inherit(area2) << endl;
 372 }
 373 
 374 class class_Line_CC_B
 375 {
 376     public:
 377         int     getLength_Line_CC_B(void);
 378         class_Line_CC_B(int len);                            // 简单的构造函数
 379         class_Line_CC_B(const class_Line_CC_B &obj_cc);      // 拷贝构造函数
 380         ~class_Line_CC_B();                                  // 析构函数
 381 
 382     private:
 383         int *ptr_Line_CC_B;
 384 };
 385 
 386 class_Line_CC_B::class_Line_CC_B(int len_CC_B)
 387 {
 388     // 成员函数定义,包括构造函数
 389     cout << "1--调用构造函数" << "\t\n" << endl;
 390     // 为指针分配内存
 391     ptr_Line_CC_B = new int;
 392     cout << "1.1--内存" << ptr_Line_CC_B << "\t\n" << endl;
 393     *ptr_Line_CC_B = len_CC_B;
 394     cout << "1.2--形参" << len_CC_B << "\t\n" << endl;
 395     cout << "1.3--值域" << *ptr_Line_CC_B << "\t\n" << endl;
 396 }
 397 
 398 class_Line_CC_B::class_Line_CC_B(const class_Line_CC_B  &obj_cc)
 399 {
 400     cout << "2--调用拷贝构造函数并为指针 ptr 分配内存" << "\t\n" << endl;
 401     ptr_Line_CC_B = new int;
 402     cout << "2.1--形参" << *obj_cc.ptr_Line_CC_B << "\t\n" << endl;
 403     *ptr_Line_CC_B = *obj_cc.ptr_Line_CC_B; // 拷贝值
 404     cout << "2.2--实参" << *ptr_Line_CC_B << "\t\n" << endl;
 405 }
 406 
 407 class_Line_CC_B::~class_Line_CC_B(void)
 408 {
 409     cout << "3--释放内存" << "\t\n" << endl;
 410     cout << "3.1--形参  " << &ptr_Line_CC_B << "\t\n" << endl;
 411     delete ptr_Line_CC_B;
 412     cout << "3.2--实参  " << &ptr_Line_CC_B << "\t\n" << endl;
 413 }
 414 
 415 int class_Line_CC_B::getLength_Line_CC_B(void)
 416 {
 417     cout << "4--getLength: \t" << *ptr_Line_CC_B << "\t\n" << endl;
 418     return *ptr_Line_CC_B;
 419 }
 420 
 421 void display_class_Line_CC_B(class_Line_CC_B    obj_cc)
 422 {
 423     cout << "5--line 大小 : " << obj_cc.getLength_Line_CC_B() << "\t\n" << endl;
 424 }
 425 
 426 void display_copy_construction()
 427 {
 428 /*
 429 拷贝构造函数是一种特殊的构造函数,它在创建对象时,
 430 是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:
 431 通过使用另一个同类型的对象来初始化新创建的对象。
 432 复制对象把它作为参数传递给函数。
 433 复制对象,并从函数返回这个对象。
 434 
 435 如果在类中没有定义拷贝构造函数,编译器会自行定义一个。如果类带有指针变量,
 436 并有动态内存分配,则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:
 437 classname (const classname &obj) 
 438 {
 439    // 构造函数的主体
 440 }
 441 */
 442 
 443     cout << "*-------------开始-------------*" << "\n"<< endl;
 444     class_Line_CC_B     Line_CC_B(1234);
 445     cout << "*-------------显示1-------------*" << "\n" << endl;
 446     display_class_Line_CC_B(Line_CC_B);
 447     cout << "*-------------中间-------------*" << "\n" << endl;
 448     class_Line_CC_B     Line_CC_BB= Line_CC_B;
 449     cout << "*-------------显示2-------------*" << "\n" << endl;
 450     display_class_Line_CC_B(Line_CC_BB);
 451     cout << "*-------------结束-------------*" << "\n" << endl;
 452 ;
 453 }
 454 
 455 
 456 
 457 
 458 class class_Line_A
 459 {
 460    public:
 461       void   setLength_Line_A(double len);
 462       double getLength_Line_A(void);
 463       class_Line_A(double len);  // 这是构造函数
 464       ~class_Line_A();           // 这是析构函数声明
 465       //类的构造函数是一种特殊的函数,在创建一个新的对象时调用。
 466       //类的析构函数也是一种特殊的函数,在删除所创建的对象时调用。
 467    private:
 468       double length;
 469 };
 470 
 471 class_Line_A::class_Line_A(double len)
 472 {
 473     // 成员函数定义,包括构造函数
 474     cout << "*---成员函数定义,包括构造函数---* \t"<< endl;
 475     cout << "1--Object is being created, len    = " << len    << "\n" <<endl;
 476     length = len;
 477     cout << "2--Object is being created, length = " << length << "\n" << endl;
 478 }
 479 
 480 class_Line_A::~class_Line_A(void)
 481 {
 482     cout << "****---> 析构函数 <---****" << endl;
 483     cout << "5--Object is being deleted" << endl;
 484 }
 485 
 486 void class_Line_A::setLength_Line_A(double len)
 487 {
 488     length = len;
 489     cout << "3--Object is being created, length = " << length << "\n" << endl;
 490 }
 491 
 492 double class_Line_A::getLength_Line_A(void)
 493 {
 494     cout << "4--Object is being created, length = " << length << "\n" << endl;
 495     return length;
 496 }
 497 
 498 void display_Constructor_destructor()
 499 {
 500     class_Line_A         Line_A(110.0);
 501 
 502     // 获取默认设置的长度
 503     cout << "获取默认长度    -->    Length of line : " << Line_A.getLength_Line_A() << " \t <---- \n"<< endl;
 504     // 再次设置长度
 505     Line_A.setLength_Line_A(16.0);
 506     cout << "再次设置长度    -->    Length of line : " << Line_A.getLength_Line_A() << " \t <---- \n" << endl;
 507 
 508     // 设置长度
 509     Line_A.setLength_Line_A(26.0);
 510     cout << "#设置长度#    -->    Length of line : " << Line_A.getLength_Line_A() << " \t <---- \n" << endl;
 511 }
 512 
 513 
 514 
 515 class Box_class1
 516 {
 517     public:
 518         double      length;   // 长度
 519         double      breadth;  // 宽度
 520         double      height;   // 高度
 521 
 522         double      length_pub4;   // 长度
 523         void        setWidth_pub4(double wid);
 524         double      getWidth_pub4(void);
 525 
 526         // 成员函数声明
 527         double      box3_get(void);
 528         void        box3_set(double len, double bre, double hei);
 529         void        setLength(double len);
 530         void        setBreadth(double bre);
 531         void        setHeight(double hei);
 532 
 533 
 534     private:
 535         double      width_pri4;
 536 
 537 
 538     protected:
 539         double      width_pro5;
 540 
 541 
 542 };
 543 
 544 class Box_class1_A 
 545 {
 546     // public 继承
 547     public:
 548         int A_pub_a;
 549         Box_class1_A()
 550         {
 551             cout << "Box_class1_A-A \t" << endl;   //正确
 552             A_pub_a1 = 11;
 553             cout << "A_pub_CA_a1 \t" << A_pub_a1 << endl;   //正确
 554             A_pub_a2 = 12;
 555             cout << "A_pub_CA_a2 \t" << A_pub_a2 << endl;   //正确
 556             A_pub_a3 = 13;
 557             cout << "A_pub_CA_a3 \t" << A_pub_a3 << endl;   //正确
 558             A_pub_a = 14;
 559             cout << "A_pub_CA_a    \t" << A_pub_a << endl;       //正确
 560             cout << "\n"<< endl;
 561         }
 562         void fun_A() 
 563         {
 564             cout << "A_pub_a0_F \t" << A_pub_a  << endl;       //正确
 565             cout << "A_pub_a1_F \t" << A_pub_a1 << endl;   //正确
 566             cout << "A_pub_a2_F \t" << A_pub_a2 << endl;   //正确
 567             cout << "A_pub_a3_F \t" << A_pub_a3 << endl;   //正确
 568             cout << "\n" << endl;
 569         }
 570     public:
 571         int A_pub_a1;
 572     protected:
 573         int A_pub_a2;
 574     private:
 575         int A_pub_a3;
 576 };
 577 
 578 class Box_class1_B : public Box_class1_A
 579 {
 580     public:
 581         int B_pub_a;
 582         Box_class1_B(int er_i)
 583         {
 584             cout << "Box_class1_A-公共继承 \t" << endl;
 585             Box_class1_A();
 586             B_pub_a = er_i;
 587             cout << "class1_B-public-class1_A \t" << B_pub_a << endl;   //正确
 588             cout << "\n" << endl;
 589         }
 590         void fun_B() 
 591         {
 592             cout << "B_pub_a_F \t" <<  A_pub_a << endl;         //正确,public成员
 593             cout << "B_pub_a0_F \t" << A_pub_a1 << endl;       //正确,基类的public成员,在派生类中仍是public成员。
 594             cout << "B_pub_a2_F \t" << A_pub_a2 << endl;       //正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。
 595             //cout << "B_pub_a3_F \t" << A_pub_a3 << endl;       //错误,基类的private成员不能被派生类访问。
 596             cout << "B_pub_a_F-B_pub_a \t" << B_pub_a << endl;          //正确,public成员
 597         }
 598 };
 599 
 600 class Box_class1_B_pro : protected Box_class1_A
 601 {
 602 public:
 603     int B_pro_a;
 604     Box_class1_B_pro(int B_pro_i)
 605     {
 606         cout << "Box_class1_A-保护继承 \t" << endl;
 607         Box_class1_A();
 608         B_pro_a = B_pro_i;
 609         cout << "class1_B_pro-protected-class1_A \t" << B_pro_a << endl;   //正确
 610         cout << "\n" << endl;
 611     }
 612     void fun_B_pro() 
 613     {
 614         cout << "B_pro_a0 \t" << B_pro_a << endl;       //正确,public成员。
 615         cout << "B_pro_a1 \t" << A_pub_a1 << endl;       //正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。
 616         cout << "B_pro_a2 \t" << A_pub_a2 << endl;       //正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。
 617         //cout << "B_pro_a3 \t" << A_pub_a3 << endl;       //错误,基类的private成员不能被派生类访问。
 618     }
 619 };
 620 
 621 class Box_class1_B_pri  : private Box_class1_A 
 622 {
 623     public:
 624       int B_pri_a;
 625       Box_class1_B_pri(int  B_pri_i)
 626       {
 627             cout << "Box_class1_A-私有继承 \t" << endl;
 628             Box_class1_A();
 629             B_pri_a = B_pri_i;
 630             cout << "class1_B_pro-protected-class1_A \t" << B_pri_a << endl;   //正确
 631             cout << "\n" << endl;
 632       }
 633       void fun_B_pri()
 634       {
 635         cout << "B_pri_a0 \t" << B_pri_a << endl;       //正确,public成员。
 636         cout << "B_pri_a0 \t" << A_pub_a1 << endl;       //正确,基类public成员,在派生类中变成了private,可以被派生类访问。
 637         cout << "B_pri_a0 \t" << A_pub_a2 << endl;       //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。
 638         //cout << "B_pri_a0 \t" << A_pub_a3 << endl;       //错误,基类的private成员不能被派生类访问。
 639       }
 640 };
 641 
 642 class SmallBox :Box_class1 
 643 {
 644     // SmallBox 是派生类
 645 public:
 646     void    setSmallWidth_pro5(double wid);
 647     double  getSmallWidth_pro5(void);
 648 };
 649 
 650 double SmallBox::getSmallWidth_pro5(void)
 651 {
 652     return width_pro5;    // 子类的成员函数
 653 }
 654 
 655 void SmallBox::setSmallWidth_pro5(double wid)
 656 {
 657     width_pro5 = wid;
 658 }
 659 
 660 double Box_class1::getWidth_pub4(void)
 661 {
 662     return      width_pri4;    // 成员函数定义
 663 }
 664 
 665 void Box_class1::setWidth_pub4(double wid)
 666 {
 667     width_pri4 = wid;
 668 }
 669 
 670 double Box_class1::box3_get(void)
 671 {
 672     //Class::Method 是类 Class 中的方法 Method 的意思,常用于在 class 块外定义方法。(声明在内)
 673     // 成员函数定义
 674     return      length * breadth * height;
 675 }
 676 
 677 void Box_class1::box3_set(double len, double bre, double hei)
 678 {
 679     length  = len;
 680     breadth = bre;
 681     height  = hei;
 682 }
 683 
 684 void Box_class1::setLength(double len)
 685 {
 686     length = len;
 687 }
 688 
 689 void Box_class1::setBreadth(double bre)
 690 {
 691     breadth = bre;
 692 }
 693 
 694 
 695 
 696 void Box_class1::setHeight(double hei)
 697 {
 698     height = hei;
 699 }
 700 
 701 void display_class_object_mode()
 702 {
 703 #if 0
 704     // C++ 类& 对象
 705     // C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计。
 706     // 类是 C++ 的核心特性,通常被称为用户定义的类型。
 707     // 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。
 708     // 类中的数据和方法称为类的成员。函数在一个类中被称为类的成员。
 709     class  classname
 710     {
 711         access    specifiers :    // 访问修饰符:private/public/protected
 712             data    members / variables;    // 变量
 713             member  functions() {}           // 方法
 714     };
 715 
 716     // 数据封装是面向对象编程的一个重要特点,它防止函数直接访问类类型的内部成员。
 717     // 类成员的访问限制是通过在类主体内部对各个区域标记 public、private、protected 
 718     // 来指定的。关键字 public、private、protected 称为访问修饰符。
 719     // 一个类可以有多个 public、protected 或 private 标记区域。
 720     // 每个标记区域在下一个标记区域开始之前或者在遇到类主体结束右括号之前都是有效的。
 721     // 成员和类的默认访问修饰符是 private。
 722     class Base 
 723     {
 724         public:
 725             // 公有成员 
 726             // 公有成员在程序中类的外部是可访问的。
 727         protected:
 728             // 受保护成员
 729             // 私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。
 730             // 只有类和友元函数可以访问私有成员。
 731             // 默认情况下,类的所有成员都是私有的。例如在下面的类中,
 732             // width 是一个私有成员,这意味着,如果您没有使用任何访问修饰符,
 733             // 类的成员将被假定为私有成员:
 734         private:
 735             // 私有成员
 736     };
 737     //protected(受保护)成员
 738     //protected(受保护)成员变量或函数与私有成员十分相似,但有一点不同,protected
 739     //(受保护)成员在派生类(即子类)中是可访问的。在下一个章节中,您将学习到派生类
 740     // 和继承的知识。现在您可以看到下面的实例中,我们从父类 Box 派生了一个子类 smallBox。
 741     // 下面的实例与前面的实例类似,在这里 width 成员可被派生类 smallBox 的任何成员函数访问。
 742 
 743     //继承中的特点
 744     //有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性。
 745     //1.public 继承:基类 public 成员,protected 成员,private 成员的访问属性
 746     //在派生类中分别变成:public, protected, private
 747     //2.protected 继承:基类 public 成员,protected 成员,private 成员的访问属性
 748     //在派生类中分别变成:protected, protected, private
 749     //3.private 继承:基类 public 成员,protected 成员,private 成员的访问属性
 750     //在派生类中分别变成:private, private, private
 751     //但无论哪种继承方式,上面两点都没有改变:
 752     //1.private 成员只能被本类成员(类内)和友元访问,不能被派生类访问;
 753     //2.protected 成员可以被派生类访问。
 754 
 755 
 756 #endif
 757 
 758     Box_class1      Box1;        // 声明 Box1,类型为 Box
 759     Box_class1      Box2;        // 声明 Box2,类型为 Box
 760     Box_class1      Box3;        // 声明 Box3,类型为 Box
 761     Box_class1      Box4;        // pri 私有
 762     SmallBox        Box5;        // pro 受保护 - 派生类
 763     double volume = 0.0;     // 用于存储体积
 764 
 765 
 766     //---------------------------------------------------------------------------12//
 767     // box 1 详述
 768     Box1.height  = 5.0;
 769     Box1.length  = 6.0;
 770     Box1.breadth = 7.0;
 771     // box 2 详述
 772     Box2.height  = 10.0;
 773     Box2.length  = 12.0;
 774     Box2.breadth = 13.0;
 775 
 776     // box 1 的体积
 777     volume = Box1.height * Box1.length * Box1.breadth;
 778     cout << "Box1 的体积:" << volume << "\n" << endl;
 779     // box 2 的体积
 780     volume = Box2.height * Box2.length * Box2.breadth;
 781     cout << "Box2 的体积:" << volume << "\n" << endl;
 782     //---------------------------------------------------------------------------3//
 783     // 
 784     // box 3 详述
 785     Box3.box3_set(16.0, 8.0, 12.0);
 786     volume = Box3.box3_get();
 787     // 需要注意的是,私有的成员和受保护的成员不能使用直接成员访问运算符 (.) 来直接访问。
 788     cout << "Box3 的体积:" << volume << "\n" << endl;
 789 
 790     //---------------------------------------------------------------------------4//
 791     // 不使用成员函数设置长度
 792     Box4.length_pub4 = 444.0; // OK: 因为 length 是公有的
 793     cout << "Length of box4 : " << Box4.length_pub4 << "\n" << endl;
 794 
 795     // 不使用成员函数设置宽度
 796     //Box4.width_pri4 = 10.0; // Error: 因为 width 是私有的
 797      cout << "Width of box4 : " << Box4.getWidth_pub4() << "\n" << endl;
 798      Box4.setWidth_pub4(4442.0);  // 使用成员函数设置宽度
 799      cout << "Width of box4 : " << Box4.getWidth_pub4() << "\n" << endl;
 800 
 801      //---------------------------------------------------------------------------5//
 802      // 使用成员函数设置宽度
 803      Box5.setSmallWidth_pro5(555.0);
 804      cout << "Width of box5 : " << Box5.getSmallWidth_pro5() << "\n" << endl;
 805 
 806 
 807      //---------------------------------------------------------------------------6//
 808      // public 继承
 809      cout << "public 继承*-----------------6----------------* \n" << endl;
 810      Box_class1_B       derive_b(666);
 811      cout << "derive_b.A_pub_a  :" << derive_b.A_pub_a   << "\n" << endl;
 812      cout << "derive_b.B_pub_a  :" << derive_b.B_pub_a   << "\n" << endl;
 813      cout << "derive_b.A_pub_a1:" << derive_b.A_pub_a1   << "\n" << endl;   //正确
 814      //cout <<  derive_b.A_pub_a2 << endl;   //错误,类外不能访问protected成员
 815      //cout <<  derive_b.A_pub_a3<< endl;   //错误,类外不能访问private成员
 816      
 817      //---------------------------------------------------------------------------7//
 818      // protected 继承
 819      cout << "protected 继承*-----------------7----------------* \n" << endl;
 820      Box_class1_B_pro       derive_pro_b(777);
 821      cout << "B_pro_a \t" << derive_pro_b.B_pro_a << "\n"<< endl;       //正确。public成员
 822      //cout << derive_pro_b.a1 << endl;      //错误,protected成员不能在类外访问。
 823      //cout << derive_pro_b.a2 << endl;      //错误,protected成员不能在类外访问。
 824      //cout << derive_pro_b.a3 << endl;      //错误,private成员不能在类外访问。
 825      
 826      //---------------------------------------------------------------------------8//
 827      // private 继承
 828      cout << "protected 继承*-----------------8----------------* \n" << endl;
 829      Box_class1_B_pro       derive_pri_b(8888);
 830      cout << derive_pri_b.B_pro_a << endl;       //正确。public成员
 831      //cout << derive_pri_b.a1 << endl;      //错误,private成员不能在类外访问。
 832      //cout << derive_pri_b.a2 << endl;      //错误, private成员不能在类外访问。
 833      //cout << derive_pri_b.a3 << endl;      //错误,private成员不能在类外访问。
 834      system("pause");
 835 
 836 }
 837 
 838 //--------------------------------------基础--------------------------------------//
 839 void display_struct_data_printBook(struct  Books  book)
 840 {
 841     cout << "\n" << endl;
 842     cout << "书标题 : " <<  book.title    << endl;
 843     cout << "书作者 : " <<  book.author   << endl;
 844     cout << "书类目 : " <<  book.subject  << endl;
 845     cout << "书 ID  : " <<  book.book_id  << endl;
 846     cout << "\n" << endl;
 847 }
 848 
 849 void display_struct_data_printBook2(struct  Books  *book)
 850 {
 851     // 该函数以结构指针作为参数
 852     cout << "\n" << endl;
 853     cout << "书标题  : " << book->title   << endl;
 854     cout << "书作者  : " << book->author  << endl;
 855     cout << "书类目  : " << book->subject << endl;
 856     cout << "书 ID   : " << book->book_id << endl;
 857     cout << "\n" << endl;
 858 }
 859 
 860 void display_struct_data()
 861 {
 862     Books    Book1;        // 定义结构体类型 Books 的变量 Book1
 863     Books    Book2;        // 定义结构体类型 Books 的变量 Book2
 864     Books    Book3;        // 定义结构体类型 Books 的变量 Book3
 865     Books    Book4;        // 定义结构体类型 Books 的变量 Book4
 866     Books    Book5;        // 定义结构体类型 Books 的变量 Book5
 867     Books    Book6;        // 定义结构体类型 Books 的变量 Book6
 868 
 869     //-------------------------------------------------------------12//
 870     // Book1 详述
 871     strcpy(Book1.title, "C++ 教程1");
 872     strcpy(Book1.author, "Runoob1");
 873     strcpy(Book1.subject, "编程语言1");
 874     Book1.book_id = 12345;
 875    
 876     // Book2 详述
 877     strcpy(Book2.title, "CSS 教程2");
 878     strcpy(Book2.author, "Runoob2");
 879     strcpy(Book2.subject, "前端技术2");
 880     Book2.book_id = 12346;
 881     
 882     // 输出 Book1 信息
 883     cout << "第一本书标题 : " << Book1.title << endl;
 884     cout << "第一本书作者 : " << Book1.author << endl;
 885     cout << "第一本书类目 : " << Book1.subject << endl;
 886     cout << "第一本书 ID : " << Book1.book_id << endl;
 887     cout << "\n" << endl;
 888     // 输出 Book2 信息
 889     cout << "第二本书标题 : " << Book2.title << endl;
 890     cout << "第二本书作者 : " << Book2.author << endl;
 891     cout << "第二本书类目 : " << Book2.subject << endl;
 892     cout << "第二本书 ID : " << Book2.book_id << endl;
 893     cout << "\n" << endl;
 894     //-------------------------------------------------------------34//
 895     // Book1 详述
 896     strcpy(Book3.title, "C++ 教程13");
 897     strcpy(Book3.author, "Runoob13");
 898     strcpy(Book3.subject, "编程语言13");
 899     Book3.book_id = 12345;
 900 
 901     // Book2 详述
 902     strcpy(Book4.title, "CSS 教程24");
 903     strcpy(Book4.author, "Runoob24");
 904     strcpy(Book4.subject, "前端技术24");
 905     Book4.book_id = 12346;
 906 
 907     // 输出 Book3 信息
 908     display_struct_data_printBook(Book3);
 909     // 输出 Book4 信息
 910     display_struct_data_printBook(Book4);
 911     //-------------------------------------------------------------56//
 912      // Book1 详述
 913     strcpy(Book1.title, "C++ 教程5");
 914     strcpy(Book1.author, "Runoob5");
 915     strcpy(Book1.subject, "编程语言5");
 916     Book1.book_id = 12345;
 917 
 918     // Book2 详述
 919     strcpy(Book2.title, "CSS 教程6");
 920     strcpy(Book2.author, "Runoob6");
 921     strcpy(Book2.subject, "前端技术6");
 922     Book2.book_id = 12346;
 923 
 924     // 通过传 Book1 的地址来输出 Book1 信息
 925     display_struct_data_printBook2(&Book1);
 926     // 通过传 Book2 的地址来输出 Book2 信息
 927     display_struct_data_printBook2(&Book2);
 928 }
 929 
 930 void diaplay_input_output_data()
 931 {
 932     static char df_str[] = "Hello C++";
 933     // 标准输出流(cout)预定义的对象 cout 是 iostream 类的一个实例。
 934     // cout 对象"连接"到标准输出设备,通常是显示屏。cout 是与流插入运算符 << 结合使用的,
 935     cout << "Value of str is : " << df_str << endl;
 936 
 937     //标准输入流(cin)
 938     // 预定义的对象 cin 是 iostream 类的一个实例。cin 对象附属到标准输入设备,通常是键盘。
 939     // cin 是与流提取运算符 >> 结合使用的,如下所示:
 940     static char df_name[50];
 941 
 942     cout << "请输入您的名称: ";
 943     cin >> df_name;
 944     cout << "您的名称是: " << df_name << endl;
 945 
 946     //标准错误流(cerr)
 947     // 预定义的对象 cerr 是 iostream 类的一个实例。cerr 对象附属到标准输出设备,
 948     // 通常也是显示屏,但是 cerr 对象是非缓冲的,且每个流插入到 cerr 都会立即输出。
 949     //  cerr 也是与流插入运算符 << 结合使用的,如下所示:
 950     char df_str2[] = "Unable to read....";
 951 
 952     cerr << "Error message : " << df_str2 << endl;
 953     //标准日志流(clog)
 954     // 预定义的对象 clog 是 iostream 类的一个实例。clog 对象附属到标准输出设备,
 955     // 通常也是显示屏,但是 clog 对象是缓冲的。这意味着每个流插入到 clog 都会先存储在缓冲区,直到缓冲填满或者缓冲区刷新时才会输出。
 956     // clog 也是与流插入运算符 << 结合使用的,
 957     clog << "Error message : " << df_str2 << endl;
 958 
 959 
 960 }
 961 
 962 void diaplay_time_data()
 963 {
 964 #if 0
 965     struct tm_time {
 966         int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61
 967         int tm_min;   // 分,范围从 0 到 59
 968         int tm_hour;  // 小时,范围从 0 到 23
 969         int tm_mday;  // 一月中的第几天,范围从 1 到 31
 970         int tm_mon;   // 月,范围从 0 到 11
 971         int tm_year;  // 自 1900 年起的年数
 972         int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起
 973         int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
 974         int tm_isdst; // 夏令时
 975 }
 976 #endif
 977     // 基于当前系统的当前日期/时间
 978     time_t now = time(0);
 979 
 980     // 把 now 转换为字符串形式
 981     char *dt = ctime(&now);
 982 
 983     cout << "本地日期和时间:" << dt << endl;
 984 
 985     // 把 now 转换为 tm 结构
 986     tm *gmtm = gmtime(&now);
 987     dt = asctime(gmtm);
 988     cout << "UTC 日期和时间:" << dt << endl;
 989 
 990     // 基于当前系统的当前日期/时间
 991     time_t now2 = time(0);
 992 
 993     cout << "1970 到目前经过秒数:" << now2 << endl;
 994 
 995     tm *ltm = localtime(&now2);
 996 
 997     // 输出 tm 结构的各个组成部分
 998     cout << "年  : " << 1900 + ltm->tm_year << endl;
 999     cout << "月  : " << 1 +    ltm->tm_mon  << endl;
1000     cout << "日  : " <<        ltm->tm_mday << endl;
1001     cout << "时间: " <<        ltm->tm_hour << ":"<< ltm->tm_min << ":"<< ltm->tm_sec << endl;
1002 }
1003 
1004 void display_quote()
1005 {
1006 #if 0
1007     // 引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。
1008     // 一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。
1009 
1010     // C++ 引用 vs 指针:引用很容易与指针混淆,它们之间有三个主要的不同:
1011     // 不存在空引用。引用必须连接到一块合法的内存。
1012     // 一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。
1013     // 引用必须在创建时被初始化。指针可以在任何时间被初始化。
1014 #endif
1015     // 声明简单的变量
1016     static int    we_i;
1017     static double we_d;
1018 
1019     // 声明引用变量
1020     static int&    we_r = we_i;
1021     static double& we_s = we_d;
1022 
1023     we_i = 5;
1024     cout << "Value of i : " << we_i << endl;
1025     cout << "Value of i reference : " << we_r << endl;
1026 
1027     we_d = 11.7;
1028     cout << "Value of d : " << we_d << endl;
1029     cout << "Value of d reference : " << we_s << endl;
1030 }
1031 
1032 int *getRandom()
1033 {
1034     // 要生成和返回随机数的函数
1035     static int  rty_r[10];
1036     srand((unsigned long)time(NULL));                        // 设置种子
1037     for (int indec_E = 0; indec_E < 10; indec_E++ )
1038     {
1039         rty_r[indec_E] = rand();
1040         cout << "\t"<< indec_E << "\t" << rty_r[indec_E] << endl;
1041     }
1042 
1043     return rty_r;
1044 }
1045 
1046 void display_Pointer_func_get()
1047 {
1048     static  int  * rty_p;
1049 
1050     rty_p = getRandom();
1051     for (int indec_F = 0; indec_F < 10; indec_F++)
1052     {
1053         cout << "*(rty_p + " << indec_F << ") : " << *(rty_p + indec_F) << endl;
1054     }
1055 }
1056 
1057 double display_Pointer_getAverage(int* rt_arr, int rt_size)
1058 {
1059     static  int    rt_i, rt_sum = 0;
1060     static  double avg;
1061 
1062     for (rt_i = 0;  rt_i < rt_size;  rt_i++)
1063     {
1064         cout << "\t" << "rt_i: " << "\t" << rt_i <<  endl;
1065         rt_sum += rt_arr[rt_i];
1066         cout << "rt_arr[rt_i]: " << rt_arr[rt_i] << endl;
1067         cout << "rt_sum      : " << rt_sum << endl;
1068 
1069     }
1070     cout << "\n" << endl;
1071     avg = double(rt_sum) / rt_size;
1072     
1073     cout << "Average value is: " << avg << endl;  // 输出返回值
1074 
1075     return avg;
1076 }
1077 
1078 void display_Pointer_opration_getSeconds(unsigned long *par_sec_seconds)
1079 {
1080     *par_sec_seconds = time(NULL); // 获取当前的秒数
1081     cout << "Number of seconds :" << *par_sec_seconds << endl; // 输出实际值
1082     return;
1083 }
1084 
1085 void display_MultPointer_opration()
1086 {
1087     // 指向指针的指针是一种多级间接寻址的形式,或者说是一个指针链。
1088     // 指针的指针就是将指针的地址存放在另一个指针里面。通常,一个指针包含一个变量的地址。
1089     // 当我们定义一个指向指针的指针时,第一个指针包含了第二个指针的地址,第二个指针指向包含实际值的位置。
1090     // 一个指向指针的指针变量必须如下声明,即在变量名前放置两个星号。 int **var;
1091     static  int     wer_var;
1092     static  int     * wer_ptr;
1093     static  int     ** wer_pptr;
1094     wer_var  = 12345;
1095     wer_ptr  = &wer_var;            // 获取 var 的地址
1096     wer_pptr = &wer_ptr;            // 使用运算符 & 获取 ptr 的地址
1097 
1098     // 使用 pptr 获取值
1099     cout << "var    值为:" << wer_var    << &wer_var      << endl;
1100     cout << "*ptr   值为:" << *wer_ptr   << &(*wer_ptr)   << endl;
1101     cout << "**pptr 值为:" << **wer_pptr << &(**wer_pptr) << endl;
1102 }
1103 
1104 void display_pointer_opration()
1105 {
1106     static   int  var1;
1107     static   int  var2[10];
1108     static   int  index_var2;
1109     static   int  *ptr_var2;
1110     static   int  var_real = 20;                        // 实际变量的声明
1111     // 指针变量声明的一般形式为:type *var-name;
1112     static   int  *pointer_statement;                   // 指针变量的声明
1113     static   int  *ptr_statement = NULL;
1114 
1115     const    int   MAX_Q = 10;
1116     static   int   var_array4[MAX_Q] = { 10, 50, 100, 200, 300, 99, 55, 33, 11, 1};
1117     static   int   var_array5[MAX_Q] = { 10, 50, 100, 200, 300, 99, 55, 33, 11, 1};
1118     static   int   *ptr_var_array5[MAX_Q];
1119     static   int   *ptr_state_max;
1120     const    char  *names[MAX_Q] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",};
1121 
1122 
1123 
1124     //1------------------------------------------------------------------
1125     cout << "\n\t" "1" "\t\n" << endl;
1126     cout << "var1 变量的地址: " << &var1 << endl;
1127     cout << "var2 变量的地址: " << &var2 << endl;
1128     ptr_var2 = var2;
1129     for (index_var2 = 0; index_var2 < 10; index_var2++)
1130     {
1131          cout << "var2 数组遍历地址:" << index_var2 << endl;
1132          cout << "Address of var   ["  << index_var2 << "] = " << ptr_var2 << endl;
1133          cout << "Value   of var   ["  << index_var2 << "] = " << *ptr_var2 << endl;
1134          ptr_var2++;
1135     }
1136     
1137     //2------------------------------------------------------------------
1138     cout << "\n\t" "2" "\t\n" << endl;
1139     pointer_statement = &var_real;       
1140     cout << "Value of var variable        : " <<  var_real           << endl; // 在指针变量中存储 var 的地址
1141     cout << "Address stored in ip variable: " <<  pointer_statement  << endl; // 输出在指针变量中存储的地址
1142     cout << "Value of *ip variable        : " << *pointer_statement  << endl;         // 访问指针中地址的值
1143 
1144 
1145     //3------------------------------------------------------------------
1146     // 在变量声明的时候,如果没有确切的地址可以赋值,为指针变量赋一个 NULL 值是一个良好的编程习惯。赋为 NULL 值的指针被称为空指针。
1147     // NULL 指针是一个定义在标准库中的值为零的常量。
1148     cout << "\n\t" "3" "\t\n" << endl;
1149     cout << "ptr 的地址是 " <<  pointer_statement    << endl;
1150     cout << "ptr 的值是 "   << *pointer_statement    << endl;
1151     cout << "\n" << endl;
1152 
1153     //4------------------------------------------------------------------
1154     // 指针中的数组地址
1155     cout << "\n\t" "4" "\t\n" << endl;
1156     ptr_state_max = var_array4;
1157     cout << "\n --递增" << endl;
1158     for (int index_A = 0; index_A < MAX_Q; index_A++)
1159     {
1160         cout << "Address of var[" << index_A << "] = "  <<   ptr_state_max  << endl;
1161         cout << "Value   of var[" << index_A << "] = "  <<  *ptr_state_max  << endl;
1162         ptr_state_max++; // 移动到下一个位置
1163     }
1164     cout << "\n --递减" << endl;
1165     for (int index_A = MAX_Q; index_A > 0; index_A--)
1166     {
1167         ptr_state_max--; // 移动到下一个位置
1168         cout << "Address of var[" << index_A << "] = " << ptr_state_max << endl;
1169         cout << "Value   of var[" << index_A << "] = " << *ptr_state_max << endl;
1170     }
1171     cout << "\n" << endl;
1172 
1173     //5------------------------------------------------------------------
1174     cout << "\n\t" "5" "\t\n" << endl;
1175     for (int index_B = 0; index_B < MAX_Q; index_B++)
1176     {
1177         cout << "Value of var[" << index_B << "] = ";
1178         cout << var_array5[index_B] << endl;
1179     }
1180     cout << "\n" << endl;
1181     
1182     // int* ptr[MAX];
1183     // 把 ptr 声明为一个数组,由 MAX 个整数指针组成。因此,ptr 中的每个元素,都是一个指向
1184     // int 值的指针。下面的实例用到了三个整数,它们将存储在一个指针数组中,*ptr_var_array5
1185     for (int index_C = 0; index_C < MAX_Q; index_C++)
1186     {
1187         ptr_var_array5[index_C] = &var_array5[index_C]; // 赋值为整数的地址
1188         cout << "赋值为整数的地址:Value [ " << index_C << " ] = " << *ptr_var_array5[index_C] << endl;
1189     }
1190     // 指向字符的指针数组来存储一个字符串列表
1191     cout << "\n" << endl;
1192     for (int index_D = 0; index_D < 3; index_D++)  // 目前是3个定义数据
1193     {
1194         cout << "Value of names[" << index_D << "] = " << names[index_D] << endl;
1195     }
1196 } 
1197 
1198 void display_string_opration()
1199 {
1200     static char qwe_site[7]  = { 'R', 'U', 'N', 'O', 'O', 'B', '\0' };
1201     static char qwe_str1[13] = "runoob" ;
1202     static char qwe_str2[13] = "google" ;
1203     static char qwe_str3[23];
1204     static int  qwe_len;
1205 
1206     cout << "菜鸟教程: "<< qwe_site << endl;
1207 
1208     // 复制 str1 到 str3
1209     strcpy(qwe_str3, qwe_str1);
1210     cout << " strcpy( str3, str1) :  " << qwe_str3 << endl;
1211 
1212     // 连接 str1 和 str2
1213      strcat(qwe_str1, qwe_str2);
1214     cout << "strcat( str1, str2): " << qwe_str1 << endl;
1215 
1216     // 连接后,str1 的总长度
1217     qwe_len = strlen(qwe_str1);
1218     //cout << "strlen(str1) : " << qwe_len << endl;
1219 }
1220 
1221 void display_array()
1222 {
1223     int array_N[20]; // n 是一个包含 10 个整数的数组
1224 
1225     // 初始化数组元素          
1226     for (int index_A = 0; index_A < 20; index_A++)
1227     {
1228         array_N[index_A] = index_A*5 + 100; // 设置元素 index_A 为 index_A + 100
1229     }
1230     cout << " Element" << setw(20) << "Value" << endl;
1231 
1232     // 输出数组中每个元素的值                     
1233     for (int index_A = 0; index_A < 20; index_A++)
1234     {
1235         cout << setw(5) << index_A << setw(22) << array_N[index_A] << endl;
1236     }
1237 }
1238 
1239 void display_func(void)
1240 {
1241     static int ASD_Count = 5; // 局部静态变量
1242     ASD_Count++;
1243     std::cout << "变量 ASD_Count 为 " << ASD_Count;
1244     std::cout << " , 变量 ASD_Count 为 " << ZXC_count << std::endl;
1245 }
1246 
1247 void display_const_value()
1248 {
1249     const int  LENGTH_C = 3;
1250     const int  WIDTH_C = 4;
1251     const char NEWLINE_C = '\n';
1252     int AREA_C;
1253 
1254     AREA_C = LENGTH_C * WIDTH_C;
1255     cout << AREA_C;
1256     cout << NEWLINE;
1257 }
1258 
1259 void display_define_value()
1260 {
1261     static int area;
1262 
1263     area = LENGTH * WIDTH;
1264     cout << area <<endl;
1265     cout << NEWLINE <<endl;
1266 }
1267 
1268 void display_string_constant()
1269 {
1270     string greeting = "hello, runoob";
1271     cout << greeting << endl;
1272 
1273     
1274 
1275     string greeting2 ="hello, runoob";
1276     cout << greeting2 <<endl;
1277 }
1278 
1279 void display_calculation()
1280 {
1281     // 变量定义
1282    static int a, b;
1283    static int c;
1284    static float f;
1285 
1286     // 实际初始化
1287     a = 10;
1288     b = 20;
1289     c = a + b;
1290 
1291     cout << c << endl;
1292 
1293     f = 70/ 3;
1294     cout << f << endl;
1295 }
1296 
1297 void display_data_type()
1298 {
1299         cout << "type: \t\t" << "************size**************" << endl;
1300         cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);        // sizeof() 运算符用来获取各种数据类型的大小。
1301         cout << "\t最大值:" << (numeric_limits<bool>::max)();
1302         cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
1303         cout << "char: \t\t" << "所占字节数:" << sizeof(char);
1304         cout << "\t最大值:" << (numeric_limits<char>::max)();
1305         cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
1306         cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
1307         cout << "\t最大值:" << (numeric_limits<signed char>::max)();
1308         cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
1309         cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
1310         cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
1311         cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
1312         cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
1313         cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
1314         cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
1315         cout << "short: \t\t" << "所占字节数:" << sizeof(short);
1316         cout << "\t最大值:" << (numeric_limits<short>::max)();
1317         cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
1318         cout << "int: \t\t" << "所占字节数:" << sizeof(int);
1319         cout << "\t最大值:" << (numeric_limits<int>::max)();
1320         cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
1321         cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
1322         cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
1323         cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
1324         cout << "long: \t\t" << "所占字节数:" << sizeof(long);
1325         cout << "\t最大值:" << (numeric_limits<long>::max)();
1326         cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
1327         cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
1328         cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
1329         cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
1330         cout << "double: \t" << "所占字节数:" << sizeof(double);
1331         cout << "\t最大值:" << (numeric_limits<double>::max)();
1332         cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
1333         cout << "long double: \t" << "所占字节数:" << sizeof(long double);
1334         cout << "\t最大值:" << (numeric_limits<long double>::max)();
1335         cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
1336         cout << "float: \t\t" << "所占字节数:" << sizeof(float);
1337         cout << "\t最大值:" << (numeric_limits<float>::max)();
1338         cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
1339         cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
1340         cout << "\t最大值:" << (numeric_limits<size_t>::max)();
1341         cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
1342         cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
1343         // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
1344         cout << "type: \t\t" << "************size**************" << endl;
1345 }
1346 
1347 int main()
1348 {
1349     //  endl,这将在每一行后插入一个换行符,<< 运算符用于向屏幕传多个值,
1350     cout << "Hello, world!" << endl;  
1351     cout << "\n" << endl;
1352     //display_data_type();
1353     //display_calculation();
1354     //display_string_constant();
1355     //display_define_value();
1356     //display_const_value();
1357     /*  while (ZXC_count--){display_func();}  */
1358     //display_array();
1359     //display_string_opration();
1360     //display_pointer_opration();
1361     //display_MultPointer_opration();
1362     //display_Pointer_opration_getSeconds(&sec_seconds);
1363     //display_Pointer_getAverage(rt_balance,5);
1364     //display_Pointer_func_get();
1365     //display_quote();
1366     //diaplay_time_data();
1367     //diaplay_input_output_data();
1368     //display_struct_data();
1369     //--------------------------------------------------------------------------//
1370     
1371     //display_class_object_mode();
1372     //display_Constructor_destructor();
1373     //display_copy_construction();
1374     //display_inherit();
1375     //display_heavy_load();
1376     //display_polymorphic();
1377 
1378     //
1379     //display_fstream_data();
1380 
1381 
1382 
1383 
1384 
1385 
1386 
1387 
1388 
1389 
1390 
1391     return 0;                          //  终止 main( )函数,并向调用进程返回值 0
1392 }

高级技能后续更新

posted @ 2022-07-26 18:09  楚格  阅读(39)  评论(0编辑  收藏  举报