c++学习笔记十一

 类


  1 封装
    继承
    多态


  2 类的定义:
      class Box{
 //定义常量
          public:
               double length;
               double width;
               double height;
          //方法
          double value(){
              return length*width*height;
           }
      
}
   3 构造函数  示例代码如下:
       class Box{
 //定义常量
          public:
               double length;
               double width;
               double height;
//constructor 构造器
Box(double lengthValue,double widthValue,double heightValue){
            cout<<"constructor is called !"<<endl;
     length=lengthValue;
              width=widthValue;
     height=heightValue;
 }
          //方法
          double value(){
              return length*width*height;
           }
      
}   
    构造函数的定义放在类的外部
         一、在一个头文件中定义一个Box类  代码如下Box.h:
             #ifndef BOX_H
             #define BOX_H
             class Box{
                 public:
                     double length;
                     double width;
                     double height;
                 Box(double lengthValue,double widthValue,double 


lengthValue);
                 double value();
              };
             #endif
         二、函数的定义放到.cpp文件中 Box.cpp
             #include <iostream>
             #include "Box.h"       //引入头文件
             using std::cout;
             using std::endl;
             //定义构造成器
             Box:: Box(double lengthValue,double widthValue,double 


lengthValue)    {
length=lengthValue;
              width=widthValue;
      height=heightValue;
                 }  
//方法
             double  Box:: value(){
                  return length*width*height;
                }       
              默认的构造函数 如果没有定义构造成器, 编译器会自动生成一个
              默认的初始化值 (在定义有参数的构造成函数后,可以去掉默认的构


造器)
              在构造器中使用初始化列表   示例代码如下:
                Box:: Box(double lengthValue,double widthValue,double 


lengthValue):length(lengthValue),width(widthValue),height(lengthValue){
                  //called
               }
              使用explicit关键字(类的构造器只有一个参数是非常危险的) 可以辟


免隐式类型转换
                class Cube{
                    public:
                        couble side;
                          
                         Cube(double side);
                         double value();
                         bool compareValue(Cube aCube);    
                 }    
                       
     4 类的私有成员  成员变量用private 定义
         访问私有类成员 (添加get方法)
                class Box{
   //构造器
                    Box(..);      
   double value();
                    //get方法 
                    double getLength(){return length;}
                    double getWidth(){return width;}
                    double getHeight(){return height;}                   
                  }
          访问对象的尺寸  pBox->getlength();
          默认的副本构造函数  
            定义一个  Box pBox=(1.2,2.3,3.4);对象,如果想创建一个跟pBox一要


的对象,可以使用        Box pBox2=(pBox);
     5  友元  可以访问类对象的任意成员
        类的友元函数 关键字friend
          
        友元类
          class Box{
              //常量
              private:
                  int parm1;
                  int parm2
              //友元类
              friend class Box2;
              //成员方法   
           }
      6 this 指针
        显示使用this指针
           示例代码如下:
                   double Box::value(){
                          return this->length*this->width*this.height;
                      }
         从函数中返回this   
            示例代码如下(Box.h文件):
              #ifndef BOX_H
             #define BOX_H
             class Box{
                 private :
                     double length;
                     double width;
                     double height;
Box* setLength(double len);
Box* setWidth(double wid);
Box* setHeight(double hei);


                 Box(double lengthValue,double widthValue,double 


lengthValue);
                 double value();
              };
             #endif
             (Box.cpp文件)


              #include <iostream>
             #include "Box.h"       //引入头文件
             using std::cout;
             using std::endl;
             //定义构造成器
             Box:: Box(double lengthValue,double widthValue,double 


lengthValue)    {
length=lengthValue;
              width=widthValue;
      height=heightValue;
                 } 
//实现set方法
Box* Box:: setLength(double len){
                     if(len>0) length=len;
                     return this;
}
      Box* Box:: setWidth(double wid){
                     if(wid>0) width=wid;
                     return this;
}
Box* Box:: setHeight(double hei){
                     if(hei>0) height=len;
                     return this;
}
//方法
             double  Box:: value(){
                  return length*width*height;
                }       
            
        7 const对象和const成员函数  
            double compareVlume(const Box &otherBox);//由于参数不能修改,所


以设置为const类型 
            (访问器函数,仅访问数据成员的值,而不去修改他们)
              示例代码如下(Box.h头文件):
                 #ifndef BOX_H
             #define BOX_H
             class Box{
                 private :
                     double length;
                     double width;
                     double height;
 


                 Box(double lengthValue,double widthValue,double 


lengthValue);
                 double comparevalue() const;// const成员函数
              };
             #endif
             在.cpp中的定义为
              doublc Box::conparevalue() const{
                   return length*width*height;
               }
               
           * 类中的mutalbe成员
               示例代码 .h头文件如下:
              class   secureAccess(){
                   //成员函数
                   public :
                      bool isLocked() const;
                   //成员变量
  private :
                      mutable int time;
               }
             .cpp实现代码如下:


                bool SecureAccess::isLocked() const{
                        time=getCurrentTime;//取当前时间给time重新赋值
       return lockStatus();//返回当前门的状态
                 }


               调用该方法:
                  const SecureAccess mainDoor();
                  bool dorState=mainDoor.isLocked();
                
             *常量的强制转换   
                格式如下:const_cast<类型>(表达式);//表达示必需是const类型


或与类型相同
              
       8 类的对象数组
           示例代码.h的头文件如下:
             class Box{
               //构造器
               Box();
               Box(double lengthValue,double widthValue,double 


heightValue);

               //方法
               double volume() const;//体积
               int compareVolume() const;//比较体积
               //成员变量
                private :
                   double lenght;
                   double width;
                   double height;
             }
             .cpp实现代码:
              如果只实现默认的构造成器,则构造函数只有一个
                 Box::Box(){
                    cout<<"defaule constructor iscalled!";
                    length=width=height=1.0;
                 }
            
               创建和使用Box数组
               #inclued <iostream>
               #inclued "Box.h"
               
                using std::cout;
                using std::endl;
                int main(){
                  cout<<endl;
                  
                //初始化成员变量
                 Box firstBox(17.0,11.0,5.0);  
                 Box boxes[5];
                 
                 //计算第一个盒子的体积
                 cout<<"volume of first box="
                     <<firstBox.volume()
                     <<endl; 
                 //数组中盒子的数量
                 const int count=sizeof boxes/sizeof boxes[0];  // 用数组的


总大小除以类对象的大小  得出数组的成员个数
                 couc<<"the boxes array has "
                     <<count
                     <<"element";
                 
                 //输出成员信息
                 for(int i=0;i<count;i++){
                    cout<<"box["
                        <<i
                        <<"]"
                        <<box[i].volume
                        <<endl;                    
                 }
                    return 0;
                } 
                
        9 类对象的大小
            受边界对齐影响的对象大小  示例代码.h的头文件如下:
            #idndef SIZEBOX_H
            #define SIZEBOX_H
            
             class SizeBox{
                  public:
                     SizeBox();
                     int totalSize();
                  private: 
                     char* pMaterial; 
                     double length;
                     double width;
                     double height;
              };
             #endif;
           .cpp代码实现部分
            #inclued "SizeBox.h"
            //初始化列表
            SizeBox::SizeBox():length(1.0),width(1.0),height


(1.0),pMaterial("Cardboard"){};   
            //取成员大小
           SizeBox::int totalSize() {
                return sizeof(length)+sizeof(width)+sizeof(height)+sizeof


(pMaterial);
            } 
            .cpp实现部分代码如下:
            #include <iostream>
            #include "SizeBox.h"
          
            using std::cout:
            using std::endl;
            
            int main(){
               //定义常量
               SizeBox sizeBox; 
               SizeBox boxes[10];


               //输出SizeBox 占用内存大小
                cout<<endl
                    <<""
                    <<sizeBox.totalSize;
               //SizeBox对象的大小
                cout<<endl
                    <<sizeof SizeBox;
             // 对象数组的大小
                cout<<endl
                    <<sizeof boxes;
                
                return 0;
               }
           
    类的静态成员     关键字static
      示例代码
           在Box中定义一个静态变量
               static int num;
           如果在类的外部对其引用
               int Box:: num=0;
       静态成员的类型:
           如在头文件Box.h中声明 const staice Box reBox;
           在.cpp实现文件中 const static Box Box::reBox(1.0,1.0,1.0); 
       类的静态成员函数    (不能声明为const 静态成员函数与类的对象无关,它没


有this指针,所以不能使用const)
            示例代码如下:
              class Box{
                 //构造函数
                 public:
                  Box(); 
                  Box(double lengthValue,double widthValue,double 


heightValue);
             
                 //方法
                 double volume() const;
                 int compareVolume(const Box & otherBox) const;
                 //静态成员函数
                 static int getObjectCount(){
                       return objectCount;
                 }
                 
                 //成员变量
                 private:
                    static int objectCount;
                    double length;
                    double width;
                    double height;
              }         
        静态成员函数的调用:
              Box:: getObjectCount();//取得对象数
posted @ 2012-09-20 23:20  retacn_yue  阅读(171)  评论(0编辑  收藏  举报