zoukankan      html  css  js  c++  java
  • c++总结系列之期中版

    C++期中总结笔记

    1.C++不仅支持面向对象,也可以像C一样支持面向过程.

    2.OOP三大特性:封装 继承 多态

    3.函数重载依据:函数类型and形参个数,返回类型不能作为依据

    4.常成员函数是指通过函数获得成员的值,不一定用const修饰返回值.

    5.引用就是取别名

    6.名称空间防止命名冲突

    7.private修饰函数和成员

    8.inline内联函数

    9.构造函数只负责初始化,不负责分配对象占用内存空间

    10.构造函数能够重载,析构函数没有形参.

    11.所有成员默认访问属性 private

    12.引用类的目的:提供一个机制,实现从现实世界到程序世界的映射 提供代码重用性 数据封装

    13.定义类 访问属性: 定义结束;

    14.指针无论类型4字节

    15.this指针是数据区中的数据和代码区中的函数连接的纽带,this指针作用于作用域的类内部 this指针作为非静态成员函数的隐含形参,编译器自动添加。

    //举个栗子
     
    dataSetMonth(9);
    //等价于
    SetMonth(&date,9);
    //返回对象类本身
    return *this;
    //当参数与成员名相同时,不能用n=n,需用this->n=n;
     
    //this指针实例:
    class Point
    {
        int x,y;
    public:
        Point(int a,int b)
        {
            x=a;
            y=b;
        }
        void MovePoint(int a,int b)
        {
            x+=a;
            y+=b;
        }
        void print()
        {
            cout<<"x="<<endl;
        }
    };
     
    int main()
    {
        Point point1(10,10);
        point1.MovePoint(2,2);
        point1.print();
    }
    //MovePoint函数中,x+=a <=> this->x+=a;x+=b <=> this->x+=b;
    

    类的this指针的特点

    (1) this只能在成员函数中使用 全局函数、静态函数都不能使用this 成员函数默认第一个参数为T* const this

    //举个栗子
    class A
    {
    public:
        int func(int p)
        {
        }
    };
    

    编译器认为是int func(A *const this,int p);

    2)this指针在成员函数开始前构造,在成员函数结束后清除.生命周期是同任何一个参数是一样的,没有区别.

    //举个栗子
     
    #include <iostream>
    using namespace std;
     
    class CExample
    {
    private:
        int a;
    public:
        CExample(int b)
        {
            a=b;
        }
        CExample(const CExample& C)
        {
            a=C.a;
        }
        void show()
        {
            cout<<a<<endl;
        }  
    };
     
    int main()
    {
        CExample A(100);
        CExample B=A;
        B.show();
        return 0;
    }
     
    /*CExample(const CExample& C)就是我们定义的拷贝构造函数.拷贝构造函数就是一种特殊的构造函数,函数的名称必须和类名称一致,必须的一个参数是本类型的引用变量.*/
    

    17.圆形类的实现

    #include <cmath>
    #include "circle.h"
     
    Circle::Circle(double aX,double aY,double aR):m_iX(aX),m_iY(aY),m_iRadius(aR){}
    Circle::~Circle(){}
    void Circle::setPos(double aX,double aY)
    {
        m_iX=aX;
        m_iY=aY;
    }
    void Circle::getPos(double &rX,double &rY)const
    {
        rX=m_iX;
        rY=m_iY;
    }
    double Circle::getRadius()const
    {
        return m_iRadius;
    }
    double Circle::getDist(const Circle & aRef)const
    {
        double x1,y1,x2,y2;
        this->aRef.getPos(x1,y1);
        double r=pow(x1-x2,2);
        r += pow(y1-y2,2);
        return sqrt(r);
    }
    bool Circle::isInter(const Circle & aRef)const
    {
        double min_dist=this->getRadius()+aRef.getRadius();
        double dist =this->getDist(aRef);
        if(min_dist>=dist)
        {
            return true;
        }
        return false;
    }
    

    18.动态数组类的实现

    #ifndef ARRAY_H
    #define ARRAY_H
    #include <iostream>
    class Array
    {
    public:
        Array(const int aCount=10);
        Array(const Array &aArray);
        ~Array();
        void setCount(const int aCount);
        int getValue(const int aIndex)const;
        void setValue(const int aIndex,const int aValue);
    private:
        int m_iCount,*m_pArray;       
    };
    #endif
    Array:Array(const int aCount):m_iCount(aCount),m_pArray(NULL)
    {
        if(aCount<=0)
        {
            return false;
        }
        m_pArray=new int[aCount]; 
    }
    Array:Array(const Array &aArray):m_iCount(aArray.m_iCount),m_pArray(NULL)
    {
        if(aArray.m_iCount<=0)
        {
            return false;
        }
        this->m_pArray=new int[aArray.m_iCount];
        for(int I=0;i<m_iCount;i++)
        {
            m_pArray[i]=aArray.m_pArray[i];
        }
    }
    Array::~aArray()
    {
        delete []m_pArray;
    }
    void Array::setCount(const int aCount)
    {
        if(!aCount)
        {
            return false;
        }
        int min_count=(aCount<m_iCount)?aCount:m_iCount;
        m_iCount=aCount;
        int *nArray=new int[aCount];
        if(min_count)
        {
            memcpy(nArray,m_pArray,min_count*sizeof(int));
        }
        delete []m_pArray;
        m_pArray=nArray;
    }
     
    int Array::getValue(const int aIndex)const
    {
        return m_pArray[aIndex];
    }
    void Array::setValue(const int aIndex,const int aValue)
    {
        if(!m_pArray)
        {
            return false;
        }
        if(aIndex>=m_iCount)
        {
            return false;
        }
        m_pArray[aIndex]=aValue;
    }
    

    19.复数类的实现

    Complex::Complex(double aReal,double aImag)
    {
        m_dImag=aImag;
        m_dReal=aReal;
    }
    Complex::~Complex(){}
    double Complex::GetReal()const
    {
        return m_dReal;
    }
    double Complex::GetImag()const
    {
        return m_dImag;
    }
    Complex Complex::Add(Complex &aRef)const
    {
        Complex result;
        result.m_dImag=m_dImag+aRef.m_dImag;
        result.m_dReal=m_dReal+aRef.m_dReal;
        return result;
    }
    

    20.字符串类的实现

    #include <iostream>
    using namespace std;
    class String{
    public:
        String(const char* str=NULL);
        String(const String &other);
        ~String()
        {
            delete []m_data;
        }
        void display()const;
    private:
        char *m_data; 
    };
    String::String(const char* str)
    {
        if(!str)
        {
            m_data=0;
        }
        else
        {
            m_data=new char[strlen(str)+1];
            strcpy(m_data,str);
        }
    }
     
    String::String(const String & other)
    {
        if(this!=&other)
        {
            m_data=new char[strlen(other.m_data)+1];
            strcpy(m_data,other.m_data);
        }
    }
    void String::display()const
    {
        cout<<m_data<<endl;
    }
    

    21.友元类 友元关系不能被继承 单向不具备交换性 不具有传递性

    22.指针与const 划线法 const 与 指针关系 左变指向 右变数据 双双冻结

    23.构造函数是特殊的成员函数,重点注意与类同名、无返回类型、有形参表(可为空)、函数体.

    一个类可以有多个构造函数,但形参数目或类型不能均同.形参指定了初始化式.

    24.显示定义一个构造函数:没有形参的默认构造函数.默认构造函数,无显示初始化式.

    //举个栗子
    vector<int> vi; //无元素向量对象
     
    string s; //空字符串
     
    Sales_item item; //空的Sales_item对象
    

    25.构造函数Public,不可以Private.否则不能初始化对象.

    26.函数重载是为了用一个函数名实现不同函数功能.

    27.函数重载和重复声明有区别.形参表和返回类型一致,就近采用,单一声明.同表不同型,第一正确.

  • 相关阅读:
    【原创】Zend Framework快速开发(二)使用命令完成项目
    分析CMMS系统笔记使用js控制快捷键
    学习笔记——php利用@来抑制错误
    WINDOWS + WAMP + Zend Framework 配置
    PHP的$_SERVER变量笔记
    【原创】Zend Framework快速开发(一)zend框架的配置和项目创建(原创)
    带符号的8位2进制数最小值为多少?
    动态规划笔记
    我一直在拖国家的后退
    手机客户端和服务器通信时如何安全高效的进行身份验证
  • 原文地址:https://www.cnblogs.com/xzmds/p/5122460.html
Copyright © 2011-2022 走看看