第十八课:对象的构造(中)----------狄泰软件学院

一、带有参数的构造函数

#include <iostream>
using namespace std;
class Test
{
public:
    Test() 
    { 
        printf("Test()\n");
    }
    Test(int v) 
    { 
        printf("Test(int v), v = %d\n", v);
    }
};

int main()
{
    Test t;      // 调用 Test()
    Test t1(1);  // 调用 Test(int v)
    Test t2 = 2; // 调用 Test(int v)
    system("pause");
    return 0;
}

注意:

1.对象的定义和声明不同

对象的定义:申请对象的空间并且调用构造函数
对象的声明:告诉编译器存在这一一个对象

2.初始化和赋值不同

在c语言里或许没看出太大区别,但在c++中
对象初始化:调用相应的构造函数
对象的复制:调用的是拷贝构造函数

class Test
{
public:
    Test(){}
    Test(int v){}
};
int main()
{
    Test t;//调用Test()
    Test t1(1);//调用Test(int v)
    Test t2 = 1;//调用Test(int v)
    int i = 1;//初始化
    i = 1;//赋值(初始化与赋值是不同的)
    t = t2;//赋值
    int i(100);//i的值初始化为100
    return 0;
}

二、构造函数的调用:

一般情况下,在对象创建时自动调用
一些特殊情况下,需要手工调用

#include <iostream>
using namespace std;
class Test
{
private:
    int m_value;
public:
    Test() 
    { 
        printf("Test()\n");

        m_value = 0;
    }
    Test(int v) 
    { 
        printf("Test(int v), v = %d\n", v);

        m_value = v;
    }
    int getValue()
    {
        return m_value;
    }
};

int main()
{
    Test ta[3] = {Test(), Test(1), Test(2)};   //调用了不同的构造函数,若只是  Test ta[3],就三个对象都只调用Test() 

    for(int i=0; i<3; i++)
    {
        printf("ta[%d].getValue() = %d\n", i , ta[i].getValue());
    }

    Test t = Test(100);

    printf("t.getValue() = %d\n", t.getValue());

    return 0;
}

三、数组类解决原生数组的安全性问题

提供函数获取数组长度
提供函数获取数组元素
提供函数设置数组元素
头文件:

#ifndef _INTARRAY_H_
#define _INTARRAY_H_
#include<iostream>
class Intarray
{
private:
    int m_length;
    int *m_poiter;
public:
    Intarray(int length);
    int get_length();
    bool get_data(int index, int& data);
    bool set_data(int index, int data);
    void free();
};
#endif

arraay.cpp

#include"Intarray.h"
Intarray::Intarray(int length)
{
    m_poiter = new int[length];
    m_length = length;
    for(int i = 0; i < length; i++)
    {
        m_poiter[i] = 0;
    }
}
int Intarray::get_length()
{
    return m_length;
}
bool Intarray::get_data(int index, int& data)
{
    bool ret = (index >= 0) && (index < get_length());
    if(ret)
    {
        data = m_poiter[index];
    }
    return ret;
}
bool Intarray::set_data(int index, int data)
{
    bool ret = (index >= 0) && (index < get_length());
    if(ret)
    {
        m_poiter[index] = data;
    }
    return ret;
}
void Intarray::free()
{
    delete[] m_poiter;
}

main.cpp

#include"Intarray.h"
using namespace std;
int main()
{
    Intarray myarray(5);
    printf("the length is %d\n",myarray.get_length() );
    for(int i = 0; i < myarray.get_length(); i++)
    {
        myarray.set_data(i, i+1);
    }

    for(int i = 0; i < myarray.get_length(); i++)
    {
        int data = 0;
        if(myarray.get_data(i,data))
            printf("the data is %d\n", data);
    }

    myarray.free();
    system("pause");
    return 0;
}

小结:

构造函数可以根据需要定义参数
构造函数之间可以存在重载关系
构造函数遵循c++函数重载规则
对象定义时会触发构造函数的调用
在一些情况下需要手工调用构造函数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值