Array of objects and pointers

I just posted the relevant code for my question. There is a declaration in main that is a pointer to an array of Competitor objects, followed by the creation of 4 Competitor objects to be placed in that array(in bold). The part I don't know how to do is when a Competitor object is instantiated, the constructor in Competitor.cpp should be called to initialize the instance variables...in bold...this is something I dont know how to do, but have tried to look it up in my book to no avail.

What the program is supposed to do is place a competitor's name, lane assignment, and then their time...in one object...and then each object into an array for later use.



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
int main() {
    const int lanes = 4;
    Ranker rank(lanes);
 
    csis.open("csis.dat");
 
    // First make a list of names and lane assignments
    Competitor* starters[lanes];
 
    starters[0] = new Competitor("EmmyLou Harris", 1);
    starters[1] = new Competitor("Nanci Griffith", 2);
    starters[2] = new Competitor("Bonnie Raitt",   3);
    starters[3] = new Competitor("Joni Mitchell",  4);    // The race is run; now assign a time to each person
    starters[0]->setTime((float)12.0);
    starters[1]->setTime((float)12.8);
    starters[2]->setTime((float)11.0);
    starters[3]->setTime((float)10.3);

//competitor.h

#ifndef _COMPETITOR_H
#define _COMPETITOR_H

class Competitor{

	private:
		int lane_number;
		char *name;
		double time;

	public:
		
		Competitor(string athlete, int lane_assignment);

		void setTime(double finishTime);

		char returnName();
		int returnLane();
		double returnTime();

		void printObject();



		~Competitor(){
			name = "\0";
			lane_number = 0;
		}

};

#endif


//competitor.cpp

#include <iostream>
#include "competitor.h"

using namespace std;



Competitor::Competitor(string athlete, int lane_assignment){
	name = new char[strlen(athlete) + 1];
	strcpy(name, athlete);

	lane_number = lane_assignment;
}
void Competitor::setTime(double finishTime){
	time = finishTime;
	

}

char Competitor::returnName(){

	return name;

}

int returnLane(){
	

}

double returnTime(){


}

void printObject(){


}
The new operator automatically calls the constructor. You're doing it right.
yes. I know that is what happens when the new operator is used...The syntax for the constructor is wrong...and I'm not sure why...also I dont know how to add the time to each object since it is a separate statement. Any suggestions?
Topic archived. No new replies allowed.