【C++】 18_对象的构造 (中)

发布时间:2019-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 18_对象的构造 (中)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

构造函数

带有参数的构造函数

  • 构造函数可以根据需要定义参数
  • 一个类中可以存在多个重载的构造函数
  • 构造函数的重载遵循 C++ 重载规则
class test
{
public:
    Test(int v)
    {
        // use v to inITialize member.
    }
};

友情提示

  • 对象定义和对象声明不同

    • 对象定义 - 申请对象的空间并调用构造函数
    • 对象声明 - 告诉编译器存在这样一个对象
Test t;             // 定义对象并调用构造函数

int main()
{
    extern Test t;  // 告诉编译器存在名为 t 的 Test 对象
    
    return 0;
}

构造函数的自动调用

class Test
{
public:
    Test() { };
    Test(int v) { };
};

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

编程实验: 带参数的构造函数

#include <stdio.h>

class Test
{
public:
    Test() 
    { 
        printf("Test()n");
    };
    
    Test(int v) 
    { 
        printf("Test(int v), v = %dn", v);
    };
};

int main()
{
    Test t;      // 调用 Test()
    Test t1(1);  // 调用 Test(int v)
    Test t2 = 2; // 调用 Test(int v)
    
    int i(100);
    
    printf("i = %dn", i);
    
    return 0;
}
输出:
Test()
Test(int v), v = 1
Test(int v), v = 2
i = 100

构造函数的调用

  • 一般情况下,构造函数在定义对象时被自动调用
  • 一些特殊情况下,需要手工调用构造函数

如何创建一个对象数组

编程实验: 构造函数的手动调用

#include <stdio.h>

class Test
{
private:
    int m_value;

public:
    Test() 
    { 
        printf("Test()n");
        
        m_value = 0;
    };
    
    Test(int v) 
    { 
        printf("Test(int v), v = %dn", v);
        
        m_value = v;
    };
    
    int getValue()
    {
        return m_value;
    }
};

int main()
{
    Test ta[3] = {Test(), Test(1), Test(2)};
    
    for(int i=0; i<3; i++)
    {
        printf("ta[%d].getValue = %dn", i, ta[i].getValue());
    }
    
    Test t = Test(100);
    
    printf("t.getValue() = %dn", t.getValue());
    
    return 0;
}
输出:
Test()
Test(int v), v = 1
Test(int v), v = 2
ta[0].getValue = 0
ta[1].getValue = 1
ta[2].getValue = 2
Test(int v), v = 100
t.getValue() = 100

小实例

  • 需求: 开发一个数组类解决原生数组的安全性问题
  • 提供函数获取数组长度
  • 提供函数获取数组元素
  • 提供函数设置数组元素

编程实验: 数组类的实现

IntArray.h

#ifndef _INTARRAY_H_
#define _INTARRAY_H_

class IntArray
{
private:
    int m_length;
    int* m_pointer;

public:
    IntArray(int len);
    int length();
    bool get(int index, int&amp; value);
    bool set(int index, int value);
    void free();
};

#endif

IntArray.cpp

#include "IntArray.h"

IntArray::IntArray(int len)
{
    m_pointer = new int[len];
    
    for(int i=0; i<len; i++)
    {
        m_pointer[i] = 0;
    }
    
    m_length = len;
}

int IntArray::length()
{
    return m_length;
}

bool IntArray::get(int index, int& value)
{
    bool ret = (index >= 0) && (index < length());
    
    if( ret )
    {
        value = m_pointer[index];
    }
    
    return ret;
}

bool IntArray::set(int index, int value)
{
    bool ret = (index >= 0) && (index < length());
    
    if( ret )
    {
        m_pointer[index] = value;
    }
    
    return ret;
}

void IntArray::free()
{
    delete[] m_pointer;
}

main.cpp

#include <stdio.h>
#include "IntArray.h"

int main()
{
    IntArray a(5);
    
    for(int i=0; i<a.length(); i++)
    {
        a.set(i, i+1);
    }
    
    for(int i=0; i<a.length(); i++)
    {
        int value = 0;
        
        if( a.get(i, value) )
        {
            printf("a[%d] = %dn", i, value);
        }
    }
    
    a.free();
    
    return 0;
}
输出:
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5

小结

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

以上内容参考狄泰软件学院系列课程,请大家保护原创

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 18_对象的构造 (中)全部内容,希望文章能够帮你解决【C++】 18_对象的构造 (中)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。