C++学习笔记

来源:互联网 发布:电话客服软件 编辑:程序博客网 时间:2024/06/03 13:46

static关键字

using namespace std;class Dog{public:    static int Dogs;    int num;    static int GetDOg()//**静态成员函数只能访问静态成员变量**    {        cout << Dogs << endl;        return Dogs;    }    void setDog(int Dogs)    {        this->Dogs = Dogs;     }private:};int Dog::Dogs = 0;//this is very import必须赋值,build不报错,但是新建一个对象会报错void main(){    Dog d1, d2;    d1.GetDOg();    d1.setDog(20);    d1.GetDOg();    d2.GetDOg();    system("pause");}

运算符重载

1.用成员函数和友元函数实现(一般<<和>>用友元,其他的最好用成员函数)

#include<iostream>using namespace std;class Complex{public:    Complex(int a, int b)    {        this->a = a;        this->b = b;    }    Complex operator+(Complex &c2)//成员函数实现    {        Complex tmp(a+c2.a,b+c2.b);        return tmp;    }    void get()    {        cout << a << "+" << b << "i" << endl;    }    friend Complex operator-(Complex &c2, Complex &c1);//友元函数实现private:    int a;    int b;};Complex operator-(Complex &c2, Complex &c1){    Complex tmp(c2.a-c1.a,c2.b-c1.b);    return tmp;}void main(){    Complex c1(1,2), c2(3,4);    Complex c3 = c1 + c2;    c3.get();    Complex c4 = c2 - c1;    c4.get();    system("pause");}

2.“++”的前置运算符和后置运算符重载(难点)

#include<iostream>#include<math.h>using namespace std;class Complex{public:    int a;    int b;public:    Complex(int a,int b)    {        this->a = a;        this->b = b;    }    void get()    {        cout << "a:" << a << " " << "b:" << b << endl;    }    Complex operator++()//c1++    {        a++;        b++;        return *this;    }    friend Complex operator++(Complex &c, int);};Complex operator++(Complex &c, int){    Complex tmp=c;    ++c.a;    ++c.b;    return tmp;}void main(){    Complex c1(1, 1), c2(2, 3);    c1.get();    Complex c3 = c1++;    c1.get();    c3.get();    Complex c4 = ++c2;    c2.get();    c4.get();    //a++ 先参与运算,再自增    //++a先自增,再参与运算    system("pause");}/*a : 1 b : 1a : 2 b : 2a : 1 b : 1a : 3 b : 4a : 3 b : 4*/

3.矩阵乘法

Matrix operator*(Matrix x, Matrix y){    if (x.col != y.row){ cerr << "m1 cannot * with m2" << endl; }    Matrix tmp(x.row,y.col);    for (int i = 0; i < x.row; i++)    {        for (int j = 0; j < x.col; j++)        {            tmp.matrix[i][j] = 0;            for (int k = 0; k < x.col;k++)            {                tmp.matrix[i][j] = x.matrix[i][k] * y.matrix[k][j] + tmp.matrix[i][j];            }            cout << "tmp.matrix[" << i << "][" << j << "]" << "=" << tmp.matrix[i][j] << endl;        }    }    return tmp;}

4.虚函数的应用场景

#include<iostream>#include<math.h>using namespace std;class Form{public:    virtual void form() = 0;//有纯虚函数,不能创建对象    virtual double area(){ return 0; }    virtual double volum(){ return 0; }};class Point :public Form{public:    Point(int x,int y)    {        this->x = x;        this->y = y;    }    virtual void form(){cout<<"Point"<<endl;}    virtual double area(){ cout << 0 << endl; return 0; }    virtual double volum(){ cout << 0 << endl; return 0; }protected:    int x;    int y;};class Circle :public Point{public:    Circle(int x, int y, int r) :Point(x, y)    {        this->r = r;    }    virtual void form(){ cout << "Circle" << endl; }    virtual double area(){ cout << 3.14*r*r << endl; return 3.14*r*r; }    virtual double volum(){ cout << 0 << endl; return 0; }protected:    double r;};class Cylinder :public Circle{public:    Cylinder(int x, int y, int r, int h) :Circle(x, y, r)    {        this->h = h;    }    virtual void form(){ cout << "Cylinder" << endl; }    virtual double area(){ cout << 3.14*r*r * 2 + 2 * 3.14*r*h << endl; return 3.14*r*r * 2 + 2 * 3.14*r*h; }    virtual double volum(){ cout << 3.14*r*r*h << endl; return  3.14*r*r*h; }private:    double h;};void main(){    Form *p;    Point p1(1, 2);    Circle c1(1,2,1);    Cylinder c(1, 1, 1, 2);    p = &p1;    p->form();    p->area();    p->volum();    p = &c1;    p->form();    p->area();    p->volum();    p = &c;    p->form();    p->area();    p->volum();    system("pause");}
0 0
原创粉丝点击