根据实例复习Cpp

1. 还是这里开始Cpp

  1. #include <iostream>  
  2. // 一个良好的编程习惯是将using直接跟在include之后  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     int number1;  
  7.     int number2;  
  8.     int sum;  
  9.     cout << "Enter the first number :";  
  10.     cin >> number1;  
  11.     cout << "Enter the second number :";  
  12.     cin >> number2;  
  13.     sum = number1 + number2;  
  14.     // <<流操作符  
  15.     cout << "Sum is " << sum << endl;  
  16.       
  17.     return 0;  
  18. }

2. 定义第一个类

  1. // 定义第一个类  
  2. #include <iostream>  
  3. using namespace std;  
  4. class GradeBook  
  5. {  
  6. public// 访问修饰符  
  7.     // 定义类方法  
  8.     void displayMessage()  
  9.     {  
  10.         cout << "Welcome to GradeBook" << endl;  
  11.     }  
  12. };  // 注意这里的分号  
  13. int main()  
  14. {  
  15.     // 声明GradeBook类,区分这里和c#中需要使用new关键字  
  16.     GradeBook g;  
  17.     g.displayMessage();  
  18.       
  19.     return 0;  
  20. }  

3. 使用string对象

  1. // 使用string对象  
  2. #include <iostream>  
  3. #include <string>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.     // 声明string对象str  
  8.     string str;  
  9.     // 读取一行  
  10.     getline(cin, str);  
  11.     // 输出  
  12.     cout << str;  
  13.       
  14.     return 0;  
  15. }  

4. 类构造函数

  1. // 测试类的构造函数  
  2. //   
  3. // 如果一个类没有提供构造函数的话,编译器将生成一个默认的  
  4. // 构造函数,在该构造函数中将调用类中每个数据程序的默认构造函数  
  5. #include <iostream>  
  6. using namespace std;  
  7. class Student  
  8. {  
  9. public:  
  10.     Student()  
  11.     {  
  12.         cout << "Call Student default constructor\n";  
  13.         this->score = 0.0;  
  14.     }  
  15. private:  
  16.     double score;     
  17. };  
  18. class GradeBook  
  19. {  
  20. public:  
  21.     int getData()  
  22.     {  
  23.         return data;  
  24.     }  
  25. private:  
  26.     Student stu;  
  27.     int data;  
  28. };  
  29. int main()  
  30. {  
  31.     // 在类GradeBook中没有构造函数,那么编译器将生成一个默认构造函数  
  32.     // 将调用Student的默认构造函数,但是data的值是不确定  
  33.     GradeBook g;  
  34.     cout << g.getData() << endl;  
  35.     return 0;  
  36. }  

5.  Cpp中类定义和实现分文件

// Main.cpp

  1. #include <iostream>  
  2. // 包含类定义头文件  
  3. #include "GradeBook.h"  
  4. int main()  
  5. {  
  6.     GradeBook g;  
  7.     g.displayMsg();  
  8.     return 0;  
  9. }  

// GradeBook.h

  1. #ifndef _GRADE_BOOK_H_  
  2. #define _GRADE_BOOK_H_  
  3. // 仅仅是类定义  
  4. class GradeBook  
  5. {  
  6. private:  
  7.     double data;  
  8. public:  
  9.     void displayMsg();  
  10.     GradeBook();  
  11. };  
  12. #endif  

// GradeBook.cpp

  1. #include <iostream>  
  2. using std::cout;  
  3. #include "GradeBook.h"  
  4. // 类实现  
  5. GradeBook::GradeBook()  
  6. {  
  7.     this->data = 0.0;  
  8. }  
  9. void GradeBook::displayMsg()  
  10. {  
  11.     cout << "Welcome to GradeBook !\n";  
  12. }  

6.else摇摆

  1. // 控制结构:else摇摆  
  2. #include <iostream>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     int x = 3,  y =  6;  
  7.     if (x > 5)  
  8.         if (y > 5)  
  9.             cout << "x and y are > 5" << endl;  
  10.         else    // 这里的else匹配的是最近的那个if  
  11.                 // 下面的程序将什么也不输出,因为x < 5  
  12.             cout << "x is <= 5";  
  13.     return 0;  
  14. }  

7. 存储类别,链接和作用域

这里有介绍,但是需要指明的是存储类别和作用域是相互独立的,不是说一个变量V在程序的整个运行期间都存在,并不代表在任何的作用域中该变量均能使用。

8.内联函数

  1. // 内联函数   
  2. #include <iostream>  
  3. using namespace std;  
  4. // 使用管理之inline表明向编译器提出申请将这个  
  5. // 函数内敛  
  6. inline int max(int a, int  b)  
  7. {  
  8.     return (a > b) ? a : b;  
  9. }  
  10. int main()  
  11. {  
  12.     return 0;  
  13. }  

9.  cpp中的引用

  1. // cpp中的引用类型  
  2. #include <iostream>  
  3. using namespace std;  
  4. // 在函数参数中使用引用  
  5. void increase(int& a)  
  6. {  
  7.     a++;  
  8. }  
  9. // 使用const表明的是在该函数中不改变a的值  
  10. void print(const int& a)  
  11. {  
  12.     //  将出现错误 a = 1;  
  13.     cout << a << endl;  
  14. }  
  15. int main()  
  16. {  
  17.     int value = 0;  
  18.     // 声明引用,并赋值  
  19.     int& refValue = value;  
  20.     // refValue和value指向的是同一个对象,所以更改refValue的值将  
  21.     // 更改value的值  
  22.      refValue =  5;  
  23.      cout << "the value is :" << value << endl;  
  24.      int a =  0;  
  25.      // 注意这里的函数调用方式,这里和普通的函数调用是相类似的,  
  26.      // 但是却在函数内部改变了a的值  
  27.      increase(a);  
  28.      cout << "after Increase, a is "   
  29.          << a << endl;  
  30.     return 0;  
  31. }  
 10. 默认实参

  1. // cpp默认实参  
  2. #include <iostream>  
  3. using namespace std;  
  4. // 默认参数,语法的格式是直接在函数的形式参数中写入  
  5. // 默认值  
  6. int boxVolume(int length = 1,   
  7.               int width = 1,   
  8.               int height = 1)  
  9. {  
  10.     return (length * width * height);  
  11. }  
  12. int main()  
  13. {  
  14.     // 含有默认参数的函数的调用  
  15.     cout << "1, 1, 1 is " <<  boxVolume() << endl;  
  16.     cout << "1, 2, 1 is " <<  boxVolume(1, 2) << endl;  
  17.     cout << "1, 2, 3 is " <<  boxVolume(1, 2, 3) << endl;  
  18.     return 0;  
  19. }  
 11.一元作用域分辨运算符

  1. // 一元作用域分辨运算符  
  2. #include <iostream>  
  3. using namespace std;  
  4. // 全局变量  
  5. int num = 10;  
  6. int main()  
  7. {  
  8.     // 局部变量  
  9.      int num = 5;  
  10.      cout << "local is " << num << endl;  
  11.      // 使用::来访问全局num变量  
  12.      cout << "global is " << ::num << endl;  
  13.     return 0;  
  14. }  
 12.函数重载

  1. // 函数重载  
  2. #include <iostream>  
  3. using namespace std;  
  4. // 重载其他比较迷惑特性:默认参数重载,引用类型重载,const类型重载  
  5. // 函数重载:c++在进行函数重载时只是根据函数的参数的  
  6. // 类型来判断,不根据函数的返回值来区别两个函数,因为在  
  7. // 编译器对重载函数进行编译时将根据参数的不同类型重新生成  
  8. // 名字,同时忽略函数的返回值   
  9. // #1  
  10. void overloadFunc()  
  11. {  
  12.     cout << "Call void overloadFunc()" << endl;  
  13. }  
  14. // #2   
  15. void overloadFunc(int)  
  16. {  
  17.     cout << "Call void overloadFunc(int)\n";  
  18. }  
  19. int main()  
  20. {  
  21.     int a = 0;  
  22.     // #2  
  23.     overloadFunc(a);  
  24.     return 0;  
  25. }  
 13. 函数模板

  1. // 函数模板  
  2. #include <iostream>  
  3. using namespace std;  
  4. template <typename T>  
  5. T maxValue(T a,  T b)  
  6. {  
  7.     return (a > b) ? a : b;  
  8. }  
  9. int main()  
  10. {  
  11.     // 注意这里在使用时,不需要指明类型(max<int>)  
  12.     cout << maxValue(1, 2) << endl;  
  13.     cout << maxValue('a''b') << endl;  
  14.     // 这句将产生错误,两个参数的类型不一致  
  15.     //  cout << maxValue('a', 10) << endl;  
  16.         return 0;  
  17. }  
 14. 函数指针数组

  1. // 函数指针数组  
  2. #include <iostream>  
  3. using namespace std;  
  4. void func1()  
  5. {  
  6. }  
  7. void func2()  
  8. {  
  9. }  
  10. void func3()  
  11. {  
  12. }  
  13. int main()  
  14. {  
  15.     // 声明函数指针数组  
  16.     void (*funcPtr[3])();  
  17.     funcPtr[0] = func1;  
  18.     funcPtr[1] = func2;  
  19.     funcPtr[2] = func3;  
  20.     return 0;  
  21. }  
 15. 何时调用构造函数和析构函数

全局变量的初始化是优先于main函数执行,然后开始执行main函数,在main函数中如果遇到自动变量对象,将调用该变量的构造函数,在该变量的作用域完成之后,将指定调用该变量的析构函数,如果是static变量的话,将在main函数结束之后调用析构函数。如果程序中遇到 exit或者是abort的话,将不调用任何对象的析构函数。

  1. // 函数指针数组  
  2. #include <iostream>  
  3. #include <string>  
  4. using std::string;  
  5. using std::cout;  
  6. using std::endl;  
  7. class CreateAndDestroy  
  8. {  
  9. public :  
  10.     CreateAndDestroy(int id, string msg)  
  11.     {  
  12.         m_objectId = id;  
  13.         m_message = msg;  
  14.         cout << "Object : "  << m_objectId   
  15.             << " Constructor runs " << m_message << endl;  
  16.     }  
  17.     ~CreateAndDestroy()  
  18.     {  
  19.         cout << "Object : "  << m_objectId   
  20.             << " Destructor runs " << m_message << endl;  
  21.     }  
  22. private:   
  23.     int m_objectId;  
  24.     string m_message;  
  25. };  
  26. void create()  
  27. {  
  28.     cout << "Create function : executuion  begins" << endl;  
  29.     CreateAndDestroy fifth (5, "local automic in create");  
  30.     static CreateAndDestroy sixth (6, "local static in create");  
  31.     CreateAndDestroy seventh(7, "local automic in create");  
  32.     cout << "Create function : executuion ends" << endl;  
  33. }  
  34. // 全局变量  
  35. CreateAndDestroy fist(1, "global before main");  
  36. int main()  
  37. {  
  38.     cout << "Main function execution begins" << endl;  
  39.     CreateAndDestroy second(2, "local automic in main");  
  40.     static CreateAndDestroy third(3, "local static in main");  
  41.       
  42.     create();  
  43.     cout << "Main function : execution resumes " << endl;  
  44.     CreateAndDestroy fourth(4, "local automic in main");  
  45.     cout << "Main function : execution ends"<< endl;  
  46.       
  47.     return 0;  
  48. }  

 16. const对象和const成员函数

  1. // const成员函数  
  2. #include <iostream>  
  3. #include <string>  
  4. using std::string;  
  5. using std::cout;  
  6. using std::endl;  
  7. // 定义Time类  
  8. class Time  
  9. {  
  10. private :  
  11.     int hour;  
  12.     int minute;  
  13.     int second;  
  14. public:  
  15.     Time(int h = 0, int m = 0, int s = 0)  
  16.     {  
  17.         hour = h;  
  18.         minute = m;  
  19.         second = s;  
  20.     }  
  21.       
  22.     // set方法  
  23.     // const函数,非const对象能够调用const函数,但是const  
  24.     // 对象只能调用 const成员函数  
  25.     void printTime() const  
  26.     {  
  27.         cout << "hour " << hour   
  28.             << " minute " << minute   
  29.             << " second  " << second   
  30.             << endl;  
  31.     }  
  32. };  
  33. int main()  
  34. {  
  35.     // 声明const对象  
  36.     const Time t(1,  1, 1);  
  37.     t.printTime();  
  38.     Time t2;  
  39.     t2.printTime();   
  40.     return 0;  
  41. }  

 17. const产量初始化

  1. // const产量初始化  
  2. #include <iostream>  
  3. #include <string>  
  4. using std::string;  
  5. using std::cout;  
  6. using std::endl;  
  7. // 定义Time类  
  8. class Time  
  9. {  
  10. private :  
  11.     const int CONSTANT;  
  12. public:  
  13.     // const常量初始化语法,如果是const static产量的话,直接在定义处初始化  
  14.     Time() : CONSTANT(0)  
  15.     {  
  16.     }  
  17. };  
  18. int main()  
  19. {  
  20.     Time t;  
  21.     return 0;  
  22. }  

 18. 友元函数

  1. // 友元函数  
  2. #include <iostream>  
  3. #include <string>  
  4. using std::string;  
  5. using std::cout;  
  6. using std::endl;  
  7. // 定义Time类  
  8. class Time  
  9. {  
  10. private :  
  11.     int hour;  
  12.     int minute;  
  13.     int second;  
  14. public:  
  15.     Time(int h = 0 , int m =  0, int  s =  0)  
  16.     {  
  17.         hour  = h;  
  18.         minute = m;  
  19.         second = s;  
  20.     }  
  21.     // 定义friend函数。实际上友元函数可以在class的任何位置定义  
  22.     // 因为友元函数实际上不是该类的成员函数  
  23.     friend Time add(const Time& t1, const Time& t2);  
  24. };  
  25. // 定义有缘函数,这里不需要使用friend  
  26. Time add(const Time& t1, const Time& t2)  
  27. {  
  28.     return Time(t1.hour + t2.hour,  
  29.         t1.minute + t2.minute,  
  30.         t1.second  + t2.second);  
  31. }  
  32. int main()  
  33. {  
  34.     Time t1(1, 1, 1);  
  35.     Time t2(2, 2, 2);  
  36.     Time t3 = add(t1, t2);  
  37.     return 0;  
  38. }  
 19. 类中的static成员和static函数

  1. // 类中的static成员和static函数  
  2. #include <iostream>  
  3. #include <string>  
  4. using std::string;  
  5. using std::cout;  
  6. using std::endl;  
  7. // 定义Time类  
  8. class Time  
  9. {  
  10. private :  
  11.     int hour;  
  12.     int minute;  
  13.     int second;  
  14.     // 定义static成员变量  
  15.     static int instanceCounts;  
  16. public:  
  17.     Time(int h = 0 , int m =  0, int  s =  0)  
  18.     {  
  19.         hour  = h;  
  20.         minute = m;  
  21.         second = s;  
  22.         // 使用类的静态变量  
  23.         Time::instanceCounts++;  
  24.     }  
  25.       
  26.     // 定义static成员函数  
  27.     static int getInstanceCount()  
  28.     {  
  29.         return instanceCounts;  
  30.     }  
  31. };  
  32. // 虽然static成员默认已经进行了初始化为0,但是如果不添加这初始化的话  
  33. // 在该文件中将找不到该变量  
  34. int Time::instanceCounts = 0;       // 文件作用域  
  35. int main()  
  36. {  
  37.     Time t1(1, 1, 1);  
  38.     Time t2(2, 2, 2);  
  39.       
  40.     // 使用类的静态方法  
  41.     cout << "Time instance count is " << Time::getInstanceCount() << endl;  
  42.     // 调用类的static方法  
  43.       
  44.     return 0;  
  45. }  
 20. 运算符重载

运算符重载仅仅是在简化客户端的程序的编程,可以直接调用运算函数:t1.operator==(t2);.

  1. // 运算符重载   
  2. #include <iostream>  
  3. #include <string>  
  4. using std::string;  
  5. using std::cout;  
  6. using std::endl;  
  7. // 定义Time类  
  8. class Time  
  9. {  
  10. private :  
  11.     int hour;  
  12.     int minute;  
  13.     int second;  
  14. public:  
  15.     Time(int h = 0, int m =  0, int s =  0)  
  16.     {  
  17.         hour  = h;  
  18.         minute = m;  
  19.         second = s;  
  20.     }  
  21.     // 使用默认构造函数   
  22.     // 运算符重载==  
  23.     bool operator== (const Time& other)  
  24.     {  
  25.         return (hour == other.hour) &&  
  26.             (minute ==other.minute) &&  
  27.             (second  == other.second);  
  28.     }  
  29.     // !=   
  30.     bool operator!= (const Time& other)  
  31.     {  
  32.         return !(*this == other);  
  33.     }  
  34.     // +  
  35.     Time operator+(const Time& other)  
  36.     {  
  37.         return Time(hour + other.hour,   
  38.             minute + other.minute,   
  39.             second + other.second);  
  40.     }  
  41.     // -  
  42.     Time operator-(const Time& other)  
  43.     {  
  44.         int hour = hour - other.hour;  
  45.         int minute = minute - other.minute;  
  46.         int sec = second - other.second;  
  47.         return Time(hour, second, sec);  
  48.     }  
  49.     // *  
  50.     Time operator*(int n)  
  51.     {  
  52.         return Time(hour * n,  
  53.             minute * n,  
  54.             second  * n);  
  55.     }  
  56.     // 打印信息  
  57.     void printTime()  
  58.     {  
  59.         cout << "Hour : " << hour   
  60.             << " Minute : " << minute   
  61.             << " Second : " << second  
  62.             << endl;  
  63.     }  
  64. };  
  65. int main()  
  66. {  
  67.     Time t1(1, 1, 1);  
  68.     Time t2(2, 2, 2);  
  69.       
  70.     Time t3 = t1 + t2;  
  71.     t3.printTime();  
  72.       
  73.     if (t1 == t2)  
  74.         cout << "t1 == t2" << endl;  
  75.     else  
  76.         cout << "t1 != t2" << endl;  
  77.     return 0;  
  78. }  
 21. 类型转换函数

在c++中如果构造函数可以用作类型转换函数(如果想要禁止的话,可以使用关键字explicit禁止将该构造函数用作默认的类型转换函数),也可以指定以类的类型转换函数。

  1. // 类型转换函数,这里仅仅是为了演示,没有实际意义  
  2.     // 注意这里的函数格式,没有返回值  
  3.     operator int()  
  4.     {  
  5.         return 1;  
  6.     }  
 22. 重载++运算符

由于在c++中存在a++和++a的类型,所以编译器需要使用一个所谓的“哑元素”来区分是a++还是++a。

  1. // 定义++运算符  
  2.     // ++a形式  
  3.     Time& operator++ ()  
  4.     {  
  5.         second++;  
  6.         return (*this);  
  7.     }  
  8.     // a++形式  
  9.     Time operator++(int)  
  10.     {  
  11.         Time tmp = *this;  
  12.         second++;  
  13.         return tmp;  
  14.     }  
 

 

23.类的继承属性

c++中存在三种类型的继承属性,如果不明确知名的话,默认的是private继承。不管是何种类型的继承,子类都是不能访问父类的private成员的,只是private,public,protected继承对于父类的public成员在子类中的行为是不相同的。同时需要注意的是:

1. 构造函数是不能够被继承的

2. 如果子类重写了父类的某个方法 ,但是还想调用覆盖的父类方法时,可以使用父类名::函数的形式调用

24 virtual关键字

  1. // 父类  
  2. class BaseClass  
  3. {  
  4. public:  
  5.     BaseClass()  
  6.     {  
  7.          cout << "base class constructor. " << endl;  
  8.     }  
  9.     ~BaseClass()  
  10.     {  
  11.         cout << "baseclass destructor" << endl;  
  12.     }  
  13.     virtual void toOverrideFunc()  
  14.     {  
  15.         cout << "override function in baseclass." << endl;  
  16.     }  
  17. };  
  18. // 这里使用的是public继承  
  19. class Subclass : public BaseClass  
  20. {  
  21. public:  
  22.     Subclass()  
  23.     {  
  24.         // 这里将首先调用BaseClass的构造函数  
  25.         cout  << "subclass constructor. " << endl;  
  26.     }  
  27.     ~Subclass()  
  28.     {  
  29.         // 这里首先调用该类的析构函数,然后是父类的析构函数  
  30.         cout << "subclass destructor." << endl;  
  31.     }  
  32.     // 这里仅隐藏父类的该函数toOverrideFunc  
  33.     virtual void toOverrideFunc()  
  34.     {  
  35.         cout << "override function in subclass"  << endl;  
  36.     }  
  37. };  
  38. int main()  
  39. {  
  40.     // 没有使用virtual的话,那么函数调用将取决于调用的句柄  
  41.     // 如果使用了irtual的话,那么函数调用的将取决于实际的对象类型  
  42.     BaseClass* basePtr = new Subclass();  
  43.     // 这里将调用subclass中的函数  
  44.     basePtr->toOverrideFunc();  
  45.       
  46.     Subclass* subPtr = new Subclass();  
  47.     subPtr->toOverrideFunc();  
  48.     delete basePtr;  
  49.     delete subPtr;  
  50.       
  51.     return 0;  
  52. }  
 25. 抽象类

c++中如果一个类想要成为抽象类的话,只需要将类中的一个成员函数声明为纯虚函数,纯虚函数是不能够有时显得,但是虚函数是能够有函数的时显的。

  1. // 纯虚函数  
  2. #include <iostream>  
  3. #include <string>  
  4. using std::string;  
  5. using std::cout;  
  6. using std::endl;  
  7. using std::ostream;  
  8. using std::istream;  
  9. using std::cin;  
  10. // 父类,抽象类  
  11. class BaseClass  
  12. {  
  13. public:  
  14.     // 纯虚函数  
  15.     virtual void pureVirtualFunc()= 0;  
  16. };  
  17. // 这里使用的是public继承  
  18. class Subclass : public BaseClass  
  19. {  
  20. public:  
  21.     virtual void pureVirtualFunc()  
  22.     {  
  23.         cout << "in the subclass, we override the purevirtualFunc()"  
  24.             << endl;  
  25.     }  
  26. };  
  27. int main()  
  28. {  
  29.     // 抽象类是无法实例化的  
  30.     // BaseClass b;     // 错误  
  31.     BaseClass* basePtr = new Subclass();  
  32.     basePtr->pureVirtualFunc();  
  33.     delete basePtr;  
  34.     return 0;  
  35. }  

完结

posted @   qiang.xu  阅读(409)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示