• c++ 构造函数


    有关构造函数的使用,有以下说明:
      (1) 在类对象进入其作用域时调用构造函数。
      (2) 构造函数没有返回值,因此也不需要在定义构造函数时声明类型,这是它和一般函数的一个重要的不同之点。
      (3) 构造函数不需用户调用,也不能被用户调用。
      (4) 在构造函数的函数体中不仅可以对数据成员赋初值,而且可以包含其他语句。但是一般不提倡在构造函数中加入与初始化无关的内容,以保持程序的清晰。
      (5) 如果用户自己没有定义构造函数,则C++系统会自动生成一个构造函数,只是这个构造函数的函数体是空的,也没有参数,不执行初始化操作。

      (6)构造函数可以定义到类的外部。

    1、计算长方形的体积:

      

    #include <iostream>
    using namespace std;
    class Box
    {public:
      Box(int,int,int);        //声明带参数的构造函数
      int volume( );            //声明计算体积的函数
     private:
      int height;
      int width;
      int length;
    };
    Box∷Box(int h,int w,int len)   //在类外定义带参数的构造函数
    {  height=h;
      width=w;
      length=len;
    }
    int int Box∷volume( )                //定义计算体积的函数
    {
      return(height*width*length); } int main( ) {
      Box box1(
    12,25,30); //建立对象box1,并指定box1长、宽、高的值   cout<<″The volume of box1 is ″<<box1.volume( )<<endl;   Box box2(15,30,21); //建立对象box2,并指定box2长、宽、高的值   cout<<″The volume of box2 is ″<<box2.volume( )<<endl;   return 0; } 程序运行结果如下: The volume of box1 is 9000 The volume of box2 is 9450

    2、构造函数初始化: 

    #include <iostream>
    using namespace std;
    class Box
    {  public:
        Box( );                                    //声明一个无参的构造函数
        Box(int h,int w,int len):height(h),width(w),length(len){ }
    //声明一个有参的构造函数,用参数的初始化表对数据成员初始化
      int volume( );
      private:
        int height;
        int width;
        int length;
    };
    Box∷Box( )                                   //定义一个无参的构造函数
    {
      height=10;   width=10;   length=10; } int int Box∷volume( ) {
      
    return(height*width*length); } int main( ) {   Box box1; //建立对象box1,不指定实参   cout<<″The volume of box1 is ″<<box1.volume( )<<endl;   Box box2(15,30,25); //建立对象box2,指定3个实参   cout<<″The volume of box2 is ″<<box2.volume( )<<endl;   return 0; } /* 在本程序中定义了两个重载的构造函数,其实还可 以定义其他重载构造函数,其原型声明可以为 Box∷Box(int h); //有1个参数的构造函数 Box∷Box(int h,int w); //有两个参数的构造函数 在建立对象时分别给定1个参数和2个参数。 说明: (1) 调用构造函数时不必给出实参的构造函数,称为默认构造函数(default constructor)。显然,无参的构造函数属于默认构造函数。一个类只能有一个默认构造函数。 (2) 如果在建立对象时选用的是无参构造函数,应注意正确书写定义对象的语句。 (3) 尽管在一个类中可以包含多个构造函数,但是对于每一个对象来说,建立对象时只执行其中一个构造函数,并非每个构造函数都被执行。
    */

    3、默认参数的构造函数:

      

    #include <iostream>
    using namespace std;
    class Box
    {
    public:   Box(int h=10,int w=10,int len=10); //在声明构造函数时指定默认参数   int volume( ); private:   int height;   int width;   int length; }; Box∷Box(int h,int w,int len) //在定义函数时可以不指定默认参数 {
      height=h;   width=w;   length=len; } int int Box∷volume( ) {
      return(height*width*length); } int main( ) {   Box box1; //没有给实参   cout<<″The volume of box1 is ″<<box1.volume( )<<endl;   Box box2(15); //只给定一个实参   cout<<″The volume of box2 is ″<<box2.volume( )<<endl;   Box box3(15,30); //只给定2个实参   cout<<″The volume of box3 is ″<<box3.volume( )<<endl;   Box box4(15,30,20); //给定3个实参   cout<<″The volume of box4 is ″<<box4.volume( )<<endl;   return 0; } /* 程序运行结果为   The volume of box1 is 1000   The volume of box2 is 1500   The volume of box3 is 4500   The volume of box4 is 9000   程序中对构造函数的定义(第12~16行)也可以改写成 参数初始化表的形式:   Box∷Box(int h,int w,int len):height(h),width(w),length(len){ } 说明:   (1) 应该在声明构造函数时指定默认值,而不能只在定义构造函数时指定默认值。   (2) 程序第5行在声明构造函数时,形参名可以省略。   (3) 如果构造函数的全部参数都指定了默认值,则在定义对象时可以给一个或几个实参,也可以不给出实参。   (4) 在一个类中定义了全部是默认参数的构造函数后,不能再定义重载构造函数。 */
  • 相关阅读:
    安卓开发学习笔记(七):仿写腾讯QQ登录注册界面
    android studio 撤销和恢复
    安卓开发学习笔记(六):如何实现指定图片定时开屏功能?
    JAVA小白开发环境配置(编译器为Idea)
    我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
    安卓开发学习笔记(五):史上最简单且华丽地实现Android Stutio当中Webview控件https/http协议的方法
    安卓开发学习笔记(四):Android Stuidio无法实现隐式Intent是为什么?
    XML如何添加注释?
    安卓开发学习笔记(三):Android Stuidio无法引用Intent来创建对象,出现cannot resolve xxx
    安卓开发学习笔记(二):如何用Android Stuidio在res资源下创建xml视图文件
  • 原文地址:https://www.cnblogs.com/lbangel/p/3272438.html
Copyright © 2020-2023  润新知