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?

Recommended Answers

All 5 Replies

Please use code tags to post the relevant parts of the code - no one wants to look through 10 files!

Dave

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.";
	}
}

Not exactly sure what your trying to do, but if your dealing with pointers, you use the -> operator to call functions and what not.

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> .

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.