C++类的构造函数问题

C++的类构造器和Java中的构造器的使用方法有着巨大的区别。一个从Java转到C++中来的程序员确实会有一下的疑问。

疑惑在于默认构造器和无参构造器的使用。看一下的代码:

#include <iostream>


using namespace std;


class Box{

    public :

        Box(){height = 10; width = 10; length = 10;}

        Box(int h,int w,int l):height(h),width(w),length(l){}

        int volume();

    private :

        int height;

        int width;

        int length;


};



int Box::volume()

{

    return (height*width*length);

}


int main()

{

    Box box1;

    cout << "The volume of box1 is " << box1.volume() << endl;

    Box box(3,5,2);

    cout << "The volume of box is " << box.volume() << endl;

    return 0;

}

注意标红部分的代码:Box box1;就没有加(),这个时候代表的是调用无参构造器。

再看:

#include <iostream>


using namespace std;


class Box{

    public :

        Box(){height = 10; width = 10; length = 10;}

        Box(int h,int w,int l):height(h),width(w),length(l){}

        int volume();

    private :

        int height;

        int width;

        int length;


};



int Box::volume()

{

    return (height*width*length);

}


int main()

{

    Box box1();

    cout << "The volume of box1 is " << box1.volume() << endl;

    Box box(3,5,2);

    cout << "The volume of box is " << box.volume() << endl;

    return 0;

}

把上面的代码在标红部分加上()后原封不动进行编译,产生这样的错误:

[Daniel.Lee@localhost  class]$ g++ structFunc.cpp 

structFunc.cpp: In function ‘int main()’:

structFunc.cpp:26: error: request for member ‘volume’ in ‘box1’, which is of non-class type ‘Box()’

这里的26行就是cout部分,虽然在这里肯定会出错,因为源代码本意是调用默认构造器,但是由于默认构造器不会对成员数据进行初始化,所以cout一定会出错。。。这里出错的原因就是这个!

这样:

#include <iostream>


using namespace std;


class Box{

    public :

        Box(){height = 10; width = 10; length = 10;}

        Box(int h,int w,int l):height(h),width(w),length(l){}

        int volume();

    private :

        int height;

        int width;

        int length;


};



int Box::volume()

{

    return (height*width*length);

}


int main()

{

    Box box1();

 //   cout << "The volume of box1 is " << box1.volume() << endl;

    Box box(3,5,2);

    cout << "The volume of box is " << box.volume() << endl;

    return 0;

}

就能够编译通过了!

总结下:

#include <iostream>


using namespace std;


class Box{

    public :

        Box(){height = 10; width = 10; length = 10;}

        Box(int h,int w,int l):height(h),width(w),length(l){}

        int volume();

    private :

        int height;

        int width;

        int length;


};



int Box::volume()

{

    return (height*width*length);

}


int main()

{

    Box box1;

    cout << "The volume of box1 is " << box1.volume() << endl;

    Box box2();

    Box box(3,5,2);

    cout << "The volume of box is " << box.volume() << endl;

    return 0;

}

标红部分调用的是自己定义的无参构造函数。

标绿部分是调用默认构造函数。

标蓝部分是调用自定义的有参构造函数。

转载于:https://my.oschina.net/DanielLee/blog/208754

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值