Sorry for the Noob question .I have been learning C++ for a while and from the book "Visual C++" by Ivor Horton I see that when extending some class the methods overriding is done in the header of the derived class.I haven't found any example where it can be done in .cpp files.So my question is if the .cpp files can only contains the "native" methods of the current class?Or there is a way also to override parent methods there.
-
3Reading your question, I wonder if this book explains the purposes of header/source files well enough. – ereOn Sep 19 '11 at 06:40
-
Not so much.This is really a "getting started" kind of book . – Michael IV Sep 19 '11 at 07:00
-
1This is a very important concept. I'm not sure one can efficiently learn C++ (or C for that matter) without understanding the header/source file mechanism. May I suggest you also read another resource explaining that ? It would help and you would probably understand better what is weird about your current question. – ereOn Sep 19 '11 at 07:04
-
Can you suggest a better reading source (book,article ,etc) on C++ which is not too advanced still? – Michael IV Sep 19 '11 at 07:08
-
1Here is a [complete detailed list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which you can pick from :) – ereOn Sep 19 '11 at 07:12
3 Answers
Method overriding is basically done to acheive polymorphic behavior wherein the Derived class re-implements the Base class methods suitable to its own use. So yes usually methods are overriden in dervied class.
By the way usually methods are declared in the header files and defined in the source files, So I am not sure what you exactly mean.
Probably, You should have a look at this,
What is the difference between a definition and a declaration?
-
"So yes usually methods are overriden in dervied class." So when aren't they? In my question I imply that the base class has all its methods declared in a header file. – Michael IV Sep 19 '11 at 07:05
I see that when extending some class the methods overriding is done in the header of the derived class.
Not essentially. Header is there for declarations and implementation can go in a source file. It doesn't matter even it is for polymorphic methods or native member functions.

- 34,573
- 20
- 89
- 115
-
-
I tried to override a method of the base class(which is declared in the base class's header) in derived .cpp.It doesn't work.I see that I can do it only inside the header of the derived. – Michael IV Sep 19 '11 at 07:04
-
Post it on the question of what you are actually trying to do and what is the exact error message. You might be doing a simple mistake. – Mahesh Sep 19 '11 at 07:05
You can use .h and .cpp as you wish. You can have both declaration and definition in both .h and .cpp's. Not that you should, but you can. cpp's are compiled by the compiler, if you include your .h in a cpp, it will also be compiled. So to answer your question, yes, you can override in the cpp.
Example:
A.h
class A{
public:
A() {};//definition and declaration in header
virtual void foo() {};
};
B.cpp
#include "A.h"
class B : public A{
public:
B() {};//definition and declaration in source file
virtual void foo() {};
};
int main()
{
A* pA = new B;
pA->foo(); //will call foo from B
return 0;
}
B will however only be visible in B.cpp
.
Don't do it thoguht!

- 253,575
- 64
- 457
- 625
-
I understand what you a re trying to show here but my question is actually if the content of the overridden "foo()" in the class B can be written also in B's .cpp just like a regular definition ? – Michael IV Sep 19 '11 at 07:13
-
You know what ,I got it now where was my mistake.So I can declare the function I need to override in the derived class's header and then put all the content I need into the definition of it in the .cpp. – Michael IV Sep 19 '11 at 07:17