C++学习之路---构造函数--1

     感觉构造函数像定义的初始化,构造函数是每个函数都有的,如果自己没有去定义的话,系统会给你一个构造函数,析构函数也是这样的,但是他不会给你初始化的值的

一  : 构造函数有这几个特点:

    1. 函数名字和类名相同

    2.没有返回值

    3.要在public权限下

    4.自动调用---(在定义变量的时)

    5.不能被继承

    6.可以重载

    7.默认构造函数只有一个

二: 构造函数的几种不同的赋值方式

 1.直接在函数里面附初值

#include <iostream>

using namespace std;
 class Time
{
    public :
    Time()///构造函数--初始化的过程
    {
        hour = 0;
        minute = 0;
        sec = 0;
    }
    void set_time();
    void show_time();
    private:
    int hour;
    int minute;
    int sec;
};
void Time::set_time()
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}
void Time::show_time()
{
    cout<<hour<<":"<< minute<<":"<<sec<<endl;
}
int main()
{
    Time t1;
    t1.set_time();
    t1.show_time();
    Time t2;
    t2.show_time();
    return 0;
}

2.带参数的构造函数

#include <iostream>

using namespace std;
 class Time
{
    public :
    Time(int h, int m, int s)///构造函数--初始化的过程
    {
        hour = h;
        minute = m;
        sec = s;
    }
    void show_time();
    private:
    int hour;
    int minute;
    int sec;
};
void Time::show_time()
{
    cout<<hour<<":"<< minute<<":"<<sec<<endl;
}
int main()
{
    Time t1(1,2,3);
    t1.show_time();
    Time t2(2,3,4);
    t2.show_time();
    return 0;
}


3.用参数初始化表重载

#include <iostream>

using namespace std;
 class Time
{
    public :
    Time(int h, int m, int s): hour(h),minute(m),sec(s)
    {
    }
    void show_time();
    private:
    int hour;
    int minute;
    int sec;
};
void Time::show_time()
{
    cout<<hour<<":"<< minute<<":"<<sec<<endl;
}
int main()
{
    Time t1(1,2,3);
    t1.show_time();
    Time t2(2,3,4);
    t2.show_time();
    return 0;
}


4.默认参数的构造函数

#include <iostream>

using namespace std;
 class Time
{
    public :
    Time(int h = 1, int m = 1, int s = 1): hour(h),minute(m),sec(s)
    {
    }
    void show_time();
    private:
    int hour;
    int minute;
    int sec;
};
void Time::show_time()
{
    cout<<hour<<":"<< minute<<":"<<sec<<endl;
}
int main()
{
    Time t1(5);
    t1.show_time();
    Time t2(2,3,4);
    t2.show_time();
    return 0;
}

5. 构造函数的重载

#include <iostream>

using namespace std;
 class Time
{
    public :
    Time()
    {
        hour = 1;
        minute = 2;
        sec = 3;
    }
    Time(int h , int m , int s): hour(h),minute(m),sec(s)
    {
    }
    void show_time();
    private:
    int hour;
    int minute;
    int sec;
};
void Time::show_time()
{
    cout<<hour<<":"<< minute<<":"<<sec<<endl;
}
int main()
{
    Time t1;
    t1.show_time();
    Time t2(2,3,4);
    t2.show_time();
    return 0;
}


6.默认构造函数只能有一个

#include <iostream>

using namespace std;///默认构造函数只能拥有一个
 class Time/// 在建立对象时不必给出实参的构造函数为默认构造函数
{
    public :
    Time()
    {
        hour = 1;
        minute = 2;
        sec = 3;
    }
    Time(int h = 1, int m = 1, int s = 1): hour(h),minute(m),sec(s)
    {
    }
    void show_time();
    private:
    int hour;
    int minute;
    int sec;
};
void Time::show_time()
{
    cout<<hour<<":"<< minute<<":"<<sec<<endl;
}
int main()
{
    Time t1;
    t1.show_time();
    Time t2(2,3,4);
    t2.show_time();
    return 0;
}




 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值