-2

I am learning constructors and Destructors in c++; Help me grasp my mistakes even if they are silly...

HERE is a code I have written to perform addition using classes in c++; This creates two summands of datatype num and employs the constructor sum() to perform sum of the two numbers; However when everything was goin' alright, I stumbled upon creating a copy constructor for num , (Although not necessary but still for practice)... without the dynamic object of the class sum it is not possible to run the code anyway(without removing the copy constructor)... Help me improve my code and my mistakes in the code below; Also I wanna know how to make use of the copy constructor in this program; the problem being that in the destructor the delete operation is being performed multiple times on the same piece of memory (I suppose)

Here's my Code

#include<iostream>
#include<new>
using namespace std;
class num
{
public:
    int *a;
    num(int x)
    {
        try
        {
            a=new int;
        }
        catch(bad_alloc xa)
        {
            cout<<"1";
            exit(1);
        }
        *a=x;
    }
    num(){  }
    num(const num &ob)
    {
        try
        {
            a=new int;
        }
        catch(bad_alloc xa)
        {
            cout<<"1''";
            exit(2);
        }
        *a=*(ob.a);
    }
    ~num()
    { 
        cout<<"Destruct!!!";
        delete a;
    }
};


class sum:public num
{
 public:
     int add;
     sum(num n1,num n2)
     {
         add=*(n1.a)+*(n2.a);
     }
     int getsum()
     {
         return add;
     }
};

int main()
{
    num x=58;
    num y=82;
    sum *s=new sum(x,y);
    cout<<s->getsum();
    delete s;
    return 0;
}
Claudio
  • 10,614
  • 4
  • 31
  • 71
Arijit Dey
  • 21
  • 3
  • For code reviews you might consider going to [CodeReview Stackexchange](https://codereview.stackexchange.com) – UnholySheep Jul 05 '18 at 07:57
  • 1
    `num(){ }` this is **extremely** wrong. You cannot leave your pointer data member uninitialised. – n. m. could be an AI Jul 05 '18 at 08:19
  • Why does `sum` need to inherit from `num`? `sum(num n1,num n2)` prefer taking by `const num&` instead. `sum(const num& n1, const num& n2)` – Wander3r Jul 05 '18 at 08:24
  • I don't see the need for `num` to hold a `int *`, rather than just an `int`. Or for it to exist *at all*. Just operate on `int`s – Caleth Jul 05 '18 at 11:54

1 Answers1

0

I may miss something - didn't use new/delete for too long, but tried to correct all what I noticed.

P.S. always use smart pointers.

#include <iostream>
#include <exception>
#include <new>

using namespace std;

int* allocate(const char* err_msg, int exit_code)
{
    int* a = nullptr;
    try
    {
        a = new int;
    }
    catch (bad_alloc&)
    {
        cout << err_msg << endl;
        exit(exit_code);
    }
    return a;
}

class num
{
    int* a = nullptr; // always should be initialized here

public:
    num() noexcept : a(nullptr) // or here
    {}

    /*explicit*/ num(int x) : a(allocate("1", 1))
    {
        *a = x;
    }

    num(const num& ob) : a(allocate("1''", 2))
    {
        *a = *(ob.a);
    }

    // rule of zero/three/five
    // default copy assignment will copy pointer and one int will be leaked and one will be deleted twice
    num& operator =(const num& ob)
    {
        if (&ob == this)
        {
            return *this;
        }

        *a = *(ob.a);
        return *this;
    }

    ~num()
    { 
        cout << "Destruct!!!";
        delete a;
        a = nullptr; // usefull for debug
    }

    int value() const
    {
        if (a == nullptr)
        {
            throw runtime_error("a == nullptr");
        }
        return *a;
    }
};

class sum
{
    int add = 0;

public:
    sum(const num& n1, const num& n2)
    {
        add = n1.value() + n2.value();
    }

    int getsum() const
    {
        return add;
    }
};

int main()
{
    const num x = 58;
    const num y = 82;
    const sum* s = new sum(x, y);
    cout << s->getsum() << endl;
    delete s;
    return 0;
}
wtom
  • 565
  • 4
  • 12