Problem with code

Hello again! I have two problems with an exercise I've been trying to cope with.

My first problem is how to create a constructor which will increase the number of objects by one (1) and will print a message that a new object is created, and a destructor which will reduce the number of objects by one (1). I wrote the destructor as well as the constructor but I guess the constructor is not right. Any advice?

My second problem is about building a function which will accept a parameter of type int (say int k) and will return as a value an object of type integer (which is the name of my class), and with a class variable member x equal to k. Again I wrote the function but it is wrong. Any advice on that too?

I wish a Happy New Year to everyone by the way!

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using namespace std;

class integer {

    int x{};
    
public:
    int objnum{};
    integer(){};
    integer(int a) : x(a) {};
    void set_x(int a) { x = a; };
    int get_x() { return x; };
    void display() { cout << "X is :" << x; };
    int sgobjnum(int b) { objnum = b; return objnum; };
    void show() { cout << "Number of objects is : " << objnum; };
    
    integer(integer& obj) {                       // The constructor in question
        if (obj += 1) {                          
            objnum++;
            cout << "New object created" << endl;
        }
    };
    ~integer() {
        objnum--;
        cout << "New object deleted" << endl;
    };
    friend int doublevalue(integer& obj);
    friend int returnobj(int k);
};

int doublevalue(integer& obj) {
    int doublev;
    doublev = (obj.x) * 2;
    return doublev;
}

int returnobj(int k) {                 //The function in question
    integer obj;
    return obj.x = k;
}
Hello geovoulg,

I can see why you are having problems. You code is hard to read and is missing some key parts like the include files and "main".

Without the rest of the code what you have posted does not compile and is full of errors.

Also I have no idea what you are doing in "main" or how you are dealing with the class.


1. My first problem is how to create a constructor which will increase the number of objects by one (1)

2. and will print a message that a new object is created,

3. and a destructor which will reduce the number of objects by one (1).

I wrote the destructor as well as the constructor but I guess the constructor is not right.


For point 1 just doing something that will call the ctor will increase the number of objects by 1. Using the ctor to create a new object of the class makes me thing of an endless loop with no way out. How would you know when to stop? That is if it would even work in the first place.

Point 2 is easy that is just a "cout" statement in the ctor.

Since your code is hard to read I off this suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using namespace std;

class integer
{
    int x{};

    public:
        int objnum{};

        integer() {};
        integer(int a) : x(a) {}

        void set_x(int a) { x = a; }

        int get_x() { return x; }

        void display() { cout << "X is :" << x; }

        int sgobjnum(int b) { objnum = b; return objnum; }

        void show() { cout << "Number of objects is : " << objnum; }

        integer(integer& obj)
        {                       // The constructor in question
            if (obj += 1)
            {
                objnum++;
                cout << "New object created" << endl;
            }
        }

        ~integer()
        {
            objnum--;

            cout << "New object deleted" << endl;
        }

        friend int doublevalue(integer& obj);
        friend int returnobj(int k);
};

int doublevalue(integer& obj)
{
    int doublev;

    doublev = (obj.x) * 2;

    return doublev;
}

int returnobj(int k)
{                 //The function in question
    integer obj;

    return obj.x = k;
}

Seeing the code in this way I noticed int doublev;. After a while it did make sense because you are multiplying by 2, but at first it is misleading. I would just use "v", but I would give it a better name then "v". Something to think about.

In your problem function try this:
1
2
3
4
5
6
7
8
interger returnobj(int k)
{                 //The function in question
    integer obj;

    obj.set_x = k;

    return obj;
}

Just a thought. Your code looks more like you are setting the value of "x" = "k" and then returning the new variable and not the whole object.

But without the missing code that is just a guess for now.

Andy
As the constructor/destructor is modifying objnum, objnum will need to be static. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

class integer
{
	int x {};
	static int objnum;

public:
	integer() {++objnum;}
	integer(int a) : x(a) {++objnum;}

	void set_x(int a) { x = a; }
	int get_x() const { return x; }

	void display() const { std::cout << "X is :" << x << '\n'; }
	// int sgobjnum(int b) { objnum = b; return objnum; }  //objnum should not be changed like this

	void show() const { std::cout << "Number of objects is : " << objnum << '\n'; }

	integer(integer& obj) : x(obj.x) {
		++objnum;
		std::cout << "New object created\n";
	}

	~integer() {
		--objnum;
		std::cout << "Object deleted\n";
	}
};

int integer::objnum {};

int doublevalue(const integer& obj)
{
	return obj.get_x() * 2;
}

integer returnobj(int k)
{
	return integer(k);
}


What's the test code?

Last edited on
Hello Andy and thank you. I didn't include the main function because I haven't write, for now, anything in the main, I focused on creating the above functions.

Now, this is my problem. The constructor in question I made isn't working. As a matter of fact, the compiler says that for the (obj += 1) condition no operator += matches these operands. Any ideas?

Regarding the function, I indeed return the new variable and not the whole object. And my question is, how the heck should I write the code to get the object and not the variable only?

George
Seeplus thank you. I haven't yet writen any code in main as my compiler says there are errors in the code i.e

1) In the constructor in question the compiler says that for the (obj += 1) condition no operator += matches these operands

2)The function in question as Andy also fairly noticed, returns the new variable and not the whole object. My question is, how the heck should I write the code to get the object and not the variable only as I currently can't think of a possible way.

I also forgot to tell you that the exercise requires that the objnum variable is public and not private.
Hello geovoulg,


1) In the constructor in question the compiler says that for the (obj += 1) condition no operator += matches these operands


You are trying to add 1 to an object not a variable. You will have to explain what you are trying to do with this if statement. When I commented out the if statement the rest seemed to work.

For point 2 I gave you an idea, but have not tested that part yet.


I haven't yet written any code in main as my compiler says there are errors in the code


Does that mean that you are one of those that start at the end and work backwards?

Andy
Hello Andy. Truth is after looking at my code more carefully, I found out that I was mistaken regarding my logic back when I wrote that, more confused on what I wanted to succeed I'd say. So I removed the if as well.

Regarding the 2nd point, I just saw more carefully your first reply. I used that however the response from the compiler was "no suitable conversion function from integer to int exists".

George
Last edited on
Seeplus thank you. I haven't yet writen any code in main as my compiler says there are errors in the code i.e

1) In the constructor in question the compiler says that for the (obj += 1) condition no operator += matches these operands


That's not used in my code - which compiles OK using VS2019.

I also forgot to tell you that the exercise requires that the objnum variable is public and not private.


objnum should definitely NOT be public

I suggest you write some test code - and then see what isn't supported in my code.
Topic archived. No new replies allowed.