arguments of the class function

Hello!
Please, constructor has agruments but sum function not. Is there a definition that makes it clear? Many thanks!!!
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
#include <iostream>
    using namespace std;
     
    class First{
     public:
    int a;
    int b;
     
    public:
    First(int c,int d):a(c), b(d){};
     
    int sum(){ return (a+b); }
     
    };
     
    int mmm(int x, int y) {
    int z=x+y;
    return z;
    }
     
     
    int main(){
     
    First sum1(2,3);
    First sum2(4,5);
     
    cout<<sum1.sum()<<endl;
    cout<<sum2.sum()<<endl;
     
     
        int r;
    
  
    r = mmm(sum1.a, sum1.b);
     
     
    cout<< r <<endl;
     
    return 0;
    }
 


Last edited on
constructor has agruments but sum function not. Is there a definition that makes it clear?
1
2
3
4
5
6
class First {
  // code
public:
  First( int, int );
  int sum();
};

I do not understand your question, because your class definition clearly defines what arguments each member function of your class requires.
Last edited on
Hello!

Why not:

int sum(int a, int b)...


that is what I ment!
Think. What is the purpose of a function?


Of course you can have a function, like the mmm, that returns the sum of its arguments, but what does that have to do with First? The members of First are the interface of First and determine how other code can/must interact with type First objects.
Hello!
Is that because the objects in that moment still don't exist?
What moment?
Topic archived. No new replies allowed.