Problem 4: 需要重载吗?

Problem F: 需要重载吗?

Description
定义一个类Overload,具有一个int类型的和一个char类型属性。在定义该类的对象时,如果不指定任何初始值,则输出:
Default constructor is called to make a = 0, c = ‘0’.
如果只给出一个int类型的初始值#,则输出:
First constructor is called to make a = #, c = ‘0’.
如果只给出一个char类型初始值 , 则 输 出 : S e c o n d c o n s t r u c t o r i s c a l l e d t o m a k e a = 0 , c = ′ ,则输出: Second constructor is called to make a = 0, c = ' Secondconstructoriscalledtomakea=0,c=’.
如果给出一个int类型初始值#和一个char类型初始值KaTeX parse error: Expected 'EOF', got '#' at position 47: …ed to make a = #̲, c = '’.

Input
一个int类型的值和一个char类型的值。

Output
见样例。

Sample Input
10 a
Sample Output
Default constructor is called to make a = 0, c = ‘0’.
First constructor is called to make a = 10, c = ‘0’.
Second constructor is called to make a = 0, c = ‘a’.
Third constructor is called to make a = 10, c = ‘a’.

#include <bits/stdc++.h>

using namespace std;

class Overload
{
public:
    int n;
    char c;
    Overload(int n1):n(n1),c('0')
    {
        cout<<"First constructor is called to make a = ";
        cout<<n<<", c = '";
        cout<<c;
        cout<<"'.";
        cout<<endl;
    }
    Overload(char c1):n(0),c(c1)
    {
        cout<<"Second constructor is called to make a = ";
        cout<<n<<", c = '";
        cout<<c;
        cout<<"'.";
        cout<<endl;
    }
    Overload(int n1,char c1):n(n1),c(c1)
    {
        cout<<"Third constructor is called to make a = ";
        cout<<n<<", c = '";
        cout<<c;
        cout<<"'.";
        cout<<endl;

    }
    Overload(  ):n(0),c('0')
    {
        cout<<"Default constructor is called to make a = ";
        cout<<n<<", c = '";
        cout<<c;
        cout<<"'.";
        cout<<endl;
    }
};

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int i;
    char ch;
    cin>>i>>ch;
    Overload t1, t2(i), t3(ch), t4(i, ch);
    return 0;
}

2020/05/23

蛇皮大大

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值