Help on classes please

Hi, so i was wondering if so far this is right?

i made a header file so far

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
#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>

using namespace std;

 class StudentTestScores{
    private:
	string studentName;
	double *testScores;
	int numTestScores;
   public:
	StudentTestScores(string name = "", int numScores = 0);	// class constructor	
	StudentTestScores(const StudentTestScores &other);      // class copy constructor
	~StudentTestScores();  				// class destructor
// we are suppose to make a display function to display it in the following format:
//  student Name <tab> number of test scores <tab> test scores separated by tabs <endl>
	void display()
	{
		cout << studentName << \t << numTestScores << \t << testScores << endl;
	}
	

};
#endif  


that is the header file, and now i made a cpp file to implement the constructors, destructors, copy constructor and calling the display function.

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
#include <iostream>
#include "StudentTestScores.h"
using namespace std;

//constructor
StudentTestScores::StudentTestScores(string name, int )
{
	studentName = name;
	numTestScores = 0;
}

//destructor
StudentTestScores::~StudentTestScores()
{
	delete [] testScores;
}

//copy constructor
StudentTestScores::StudentTestScores(const StudentTestScores &obj)
{
	testScores = new double;
	*testScores = *(obj.testScores);
}


void display();


is that right?
Any and all help greatly appreciated.
There are a couple of problems with your code.

1) Unlearn using namespace std; because it is a bad habit, especially if done in headers.
http://www.parashift.com/c++-faq/using-namespace-std.html

2) \t is a character so it should have apostrophes around it.
cout << studentName << '\t' << numTestScores << '\t' << testScores << endl;

3) You can't print the contents of testScores like that, how you try above.
You need to use a for() loop to iterate its contents.
1
2
3
4
5
6
7
#include <cstddef> // for std::size_t

//...

// prefer to use size_t for sizes and lengths, instead of int
for (std::size_t i=0; i < numTestScores; ++i)
    std::cout << testScores[i] << ' ';


A more elegant approach would be to use a C++ container.
But because this is homework designed to teach you about dynamic memory and the dangers of shallow copy, I won't go into details about that approach.
http://www.cplusplus.com/reference/stl/
http://www.cplusplus.com/doc/tutorial/dynamic/
http://stackoverflow.com/questions/2657810/deep-copy-vs-shallow-copy

Coincidentally your copy constructor isn't right.
1
2
3
4
5
StudentTestScores::StudentTestScores(const StudentTestScores &obj)
{
	testScores = new double;
	*testScores = *(obj.testScores);
}


You should be using the new[] operator instead.
Also in C++, just as in C, you can't copy plain arrays by simple assignment. You should therefore use std::copy() from the algorithm library.
http://www.cplusplus.com/reference/algorithm/copy/

1
2
3
4
5
6
7
StudentTestScores::StudentTestScores(const StudentTestScores &obj)
{
	studentName = obj.studentName;
	numTestScores = obj.numTestScores;
	testScores = new double[obj.numTestScores];
	std::copy(obj.testScores, obj.testScores + obj.numTestScores, testScores);
}


You could also look into initialization lists. They are used for initializing member data in constructors, without using assignment:
http://www.parashift.com/c++-faq/init-lists.html

4) You aren't using the int parameter in your constructor.
1
2
3
4
5
StudentTestScores::StudentTestScores(string name, int /* ??? */)
{
	studentName = name;
	numTestScores = 0; // ??? why not use the parameter
}


5) In your .cpp file you don't need to declare display() and also that's not the correct way to declare it.
void StudentTestScores::display();

6) Look into const correctness and mark member functions that don't change member data as const. For example, display():
http://www.parashift.com/c++-faq/const-member-fns.html

1
2
3
4
	void display() const
	{
		// ...
	}


7) You need to implement the copy operator operator= which is a bit like the copy constructor, but you need to check for self-assignment and needs to return a reference to the object.

1
2
3
4
5
6
7
8
9
StudentTestScores & operator = (const StudentTestScores &obj)
{
    if (this != &obj) // if not the same object
    {
        // then copy as usual
    }

    return *this;
}


http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

Edit: small mistakes and additions.
Last edited on
Topic archived. No new replies allowed.