-3

I have this class:

class A 
{
 public :

   int Echo (int param)
   {
      int num = param + 5;
      return num;
   }
}

And then in my Header.h I put the following definition:

#ifndef A_H
#define A_H
  class A 
{
 public :

   int Echo (int param1)
   {
          return num;
    }

#endif

I have these questions:

1- How classes should be defined in the Header.h? Should they be defined exactly like their original *.cpp file, or their signature is enough?

2- Should the definitions in Header file be accompanied with the return statement of the function? Someting like this:

int Echo(int param)
{
   return num;
}

I have asked similar question but with a different problem, and it is not acceptable in StackOverflow to edit questions which result in a totally different problem statement.

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105

1 Answers1

2

Your header file should be like this:

//something.h
#ifndef A_H
#define A_H

class A{
 public :
   int Echo (int param1);
};
#endif

That is called making a function prototype. You put the actual body of the function in a .cpp file:

//something.cpp

#include "main.h"

int A::Echo (int param)
{
   int num = param + 5;
   return num;
}

from your main file, include the header file and it should work. give the .cpp file to your compiler (if you are using an IDE it will do it for you) Example main file:

#include <iostream>
#include "main.h"

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   A a;
   cout << a.Echo(10) << endl;
   return 0;
}
while1fork
  • 36
  • 1