Calling a function

Okay this is going to be going back to the basics, but its a question about the best way of doing something. I need to call a function in another class. Is the best way to do that just to construct a class object and call the function with the class object like:
someclass.function();

or is there a better way to call the one function I need without tying the 2 classes together.
Last edited on
Well, you could also just call the function normally.*

*I'm not really sure what you are trying to the ask though.
I mean if the function is in another class that is another header.

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
#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>
#include "simpleRandomNumber.h"
#include "calculateAverage.h"
#include "calculateStandardDeviation.h"
using namespace std;

class application{
private:
	vector<double> simpleNumber;
	double average;
	double deviation;

	void simpleOutput(){
		cout << "Simple random generator\n------ ------ ---------\n";
		for(vector<double>::iterator i = simpleNumber.begin(); i!= simpleNumber.end(); ++i){
			cout << *i <<"\n";
		}
		cout << "Average: " << average << "\n";
	}

public:
	void run(){
		srand(time(0));
		generate_n(back_inserter(simpleNumber), 10, simpleRandomNumber());
		sort(simpleNumber.begin(), simpleNumber.end());
		calculateAverage calc;
		calculateStandardDeviation calcTwo;
		average = calc.getAverage(simpleNumber);
		deviation = calcTwo.getDeviation(simpleNumber);

		simpleOutput();
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
using namespace std;

class calculateAverage{
private:
	double total;
public:
	double getAverage(vector<double> numbers){
		for(int i = 0; i < 10; i++){
            total = total + numbers[i];
		}
		return total / 10;
	}

};


to call get average from the application class do I need to construct a calculate class object like I did and the call it or is there another way to call it
In this case, you would need to create an object to call the function. However I'm wondering why you have a class for calculating an average...it seems like it would be better to just have it as a global function or as a member function of application.
In the class all the assignments are supposed to use headers on things we will use multiple times. So we get used to using headers and stuff
Topic archived. No new replies allowed.