C++ Programming for Medical Imaging- Question2

 

 

#include "stdafx.h"
#include <iostream>
using namespace std;


class Image
{
public:

    Image()                            // 构造函数
    {
        cout << "construct !\n";
    };

    Image(const Image & inImg)         //  拷贝构造函数
    {
        m_pBuf = inImg.m_pBuf;
        m_nImgSize = inImg.m_nImgSize;
    }

    Image(const Image & inImg)         // 移动构造函数-主要针对浅拷贝
    {
        m_pBuf = inImg.m_pBuf;
        m_nImgSize = inImg.m_nImgSize;

        inImg.m_pBuf = NULL;           // 赋NULL
    };
    void allocBuff()
    {
        m_pBuf = new char[100];
    }
     
    ~Image()
    {
        if (NULL != m_pBuf)
        {
            delete m_pBuf;
            m_pBuf = NULL;
        }

        cout << "desconstruct !";
        
    };

private:

    char *m_pBuf;
    int m_nImgSize;
};


int main()
{
    Image * pImgA = new Image();
    pImgA->allocBuff();

    Image * pImgB = new Image(*pImgA);

    if (NULL != pImgA)
    {
        delete pImgA;
        pImgA = NULL;
    }
    

    return 0;
}

 

#include <iostream>
using namespace std;

int count = 1; //全局变量

int fun()
{
    static int count = 10; // 在第一次进入这个函数的时候,变量 count 被初始化为 10!并接着自减 1,以后每次进入该函数,count 的值是上一次函数运行之后的值
    return count--;        // 就不会被再次初始化了,仅进行自减 1 的操作;在 static 发明前,要达到同样的功能,则只能使用全局变量
}
 
//int count = 1; //全局变量
 
int main()
{
     cout<<"global  "<<"local staic"<<endl;


     for(int i = 1; i <= 10; ++count)
        cout<< i <<"        "<< fun() <<endl;
     return 0;
}

 

Question2-C

auto的原理就是根据后面的值,来自己推测前面的类型是什么。

auto的作用就是为了简化变量初始化,如果这个变量有一个很长很长的初始化类型,就可以用auto代替。

注意点:
1.用auto声明的变量必须初始化(auto是根据后面的值来推测这个变量的类型,如果后面没有值,自然会报错)

2.函数和模板参数不能被声明为auto(原因同上)

3.因为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid

4.定义在一个auto序列的变量必须始终推导成同一类型

 

Question2-D

迭代器(iterator)是一种抽象的设计理念,通过迭代器可以在不了解容器内部原理的情况下遍历容器

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;  //v是存放int类型变量的可变长数组,开始时没有元素
    for (int n = 0; n<5; ++n)
        v.push_back(n);  //push_back成员函数在vector容器尾部添加一个元素

    //定义正向迭代器
    for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) 
    {  //用迭代器遍历容器
        cout << *i << " ";  //*i 就是迭代器i指向的元素
        *i *= 2;  //每个元素变为原来的2倍
    }
    cout << endl;

    //用反向迭代器遍历容器
    for (vector<int>::reverse_iterator j = v.rbegin(); j != v.rend(); ++j)
        cout << *j << " ";

    return 0;
}

Question2-E

在C++11中,除了初始化列表(在构造函数中初始化)外,允许使用等=或花括号{}进行就地的非静态成员变量初始化

#include <iostream>

using namespace std;

int main()
{
    int a(5);

    int b{ 5 };

    cout << a << endl;

    cout << b << endl;

}

// Question2-F

在类的构造函数和析构函数中没有匹配的调用new和delete函数

两种情况下会出现这种内存泄露:

一. 是在堆里创建了对象占用了内存,但是没有显示地释放对象占用的内存;

二.是在类的构造函数中动态的分配了内存,但是在析构函数中没有释放内存或者没有正确的释放内存

 

// Question2-G

#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

class MyClass 
{

    map<string,string> fields;

public:

    // 1. 当const在函数名前面的时候修饰的是函数返回值,

    // 2. 在函数名后面表示是常成员函数,该函数不能修改对象内的任何成员,只能发生读操作,不能发生写操作。(fields 值只能读取,不能修改

    // 3. const 作为函数参数的入参,表示入参是个常量,入参不能修改。


    const string& getField(const string & value) const
    {
        auto i{ fields.find(value) };

        if (i != fields.end())
            return i->second;

        throw "No such field.";
    }

};

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值