1

I have a base class Student, and an inheriting class StudentAtA.

I define StudentAtA inside StudentAtA.h, and it overides some of the methods of Student.

For example, if Student has:

string returnUni() 
{
 return NULL;
};

Then I define inside StudentAtA.h an overriding method:

string returnUni() 
{
 return "A";
};

Since all the methods in StudentAtA are short, they're all implemented in the header file (I didn't create a StudentAtA.cpp file).

Now I have a Driver.cpp file which uses StudentAtA, and contains a main function. This is the executor.

Is it possible to compile Driver without having StudentAtA.cpp (just using the header)?

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26
Paz
  • 737
  • 7
  • 22
  • as long as your compiler can find the definition for your methods it's happy. However it's not recommended to put the method bodys inside the header file. – Glandy Dec 29 '13 at 09:05
  • Even if the methods are 1-2 lines long? (I'm new to c\c++ and aren't familiar with the standards) – Paz Dec 29 '13 at 09:14
  • 1
    yes even then: http://stackoverflow.com/a/4955288/3060253 this guy summed up the reasons – Glandy Dec 29 '13 at 09:24
  • @Glandy The discussion behind that link is, fortunately, a bit more balanced than your summary. There’s nothing absolutely wrong with method bodies in header files, it’s a balance between tighter coupling and giving the compiler more options for optimization. Obviously, the method listed above should be `virtual`, and in that case, there’s probably no reason to have it in a header, since it will hardly ever be eligible for inlining. – Christopher Creutzig Dec 29 '13 at 10:02

1 Answers1

3

yes possible. Just make sure you include StudentAtA.h in your Driver.cpp

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31