Calling functions from other classes

How do I call functions from other classes from inside another class? I have an assignment to make this sort of thing but I was sick on the day we learned it. I have errors on lines 58,61,64,67, and 71. What am I doing wrong?

Edit: I am completely lost so I apologize for the lack of info. I've heard of "Friend Classes", are these related to my topic?


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class directory{

	public:
		
		void Main()
		{
			cout << "Enter a number for the associated function:" << endl;
			cout << "1.) Calculator " << endl;
			cout << "2.) Encoder/Decoder " << endl;
			cout << "3.) Game " << endl;
			cout << "4.) EXIT " << endl;
		}
		
		void inputDirectory(int y)
		{	
			 directoryV = y;
		}
		int getDirectory()
		{
			return directoryV;
		}




	private:
		int directoryV;

};

class programes{

public:

void calculator()
{
	int repeat, n1, n2;
	do{
	cout << "Calculator --- Enter the number for your function:" << endl;
	cout << "1.) Add:" << endl;
	cout << "2.) Subtract:" << endl;
	cout << "3.) Multiply:" << endl;
	cout << "4.) Devide:" << endl;
	cout << "5.) EXIT CALCULATOR:" << endl;
	cin >> repeat;
	cout << "Enter first number:" << endl;
	cin >> n1;
	cout << "Enter second number:" << endl;
	cin >> n2;
	
 switch(repeat){
			case 1:
				cout << logic.add(n1,n2) << endl;
			break;
			case 2:
				cout << logic.subtract(n1,n2) << endl;
			break;	
			case 3:
				cout << logic.multiply(n1,n2) << endl;
			break;
			case 4:
				cout << logic.devide(n1,n2) << endl;
			break;

			default:
			return 0;
	
	




		}while(repeat == 1 || 2 || 3 || 4);

}

void code()

{

}

void game()

{

}


};

class logic{

public:

int add(int x, int y){
return x+y;

}
int subtract(int x, int y){
return x-y;

}

int multiply(int x, int y){
return x*y;

}

int devide(int x, int y){
return x/y;

}

};

int main() {

	while(true){
        logic logic;
		directory alpha;
	alpha.Main();
	int x;
	cin >> x;
	alpha.inputDirectory(x);
	x = alpha.getDirectory();
		programes pro;
			switch(x){
			case 1:
			pro.calculator();
			break;
			
			case 2:
			pro.code();
			break;

			case 3:
			pro.game();
			break;

			default:
			return 0;
			}
		}
}
Last edited on
The functions inside your logic class should just be free functions. (Also, mathematical operations don't really fit into the category of 'logic' to begin with...)

By the way, in C++ things have to be physically defined sooner in the source file before they are used.
Last edited on
LB, thank you for the information! So I should just remove the "logic" class (I know its not bool logic but I needed a name :-/)? Just for the sake of learning, is there any way to keep the class and still use the functions inside of it? Or is having them stay separate something that has to do with the language being object oriented? Again, thank you for the help and I will try your suggestion.
If you want to group related global functions, use a namespace.

By the way, it's not a good idea to make useless functions, even if you're just learning.
Topic archived. No new replies allowed.