c++构造函数与拷贝构造函数,组合类构造函数

9 篇文章 0 订阅

1、如果没有显示声明构造函数时,系统给生成构造函数,此时随机赋值,并不是0。若后面定义对象时有参数会报错。

#include<iostream>

using namespace std;

class A
{
 private:
    int a;
 public:
    inline int get_a()
    {
        return a;
    }
};

int main()
{
    A a;
    cout<<a.get_a()<<endl;
}

2、如果已经显示声明构造函数时,则系统不再生成无参数构造函数。此时如果定义对象时没有参数,则报错。

#include<iostream>

using namespace std;

class A
{
 private:
    int a;
 public:
     A(int ap){
         a=ap;
    }
     inline int get_a()
    {
        return a;
    }
};

int main()
{
    A a(0);//若为A a;则报错
    cout<<a.get_a()<<endl;
}

3、如果有默认形参,则当定义对象时没有参数也不会报错。

 

#include<iostream>

using namespace std;

class A
{
 private:
    int a;
 public:
     A(int ap=5){
         a=ap;
    }
     inline int get_a()
    {
        return a;
    }
};

int main()
{
    A a;
    cout<<a.get_a()<<endl;
}

4、构造函数中的参数名称如果和类的成员变量重合,则会覆盖类的成员变量,使得类无法正确赋值.

#include<iostream>
#include<stdio.h>

using namespace std;



class B
{
private:
    int b;
public:
    B(int b);
    int get_b()
    {
        return b;
    }
};

B::B(int b)
{
    b=b;
}

int main()
{

    B b(7);
    cout<<b.get_b();
}
//输出错误

这个问题有两种解决办法:

#include<iostream>
#include<stdio.h>

using namespace std;



class B
{
private:
    int b;
public:
    B(int b);
    int get_b()
    {
        return b;
    }
};

B::B(int bp)//在此处修改形参名字
{
    b=bp;
}

int main()
{

    B b(7);
    cout<<b.get_b();
}

#include<iostream>
#include<stdio.h>

using namespace std;



class B
{
private:
    int b;
public:
    B(int b);
    int get_b()
    {
        return b;
    }
};

B::B(int b)
{
    this->b=b;//在此处增加this指针
}

int main()
{

    B b(7);
    cout<<b.get_b();
}

当然直接使用不会产生歧义的命名最好。

 

5、不定义拷贝构造,利用系统给定可以执行

#include<iostream>
#include<stdio.h>

using namespace std;

class A
{
 private:
    int a;
 public:
    A(int ap=5){
         a=ap;
    }

    inline int get_a()
    {
        return a;
    }
};

int main()
{
    A a(9);
    A a1=a;
    cout<<a.get_a()<<endl;
    printf("%d",a1.get_a());
}

6、定义拷贝构造函数时也要注意不要将构造函数的形参名和类的私有成员名重合,否则也会造成歧义覆盖错误。

#include<iostream>
#include<stdio.h>

using namespace std;

class A
{
 private:
    int a;
 public:
    A(int ap=5){
         a=ap;
    }
    A(A &a){
        a=a;//这里歧义错误,改为a=a.a仍然错误,改为a=an.a正确,即不能重名
    }
    inline int get_a()
    {
        return a;
    }
};

int main()
{
    A a(9);
    A a1=a;
    cout<<a.get_a()<<endl;
    printf("%d",a1.get_a());
}

7,组合类构造函数:先说先得,先人后己

#include<iostream>

using namespace std;

class A
{

   private:
       int a;
   public:
       A();
       A(int a);
       ~A();
       int get(){return a;};
};

A::A()
{
    cout<<"A的无参数构造函数"<<endl;
};

A::A(int a)
{
    this->a=a;
    cout<<"A的有参数构造函数"<<endl;
};

A::~A()
{
    cout<<"A的析构函数"<<endl;

}



class B
{
    private:
        A a_inb;
        int b;
    public:
        B();
        B(int b);
        B(int a,int b);
        ~B();
        int get_a(){return a_inb.get();};
        int get_b(){return b;};
};

B::B()
{
    cout<<"B的无参数构造函数"<<endl;
};

B::B(int b):a_inb(b)
{
    cout<<"B的一个参数调用函数1"<<endl;
};

B::B(int bp)
{
    b=bp;
    cout<<"B的一个参数调用函数2"<<endl;
};

B::B(int a,int b):a_inb(a)
{
    this->b=b;
    cout<<"B的两个参数调用函数"<<endl;
};

B::~B()
{
    cout<<"B的析构函数"<<endl;
}

class C
{
private:

    B b;A a;//与这里的调用顺序一致。
public:
    inline C(){
        cout<<"C的无参构造函数"<<endl;
    }
    inline C(int a):b(a)
    {
        cout<<"C的单参数构造函数"<<endl;
    }
    inline C(int a,int b):b(a),a(b)//调用顺序与这里的实现初始化列表没有任何关系
    {
        cout<<"C的双参数构造函数"<<endl;
    }
    inline C(int a,int b,int bb):a(b),b(a,bb)
    {
        cout<<"C的三参数构造函数"<<endl;
    }
    inline int get(){return a.get();}
    inline int get_b(){return b.get_b();}
    ~C()
    {
        cout<<"C的析构函数"<<endl;
    }
    inline int get(){return a.get();}

};



int main()
{
   A a;
   A a1(0);
   B b;
   B b1(1);
   int n;
   n=a1.get();
   cout<<"a1:"<<n<<endl;
   B b2(2,3);

cout<<"b1: "<<b1.get_a()<<"  "<<b1.get_b()<<endl;
cout<<"b2: "<<b2.get_a()<<"  "<<b2.get_b()<<endl;
C d;
C c(6);
C f(8,9);
C s(2,3,4);

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值