Can help to check if I coded correctly?

Hello Everyone,

For my assignment, I am required to apply three constructors for the WeeklySales class:

- a parameterised constructor with the encessary paramaters to initialise all the data members of a WeeklySales instance.

- a default constructor that will initialise the data members salesPerson to "Unknown", noOfWeek to 13 and amount to point to an array of thirteen "0.00".

- a copy constructor that will create a new WeeklySales instance and copy the values of the data members from an existing WeeklySales instance to the new WeeklySales instance

The code below works fine just that I am seeking opinion if I am doing it correctly?

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
#include <iostream>
using namespace std;

class WeeklySales
{
	char* salesPerson;
	double* amount; // Pointer to an array
	int noOfWeek; // Size of the array
public:

	// parameterised constructor
	WeeklySales(char* personSales, double* amountToArray, int arraySize)
	{
		salesPerson = new char[strlen(personSales) + 1];
		strcpy(salesPerson, personSales);
		amount = amountToArray;
		noOfWeek = arraySize;
	}

	// default constructor
	WeeklySales()
	{
		salesPerson = new char[strlen("Unknown") + 1];
		strcpy(salesPerson, "Unknown");
		//amount = 0.00;
		noOfWeek = 13;
		double amtArray[] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00};
		amount = amtArray;
	}

	// Copy constructor
        //& <-- reference/pointing || Cloning/copy
	WeeklySales(const WeeklySales& weeklySalesCopy)  
	{
		salesPerson = new char[strlen(weeklySalesCopy.salesPerson) + 1];
		strcpy(salesPerson, weeklySalesCopy.salesPerson);
		amount = weeklySalesCopy.amount;
		noOfWeek = weeklySalesCopy.noOfWeek;
	}
 

	//print for test purpose
	void printweeklySalesCopy()
     {
		 cout << "Name of Salesperson: " << salesPerson << endl;
		 cout << "Amount: $" << amount << endl;
		 cout << "Number of Weeks: " << noOfWeek << endl << endl;
     }
};

 

int main ()
{
	// parameterised
	WeeklySales test1;
	cout << "Account 1 using parameterised constructor\n";
	test1.printweeklySalesCopy();

	// default
	WeeklySales test2;
	cout << "Account 2 using default constructor\n";
	test2.printweeklySalesCopy();

	// copy
	WeeklySales test3(test2); //cloning
	cout << "Account 3 via cloning through copy constructor\n";
	test3.printweeklySalesCopy();
}
Sorry. I just realised that when the output for amount comes out some random hex value. What could be the cause of it?

Any help is truly appreciated.
The 'amount' is a pointer. The closest operator<< is the one that takes a pointer and shows the stored address, not the operator that takes a double. You have to dereference the pointer to get the intended value out.

The amtArray is a local variable, which expires on line 29. Thus, the 'amount' holds an address to invalid memory location after the constructor call is complete.
Last edited on
thanks for replying, keskiverto.

sorry i am still quite new to c++ therefore i do not quite understand what u are saying..
Instead of explaining these errors for this case, it's best if you learn the general theory.

Here is pointers: http://www.cplusplus.com/doc/tutorial/pointers/

I couldn't find a decent tutorial on scope online (I guess you need to read a C++ book). Here is a quick explanation.
1
2
3
4
5
6
7
8
9
10
11
12
void somefunction()
{
    int x; //This variable is now accessible to this function. 
    x = y; //This will not work, because y is out of scope. 
}

int main()
{
    int y; //This variable is in scope of main(). 
    x = 5; //This will not work, because x is out of scope. 
    return 0;
}


To move values between functions, you can do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
 
void somefunction(int) //We say that somefunction takes an argument of an integer called z. 
{
    int x; 
    x = z; //z is now in scope, because it was passed from main() to this function.  
}

int main()
{
    int y = 5; //This variable is in scope of main() and only accessible to main().
    somefunction(y); //but we can pass the value to the function as an argument. 
    return 0; 
}
Last edited on
Topic archived. No new replies allowed.