C++笔记

来源:互联网 发布:spss数据分析实例报告 编辑:程序博客网 时间:2024/05/16 18:22


const必须初始化,const double *point指向常量的指针 const *double point常指针
常对象:成员函数不一定是常,数据成员是常   const MyClass myclass
    只能调用常成员函数
常成员函数:不能修改数据成员  void getX const(){}
    可被非常对象调用
常数据成员:只能用初始化列表   private:const int data


静态函数:没有this指针,不能是虚函数   static void getX(){}
静态数据成员:类外赋值,单独存储,MyClass::sta   private:static int sta


纯虚函数:包含纯虚函数的是抽象类
虚函数:以继承为基础,派生类不是非得重新定义,若定义也是虚函数,virtual可省略
    定义虚函数,看指针里内容类型而不是指针本身
    保证父类指针调用子类对象(父类是指针/引用,函数是虚函数缺一不可)   java中均可以
虚继承(虚基类):以菱形继承为继承,避免第一基类重复调用构造函数
    由远派生类进入,先调用虚基类构造函数,继承声明顺序调用其他基类(不再调用虚基类),
继承:符合同名隐藏原则,父类同名函数都被隐藏(包括重载)   用子类调用时
    子给父(类),内存小给内存大(数据类型)


动态删数组  delete[] array;  删指针 delete p


模板
    函数模板定义:template<typename T>                   //-----结尾无分号
                 ret-type fun-name(){}
    类模板定义:template<class type>                     //-----结尾无分号
               class class-name{};   
    类外定义函数时:template<typename T>             //-----结尾无分号
                   ret-type class-name<T>::fun-name(){}   
    定义对象时:class-name<type> name
    类模板使数据成员和成员函数的参数和返回值取任意数据类型


类型:
    有无符号修饰char,int及int的长短   不改变位   默认有符号
    长短修饰int,修饰时int可省略   例外long double
    float是4 double是8 long double是8
    enum枚举变量   enum enum-name {} var-list 


向量库vector         vector vec;
    vector<元素类型>name(length,初值)
    vec.size()  提取大小
    vec.push_back(i)  向后追加元素



头文件:ios  istream一般 fstream文件 sstream字符串        iostream                  -----头文件
ios:ios_base ios                                                              -----类
istream:istream输出 ostream输入 iostream出入 streambuf流缓冲区                -----类
fstream:ifstream出 ofstream人 fstream出入 fielbuf流缓冲区                     -----类
sstream:istringstream出 ostringstream人 stringstream出入 stringbuf流缓冲区    -----类
iostream:cin cout cerr clog                                                   -----对象
    width()函数,cout.width(10) 10字宽,右对齐补空格,作用一个
    fill()函数,cout.fill('*')  指定填充字符,作用所有
    put()函数,cout.put('A') 精确输出字符,不受宽度填充影响
fstream文件:ofstream创建文件并写入信息 ifstream读取信息 fstream两种功能
    open函数:void open (const char *filename,[ios::openmode mode])              -----成员函数
        打开模式:ios::app追加 ios::ate定位至文件末尾 ios::in读取 ios::out写入 ios::trunc文件存在则清空其内容
    close函数:void close()                                                    -----成员函数
    write函数(out流):void write(chat *strname,sizeof(strname))
       也可直接fileObject<<"";也可实现向文件内写入内容(覆盖模式)
    read函数(in流):void read(name,sizeof(name))内容输出到name里
    文件位置指针:istream的seekg和ostream的seekp   fileObject.seekg(n,mode)
         ios:beg开头 ios::cur当前位置 ios::end末尾  
iomanip文件     在<<内使用
    setw设置域宽,缺省左边加空格,作用一个,即使用分号隔开也可作用
    setprecision(n)设置浮点精度,显示最多n位,没有填充,作用所有
    setfill(ch)用ch填充空白,作用所有
    showpoint
    可以直接用right\left设置对齐格式,作用所有


重载:
    成员函数:Box operator+(const Box&)
             Box & operator++()   前置
             Box operator++(int) 后置
    非成员函数:Box operator+(const Box&,const Box&)
               Box & operator++(Box)   前置
               Box operator++(Box,int) 后置  int与前置相区别
    流返回值:声明为友元 friend ostream &operator<< (ostream &output,const Box&)
        friend istream &operator >>(istream &input,Box&)
    = () [] ->不能重载为类的友元函数
    . .*成员指针访问 ::域 ?: sizeof 不能重载


字符数组:
    strlen(str1)测长度
    strcmp(str1,str2)等则返回0,str1<str2返回-1,str1>str2返回1    strncmp(str1,str2,n)
    strcat(str1,str2)连接并赋值给str1   strncat(str1,str2,n)
    strcpy(str1,str2)赋值str2给str1    strncpy(str1,str2,n)
    strchr(str1,ch)查找并返回string,strspn(str1,str2) 
    strrev()颠倒字符串


string类:
    getline(cin,str,'。');







































































































原创粉丝点击