In my experiment.cpp i have Test *tests[maxTests]; as private data. How do i access this in my runExperiment function to call functions from the test class or its derived classes?
corby -4 Junior Poster
//class member functions
#include "date.h"
Date::Date()//default constructor
{
month = 1;
day = 1;
year = 2010;
};
void Date::setDate()
{
cout << "What date was this test performed(mm/dd/yyyy): ";
cin >> setw(2) >> month;
cin.ignore(1);
cin >> setw(2) >> day;
cin.ignore(1);
cin >> setw(4) >> year;
month = checkMonth(month);
day = checkDay(day);
year = checkYear(year);
}
int Date::checkMonth(int testMonth)
{
if(testMonth < 0 || testMonth > 12)
{
cout << "Invalaid month(" << testMonth << ") set to 1.\n";
return 1;
}else
return testMonth;
}
int Date::checkDay(int testDay)
{
int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(testDay > 0 && testDay <= daysPerMonth[month])
{
return testDay;
}else if(month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
{
return testDay;
}else
{
cout << "Invalid day(" << testDay << ") set to 1.\n";
return 1;
}
}
int Date::checkYear(int testYear)
{
if(testYear > 2010 || testYear < 1990)
{
cout << "Invalid year (" << testYear << ") set to 2010.\n";
return 2010;
}else
return testYear;
}
void Date::print()const
{
cout << month << '/' << day << '/' << year;
}
//header file
#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <iomanip>
using namespace std;
class Date
{
public:
Date();
void setDate();
void print()const;//int checkDate(Date &); //int = 1, int = 1, int = 1900);
private:
int month;
int day;
int year;
int checkMonth(int);
int checkDay(int);
int checkYear(int);
};
#endif
//class member functions
#include "experiment.h"
Experiment::Experiment()
{
numberOfTests = 0;
}
void Experiment::setExpName()
{
cout << "Please enter a name for this experiment: ";
cin >> name;
}
string Experiment::getExpName()
{
return name;
}
void Experiment::setPersonForExperiment()
{
cout << "Please enter the name of person responsible for conducting this experiment: ";
cin >> who;
}
string Experiment::getPersonForExperiment()
{
return who;
}
void Experiment::runExperiment()
{ char answer;
char decision;
cout << "Welcome to the \"I want to be Happy Experiment\" ";
do{
cout << "Do you want to enter a new experiement?(Y/N)";
cin >> answer;
do{
setExpName();
setPersonForExperiment();
//date here
cout << "\tEnter (W) to enter Weather data, (S) to enter Sports data or (E) to end: ";
cin >> decision;
if(decision == 'W')
{
tests = &tests[0];
tests[0].getTestInfo();
tests[0].print();
} else if(decision == 'S')
{
cout << "Sports stuff here.";
}
}while(decision == 'W' || decision == 'S');
}while(answer == 'Y');
}
//header file
#include "test.h"
#include "date.h"
#include "stest.h"
#include "wtest.h"
class Experiment
{
public:
const static int maxTests = 20;
Experiment();
void setExpName();
string getExpName();
void setPersonForExperiment();
string getPersonForExperiment();
void runExperiment();
private:
string name;//: name of the experiment
Date date;// Date the experiment was launched
string who;// person responsible for conducting the experiment
int numberOfTests;// the number of tests in this experiment
Test *tests[maxTests];// an array of all tests performed for this experiment (you can assume no more that 20 tests are run for each experiment performed.)
};
//main
#include "experiment.h"
#include "test.h"
#include "date.h"
#include "stest.h"
#include "wtest.h"
int main()
{
/*
cout << "Data" << setw(15) << "Tester" << setw(15) << "Date" << setw(15) << "pos" << setw(15) << "neg" << setw(15) << "ind\n";
*/
Experiment exp1;
exp1.runExperiment();
return 0;
}
//class member functions
#include "stest.h"
SportTest::SportTest():Test()
{
}
void SportTest::setEvent()
{
cout << "What sporting event?";
cin >> sportingEvent;
}
string SportTest::getEvent()const
{
return sportingEvent;
}
void SportTest::getTestInfo()
{
setEvent();
Test::getTestInfo();
}
void SportTest::print()const
{
cout << getEvent() << setw(15);
Test::print();
}
//header file
#ifndef STEST_H
#define STEST_H
#include "test.h"
class SportTest : public Test
{
public:
SportTest();
void setEvent();
string getEvent()const;
void getTestInfo();
void print()const;
private:
string sportingEvent;//the sporting event
};
#endif
//header file
//header file
#ifndef TEST_H
#define TEST_H
#include "date.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Test
{
public:
Test();
void setTester();
void setPosResponse();
void setNegResponse();
void setIndResponse();
void getTestInfo();
string getTester()const;
int getPosResponse()const;
int getNegResponse()const;
int getIndResponse()const;
void print()const;
protected:
Date date;//the data the test was executed
int posResponse;//Number of positive responses logged
int negResponse;//Number of negative responses logged
int indResponse;//Number of indeterminate responses logged
string tester;//person who gathered the information
};
#endif
//class member functions
#include "test.h"
Test::Test()
{
posResponse = negResponse = indResponse = 0;
}
void Test::setTester()
{
date.setDate();
cout << "Who performed the test?";
cin >> tester;
}
void Test::setPosResponse()
{
cout << "How many positive responses were logged?";
cin >> posResponse;
}
void Test::setNegResponse()
{
cout << "How many negative responses were logged?";
cin >> negResponse;
}
void Test::setIndResponse()
{
cout << "How many indifferent responses were logged?";
cin >> indResponse;
}
void Test::getTestInfo()
{
setTester();
setPosResponse();
setNegResponse();
setIndResponse();
}
string Test::getTester()const
{
return tester;
}
int Test::getPosResponse()const
{
return posResponse;
}
int Test::getNegResponse()const
{
return negResponse;
}
int Test::getIndResponse()const
{
return indResponse;
}
void Test::print()const
{
//cout << "Date" << setw(15) << "Tester" << setw(15) << "Data" << setw(15) << "pos" << setw(15) << "neg" << setw(15) << "ind\n";
cout << setw(15) << getTester();
date.print();
cout << setw(15) << getPosResponse();
cout << setw(15) << getNegResponse();
cout << setw(15) << getIndResponse();
}
//class member functions
#include "wtest.h"
WeatherTest::WeatherTest():Test()
{
}
void WeatherTest::setWeather()
{
cout << "What weather condition?";
cin >> weatherCondition;
}
string WeatherTest::getWeather()const
{
return weatherCondition;
}
void WeatherTest::getTestInfo()
{
setWeather();
Test::getTestInfo();
}
void WeatherTest::print()const
{
cout << getWeather() << setw(15);
Test::print();
}
//header file
#ifndef WTEST_H
#define WTEST_H
#include "test.h"
class WeatherTest : public Test
{
public:
WeatherTest();
void setWeather();
string getWeather()const;
void getTestInfo();
void print()const;
private:
string weatherCondition;//the weather condition
};
#endif
daviddoria 334 Posting Virtuoso Featured Poster
Please use code tags to post the relevant parts of the code - no one wants to look through 10 files!
Dave
corby -4 Junior Poster
heres the code what i want to figure out is how to use
Test *tests[maxTests]
to call a function from the Test class or any class derived from the Test class. ANy help is much appreciated!! :)
//header file
#include "test.h"
#include "date.h"
#include "stest.h"
#include "wtest.h"
class Experiment
{
public:
const static int maxTests = 20;
Experiment();
void setExpName();
string getExpName();
void setPersonForExperiment();
string getPersonForExperiment();
void runExperiment();
void createFile(char);
private:
string name;//: name of the experiment
Date date;// Date the experiment was launched
string who;// person responsible for conducting the experiment
int numberOfTests;// the number of tests in this experiment
Test *tests[maxTests];// an array of all tests performed for this experiment (you can assume no more that 20 tests are run for each experiment performed.)
};
//class member functions
#include "experiment.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
Experiment::Experiment()
{
numberOfTests = 0;
}
void Experiment::setExpName()
{
cout << "Please enter a name for this experiment: ";
cin >> name;
}
string Experiment::getExpName()
{
return name;
}
void Experiment::setPersonForExperiment()
{
cout << "Please enter the name of person responsible for conducting this experiment: ";
cin >> who;
}
string Experiment::getPersonForExperiment()
{
return who;
}
void Experiment::runExperiment()
{ char answer;
char decision;
cout << "Welcome to the \"I want to be Happy Experiment\" ";
cout << "Do you want to enter a new experiement?(Y/N)";
cin >> answer;
while(answer == 'Y')
{
do{
setExpName();
setPersonForExperiment();
//date here
cout << "\tEnter (W) to enter Weather data, (S) to enter Sports data or (E) to end: ";
cin >> decision;
if(decision == 'W')
{
/*tests = &tests[0];
tests[0].getTestInfo();
tests[0].print();*/
} else if(decision == 'S')
{
cout << "Sports stuff here.";
}
}while(decision != 'E');
char choice;
cout << "Do you want to save this information in a file(Y/N)?";
cin >> choice;
createFile(choice);
cout << "Do you still want to enter another experiment?";
cin >> answer;
/*if(choice == 'Y')
{
ofstream outClientFile("HappyExperimentTake1.txt", ios::out);
if(!outClientFile)
{
cerr << "File could not be opened" << endl;
exit(1);
}
outClientFile << getExpName() << ' ' << getPersonForExperiment() << endl;
cout << "File saved to HappyExperimentTake1.txt.";
}*/
}
}
void Experiment::createFile(char c)
{
if(c == 'Y')
{
ofstream outClientFile("HappyExperimentTake1.txt", ios::out);
if(!outClientFile)
{
cerr << "File could not be opened" << endl;
exit(1);
}
outClientFile << getExpName() << ' ' << getPersonForExperiment() << endl;
cout << "File saved to HappyExperimentTake1.txt.";
}
}
LevyDee 2 Posting Whiz in Training
Not exactly sure what your trying to do, but if your dealing with pointers, you use the -> operator to call functions and what not.
Banfa 597 Posting Pro Featured Poster
tests and RunExperiment are both members of Experiment so RunExperiment can access tests directly even though tests is private since this only stops access externally to the class.
tests is an array of pointers Test *tests[maxTests]
so tests[0] has type Test* so to access the member of Test you would use tests[0]-><Member>
.
corby -4 Junior Poster
thx
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.