How to call a Function from inside a Class

Edit: There is a more simple definition of my problem is in the 4th post of this topic.

Hi every body and thank you all for answering my earlier questions!
I read the tutorial to Polymorphism section and I wanted to make sure if I learnt it or N0T and I understood "or N0T" .
Here is the program I wanted to write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std ;

class Functions
{
    float Param1 ;
    public:
    float Constant (Param1)
    {
        cout << "Your function is this format:   f(x) = a .\nPlease enter a :     " ;
        void ValueSet (float UserParam1) {Param1 = UserParam1 ; }
        cout << "\n Now your function looks like this:   f(x) = " << Param1 ;
    }
} ;

int Main ()
{
    float UserParam1 = 2 ;
    Functions UserFuncFunction ;
    Functions :: Constant (UserParam1) ;
}


The whole idea was: I wanted to design a program that asks the user what type of function is yours? (Constant, Linear, Quadratic and ...) and I declared 4 parameters, But I couldn't even do the constant one. (The compiler didn't compile anything) How can I call a function from inside of a class?
I tried changing the Constant function to Class type, like this:
1
2
3
4
5
...
 public:
    Class Constant ()
    {
...


And can I design the class how that gets the input of a function inside it?
Last edited on
You may not define a function inside another function. So this definition is invalid

void ValueSet (float UserParam1) {Param1 = UserParam1 ; }
So!
wHAT sHOULD i DO?
cAN i DEFine a class into another class?
so I could make Constant a class and then ValueSet inside it?

But it didn't work. Unless I wrote the code wrong!

Edit
or can I define a class into a function?
Last edited on
My English is terrible and I cannot express myself or my question well.
This is what I wanted to do :
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
#include <iostream>
#include <string>
using namespace std ;

float Constant (float Param1)
{
    int main () ;
    cout << "\n\nNow your function is :   f(x) = " << Param1 << "\nIt is a constant function and can't accept any input.\n\n" ;
    main () ;
}

float Linear (float Param1 , float Param2 , float UserNumb)
{
    int main () ;
    char Replace ;
    cout << "\n\nNow your function is :   f(x) = " << Param1 <<"x + " << Param2 <<"\nIf we replace x with " << UserNumb << " , the result would be " << Param1 * UserNumb + Param2 ;
    ReplaceQ:
    cout << "\nIs there any other number you want to replace x with ? (y/n)" ;
    cin >> Replace ;
    if (Replace == 'y')
    {
        cout << "Enter it :  " ;
        cin >> UserNumb ;
        cout << "The result of f(x) with new input would be " << Param1 * UserNumb + Param2 << endl ;
        goto ReplaceQ ;
    }
    if (Replace == 'n')
    {
        main () ;
    }
}

int main ()
{
    string UserFunc ;
    float UserParam1 , UserParam2 , UserParam3 , UserParam4 , UserNumber ;
    cout << "What type is your function?\n1 - Constant\t\t\t2 - Linear\n3 - Quadratic\t\t\t4 - Cubic\n5 - Quartic\t\t\t6 - Quintic\n\nEnter it's name or it's number. (Don't use Capital Letters)\n" ;
    cin >> UserFunc ;
    if (UserFunc == "1" || UserFunc == "constant")
    {
        cout << "\n\nYour function looks like this :  f(x) = a\nPlease enter a :   " ;
        cin >> UserParam1 ;
        Constant (UserParam1) ;
    }
    if (UserFunc == "2" || UserFunc == "linear")
    {
        cout << "\n\nYour function looks like this :  f(x) = ax + b\nPlease enter a :   " ;
        cin >> UserParam1 ;
        cout << "Now enter b :   " ;
        cin >> UserParam2 ;
        cout << "Enter input of x : " ;
        cin >> UserNumber ;
        Linear (UserParam1 , UserParam2 , UserNumber) ;
    }
}


Of course it is not complete yet.
By the way this is what I wanted to do, but with the use of classes. My main Purpose is to learn how to use classes.
Please Help me.
Never ever call main. Never ever. If you want code to repeat, use a loop.

I see you're using goto as well. There are good times to use goto. This isn't one of them. Use loops.

As for using classes, this is not a great example. The point of classes is that you can make many objects of the same type. What you're doing is really not suited for classes.

@Moschops Thank you!
But how can I use a loop that connects functions to each other?
And about goto OK! They told me this before once, but I promise I won't use it again.

And the main thing: Well. This is not a great example. Can you give me a link that include examples good for class? THNX.
And you said the point of classes is that you can make many objects of the same type. That is what I didn't understand. I need to read the classes parts again, but can you explain me a bit more?well, with int I can make many objects of the same type too!!!!
Thank you again and sorry if I'm too Questionist.
But how can I use a loop that connects functions to each other?


1
2
3
4
5
6
7
8
9
10
11
int someFunction()
{
  char repeat = 'y';
  while (repeat == 'y')
  {
      someOtherFunction();
      cout << "WOuld you like to go around the loop again?" << endl;
      cin >> repeat;
  }
return 0;
}


well, with int I can make many objects of the same type too!!!!

Exactly. That's the point. You define a class when you decide that an int or a double or a float or a char or whatever isn't good enough and you need a new type. Then, once you've defined the class, you make as many of them as you like. Just like an int.
@Moschops Thank you again.
I got it all, expect one part: You said the way I can connect a function to another with aloop, but you also said I never ever call the main function. I think it's a paradox. Isn't it?
I think it's a paradox. Isn't it?

No. You don't call main. Why would that be a paradox?

You said the way I can connect a function to another

What does that even mean? You don't connect functions together. You call functions. You can call functions from other functions. But whatever you do, don't call the function named main.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
int someFunction()
{
  char repeat = 'y';
  while (repeat == 'y')
  {
      someOtherFunction();
      cout << "WOuld you like to go around the loop again?" << endl;
      cin >> repeat;
  }
return 0;
}

SomeFunction is the name of one of my functions.
SomeOrtherFunction is the main
Actually I want to rerun the program (from the starting point of main) whaen one of my functions.
Actually I want to rerun the program (from the starting point of main) whaen one of my functions.


1
2
3
4
5
6
7
int main()
{
  while (1) // loop forever
  {
      // all your code in here, when it gets to the end it will start back at the beginning
   }
}



If this is a surprise to you, you are trying something beyond your ability and you should stop and go back to the basics of C++. You need to learn about loops.
Thank you so much. Yeah. You're right. I should read better and more.
@AIVIO

You have mentioned before that you are form Iran, so I can understand how tough it is for you - with no access to Google for example.

There is a lot of documentation and reference on this site - at the top left of this page.

Here are some handy ideas:

How to quit out of a loop:

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
#include <iostream>
#include <locale>  //need this for std::toupper

//using namespace std   <--- don't do this

using std::cin;   //either do this, or put std:: before each std thing
using std::cout;  //I do this for things used a lot
using std::endl;  //then use std:: for things that are not

//Put function declarations here
float Constant (float Param1);

int main() {

     bool Quit = false;
     char answer = 'N';

    while(!Quit) {

           //your code here

           cout << "Do you want to Quit? Y or N" << endl;
           cin >> answer;
          
           answer = std::toupper(answer);  //avoids testing answer twice
           if(answer == 'Y')  //test variable once
                 Quit = true;
    }

return 0;  //if all is well, something else if not
} //end of main

//Put function definitions here

float Constant (float Param1) {
//code for function
return //a float variable
}


You can also use the break statement, to break out of a loop early, or the continue statement to restart the next iteration of the loop before the body of the loop code completes.

I would rather that beginners do something like this, rather than using infinite loops. There are situations when infinite loops are OK for advanced coders, but beginners should always try to supply an end condition for the loop.

Hope this helps and all goes well.
Last edited on
Topic archived. No new replies allowed.