c + + - constructor exercise and delete, new

Posted by dgudema on Sat, 21 Dec 2019 21:45:12 +0100

Intensive practice

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

class   ABCD
{
public:
    ABCD(int    a, int  b, int  c)
    {
        _a = a;
        _b = b;
        _c = c;
        printf("ABCD()  construct,  a: %d,b: %d,c: %d        \n", _a, _b, _c);
    }
    ~ABCD()
    {
        printf("~ABCD() construct,a: %d,b: %d,c: %d      \n", _a, _b, _c);
    }
    int getA()
    {
        return  _a;
    }
private:
    int _a;
    int _b;
    int _c;
};

class   MyE
{
public:

    MyE() :abcd1(1, 2, 3), abcd2(4, 5, 6), m(100)
    {
        cout << "MyE()" << endl;
    }
    ~MyE()
    {
        cout << "~MyE()" << endl;
    }

    MyE(const   MyE &   obj) :abcd1(7, 8, 9), abcd2(10, 11, 12), m(100)
    {
        printf("MyD(const   MyD &   obj) \n");
    }
public:
    ABCD    abcd1;   //c + + compiler does not know how to construct abc1
    ABCD    abcd2;
    const int   m;
};

int doThing(MyE mye1)//mye1. Copy construction (main::myE)
{
    printf("doThing()   mye1.abc1.a: %d \n", mye1.abcd1.getA());
    return 0;
}
int run()
{
    MyE myE;
    doThing(myE);
    return 0;
}

int run2()
{
    printf("run2    start.. \n");
    //ABCD(400, 500, 600); / / life cycle of temporary object        
    ABCD    abcd    =   ABCD(100,   200,    300);
    printf("run2    end\n");
    return 0;
}

int main(void)
{
    run2();
    return 0;
}

Intensive exercise 2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

//In the structure, the adjusting structure is dangerous
class   MyTest
{
public:
    MyTest(int  a, int  b, int  c)
    {
        _a = a;
        _b = b;
        _c = c;
    }
    MyTest(int  a, int  b)
    {
        _a = a;
        _b = b;
        MyTest(a, b, 100);//Create an anonymous object
        //
    }
    ~MyTest()
    {
        printf("MyTest~: %d,     %d,     %d\n", _a, _b, _c);
    }
    int getC()
    {
        return  _c;
    }
    void    setC(int    val)
    {
        _c = val;
    }

private:
    int _a;
    int _b;
    int _c;
};

int main()
{
    MyTest  t1(1, 2);
    printf("c: %d\n", t1.getC());    //What is the value of c?
    return 0;
}
  • Dynamic construction and release of objects
    • malloc free function, new delete operation symbol
    • Assignment base type, assignment array type, assignment object
    • new and malloc in-depth analysis, mixed testing, comparison of similarities and differences
  • Anonymous object lifecycle
  • malloc free function, new delete operation symbol
  • Assignment base type, assignment array type, assignment object
  • new and malloc in-depth analysis, mixed testing, comparison of similarities and differences
  • Anonymous object summary
    • Anonymous object lifecycle
    • Anonymous objects go and stay
    • Call construction in construction
  • Anonymous objects go and stay
  • Call construction in construction
  • Static member variable and static member function (belong to class, syntax)

new and delete

Comparison between c and c + +

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>


using namespace std;

class Test
{
public:
    Test()
    {
        cout << "Test()" << endl;
        m_a = 0;
        m_b = 0;
    }
    Test(int a, int b)
    {
        cout << "Test(int, int)" << endl;
        m_a = a;
        m_b = b;
    }
    void printT()
    {

        cout << "printT:"<<m_a<<","<<m_b << endl;
    }
    ~Test()
    {
        cout << "~Test()" << endl;

    }
private:
    int m_a;
    int m_b;
};

//In C language
void test1()
{
    int *p = (int *)malloc(sizeof(int));

    *p = 10;
    if (p != NULL) {
        free(p);
        //delete p;
        p = NULL;
    }

    int *array_p = (int *)malloc(sizeof(int)* 10);

    for (int i = 0; i < 10; i++) {
        array_p[i] = i + 1;
    }

    for (int i = 0; i < 10; i++) {
        printf("%d ", array_p[i]);
    }
    printf("\n");

    if (array_p != NULL) {
        free(array_p);
        array_p = NULL;
    }


    cout << "==============" << endl;

    Test *tp = (Test*)malloc(sizeof(Test));
    tp->printT();

    if (tp != NULL) {
        free(tp);
        tp = NULL;
    }
}

//malloc free is a function, standard library, stdlib.h
//When new initializes an object on the heap, it triggers the object's constructor. malloc can't
//free does not trigger an object's destructor.
//In C + +
void test2()
{
    int *p = new int;
    *p = 10;
    if (p != NULL) {
        free(p);
        p = NULL;
    }

    int *array_p = new int[10];
    for (int i = 0; i < 10; i++) {
        array_p[i] = i + 1;
    }

    for (int i = 0; i < 10; i++) {
        cout << array_p[i]<<" ";
    }
    cout << endl;

    if (array_p != NULL) {
        delete [] array_p;
    }

    cout << "==========" << endl;
    //Test *tp = new Test(10, 20); / / trigger parameter construction
    Test *tp = new Test;//Trigger no tragic structure
    tp->printT();
    if (tp != NULL) {
        delete tp;
        tp = NULL;
    }

}

int main(void)
{
    test1();

    cout << "-----------" << endl;

    test2();
    
    return 0;
}

Topics: C++ C