C++篇:C++学习笔记01

学习C++参考书目:《C++编程思想》第1卷:标准C++导引

学习C++参考链接:http://www.prglab.com/

学习C++参考视频:华文慕课-->程序设计实习   

C++学习笔记01:

代码范例部分来自《C++编程思想》,还有一部分来自上述参考链接,特此声明。另外,如有错误还请大家批评指正。

  1. 整行输入
    #include<string>
    string mystr;
    getline(cin,mystr);
  2. 字符串相加
    #include<string>
    string s1="I am";
    string s2("a boy") ;
    string s3,s4;
    s3=s1+" "+s2;
    s4+="8";
  3. 给字符数组赋值
    #include<string> 
    char szMyName [20];
    strcpy (szMyName,"J. Soulie");
    //或者: 
    char szMyName [20];
    setstring (szMyName,"J. Soulie"); 
    //或者: 
    char mybuffer [100];
    cin.getline (mybuffer,100);
    
  4. 字符串相加
    #include<string>
    #include<sstream>
    int a=0;
    string mystr;
    getline(cin,mystr); 
    stringstream(mystr) >> a;
    "1024"->1024 
    
  5. go to语句
    lable:
    ......
    goto lable;
  6. exit函数
    exit(0);//正常退出
    exit(其他值);//异常退出
    
  7. 函数重载
    int divide (int a, int b) {
    return (a/b);
    }
    float divide (float a, float b) {
    return (a/b);
    }
    int divide (int a){
    return a/3;
    }
    扫描二维码关注公众号,回复: 4919012 查看本文章
  8. 内联函数
    (1)成员函数:写在类里面
    (2)全局函数:前面加上inline
    
  9. 函数参数默认值
    void divide(int a,int b=2);
    divide(1);//当只有一个参数时,默认为divide(1,2);
    divide(1,3);
    
  10. 数组作为参数传递
    int a[10];
    //直接传输组
    void b(int x[])
    {
    	cout<<x[0] ;
    }
    b(a);
    //或者用指针
    void c(int* x)
    {
    	cout<<*x;
     } 
     c(a);
    
  11. 引用传递
    int a=0;
    void b(int & x)
    {
     	x++;
    } 
    b(a);//此时a=1;
    
  12. 动态内存分配
    int *a;
    a=new int;
    delete a;
    //如果创建一个数组
    a=new int [5];
    delete []a; 
    //或者
    int *a=new int[5];
    delete []a; 
    //将数组初始化为0
    int *a=new int[5]();
    delete []a; 
    
  13. 动态对象创建
    MyClass *a=new MyClass;//Myclass为类,a为对象
    //调用构造函数
    MyClass *a=new Myclass(1);
    delete a;
    //创建对象数组
    MyClass *a=new Myclass[100];
    delete []a;
    
  14. 构造函数和析构函数
    class A
    {
    	int a,b;
    	A();//默认构造函数
    	A(int,int);//自己创建的构造函数	
    	A(const A&); //复制(拷贝)构造函数
    	~A();//析构函数
     } ;
     A::A(int aa,int bb)
     {
     	a=aa;
     	b=bb;
     }
    A::A(const A& rv)
    {
    	a=rv.a;
    	b=rv.b;
    }
    A::~A()
    {
    	delete a;
    	delete b;
    } 
    //可以将默认构造函数和自己创建的构造函数写成一个
    A::A(int aa=0,int bb=0)//默认a=0,b=0;
     {
     	a=aa;
    	b=bb;
      } 
    //也可以使用构造函数初始化列表
    A::A(int aa,int bb):a(aa),b(bb){
     } 
    
    
    
  15. 操作符重载
    class CVector {
          public:
            int x,y;
            CVector () {};
            CVector (int,int);
            CVector operator + (CVector);//加号运算符重载
        }; 
    CVector CVector::operator+ (CVector param) {
            CVector temp;
            temp.x = x + param.x;
            temp.y = y + param.y;
            return (temp);
        }
    //二元运算符在类内一个参数,在类外两个参数
    CVector operator+(CVector pa,Cvector pb)
    {
    	CVector temp;
    	temp.x=pa.x+pb.x;
    	temp.y=pa.y+pb.y;
    	return(temp);
     } 
    
  16. 类的继承
    class A
    {
    	
    };
    class B:public A
    {
    //可以定义新的变量和函数; 
    //需要定义新的构造函数和析构函数;	 
    } 
    //理论上说,子类(drived class)继承了基类(base class)的所有成员,除了:
        //构造函数Constructor 和析构函数destructor
        //operator=() 成员
        //friends  
    //继承里面注意  隐藏实现 
    //继承初始化:
    B::B(int i):A(i)
    {
    	
    } 
    
  17. 多态、虚函数、抽象基类
    //关键字virtual 的作用就是在当使用基类的指针的时候,使子类中与基类同名的成员(函数)在适当的时候被//调用。
    class CPolygon {
            virtual int area (void) { return (0); }
    };
    CPolygon * ppoly1 = &rect;//rect为CPolygon子类对象
    CPolygon * ppoly2 = &trgl;//trgl为CPolygon子类对象
    cout << ppoly1->area() << endl;//调用rect.area()
    cout << ppoly2->area() << endl;//调用trgl.area()
    
  18. 函数模板
    template <class T> 
    T GetMax (T a, T b) 
    {
        T result;
        result = (a>b)? a : b;
        return (result);
    } 
        int i=5, j=6, k;
        long l=10, m=5, n;
        k=GetMax<int,int>(i,j);
        n=GetMax<long int,long int>(l,m);
    //也可以用几种不同类型
    template <class T,class U>
    T GetMin (T a, U b) { return (a<b?a:b); }
    //或者
    template <class T, int N> 
    class array {
        T memblock [N];
    };
    
  19. 类模板
    template <class T> 
    class pair 
    {
         T value1, value2;
         pair(T a,T b)
         {
         	value1=a;
         	value2=b;
    	 }
    }
    pair<int> a(1,2);
    pair<float> b(1.0,2.0);    
    //在类外定义函数时,每定义一个,前面要加一句 template <class T>
    
  20. 文件输入输出
    #include<ofstream>
    ofstream file;
    file.open("1.txt")//1.txt的后面可以加好多参数
    void a(const char* filename)
    {
    	ofstream file;
    	file.open(filename);
    }
     bool is_open(); //判断是否打开 
     file.close(); //关闭文件
     file.eof(); //判断是否到达文件末尾,返回true/false
    //还有几个检测当前指针和改变当前指针位置函数
    tellg() 和 tellp()
    seekg() 和 seekp()
    

猜你喜欢

转载自blog.csdn.net/qq_36163358/article/details/83751327
今日推荐