member function of classs

Hi friends i m not sure whether i can define a member function inside a class or not in C++. Is the following code is legal?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream> using namespace std;
class adder
{
private:
int a;
int b;
int c;
int answer;
public:
void getNums()
{
cout<<"enter numbers:"<<endl;
cin>>a>>b>>c;
}
void showRes()
{
answer=a+b+c;
cout<<''Sum of the numbers: ''<<answer<<endl;
}
}; 
Last edited on
Does it compile?
The answer to the above question is the same as the answer to your question.
There is no problem to define a member function within a class definition.
I got it. Thnx guys.
Script Coder wrote:
Does it compile?
The answer to the above question is the same as the answer to your question.


While that is true in this case... I wouldn't pitch that as if it were a golden rule to go by.

A lot of non-standard code that shouldn't work often compiles and does "work". Especially on older compilers.

So IMO we shouldn't discourage people from asking whether or not something is legal.
Last edited on
However a template member function may not be defined within a local class definition.:)
u can define a member function inside a class.
Topic archived. No new replies allowed.