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.
int main() {
constint 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"
usingnamespace std;
Competitor::Competitor(string athlete, int lane_assignment){
name = newchar[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(){
}
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?