How to get object name from within class methods

I'm working on a project in my programming class that you're supposed to make a .h and .cpp file that you can set values of an array, get values of an array, print the array, add the array to another object, and subtract the array from another object.

We're supposed to provide some cout in the class methods to show the teacher what the method is doing. like the in the set function saying 'cout << "Setting the " << place << "to " << x << " value." << endl;'

What I'm wondering is can you access the object name from the class methods so that I can say 'cout << "adding " << firstobjectname << " array to << secondobjectname << ". " << endl;

here's my cpp file.



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "safearray.h"
#include <iostream>

using namespace std;

SafeArray::SafeArray()
{
    size = 0;
}




SafeArray::SafeArray(SafeArray &x)
{

	for (int index = 0; index < 100; index++)
	{
		array[index] = x.array[index];

	}

    size=x.size;
}



SafeArray::~SafeArray()
{

}

void SafeArray::SetArray(int place, int value)
{
        if (place < 0 || place > 99)
        {
            cout << "SetArray error" << endl;
            return;
        }
		array[place] = value;


		if(place >= size)
        {
            size = place + 1;
        }

        cout << "Setting the " << place << " place in the array to " << value << ". " << endl;

}

int SafeArray::GetArray(int x)
{
    if (x < 0 || x > size-1)
        {
            cout << "SetArray error" << endl;
            return 0;
        }

        cout << "Getting the value in the " << x << " place." << endl;
	return array[x];
}

void SafeArray::PrintArray()
{
	for (int index = 0; index < size; index++)
    {
    if (index == size - 1)
	{
	cout << array[index] << ". ";
    }
	else
	cout << array[index] << ", ";
    }
    cout << endl;
}

void SafeArray::AddArray(const SafeArray s)
{
    if(size!=s.size)
    {
        cout << "sizes not equal. " << endl;

        return;
    }


	for (int count = 0; count < size; count++)
    {
        array[count] = array[count] + s.array[count];
    }


}

void SafeArray::SubArray(const SafeArray s)
{
    if(size!=s.size)
    {
        cout << "sizes not equal. " << endl;

        return;
    }


	for (int count = 0; count < size; count++)
    {
        array[count] = array[count] - s.array[count];
    }


}




I'm not sure if I'm being clear enough but basically can you add code in the classes (without having to pass through the class name as a str or char in the parameters of the method) to be able to cout some sort of confirmation message about what's happening using the object names.

Thanks!
cout << array[index] << endl; assuming your array is holding something that can be printed (e.g a string)
I think we're talking about different things. I know how to print out the array. But when you create an object in main, how you can access the name of that object in the classes.

For example, if I create SafeArray s object, how can I print out something that says

"Printing out the s array." without hard coding the 's' into the PrintArray function.

Sorry for the lack of clarity. Thanks for the reply!
You cannot. The name of the variable is not kept.

You have to give it a string member that holds the name, then assign it as part of the constructor. Basically do it manually.
Last edited on
Okay, that's kind of what I thought. Thank you!
Topic archived. No new replies allowed.