Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One class with multiple implementation files

Tags:

Is there anything wrong with having a single class (one .h) implemented over multiple source files? I realize that this could be a symptom of too much code in a single class, but is there anything technically wrong with it?

For instance:

Foo.h

class Foo
{
   void Read();
   void Write();
   void Run();
}

Foo.Read.cpp

#include "Foo.h"
void Foo::Read()
{
}

Foo.Write.cpp

#include "Foo.h"
void Foo::Write()
{
}

Foo.Run.cpp

#include "Foo.h"
void Foo::Run()
{
}
like image 526
Andrew Garrison Avatar asked Oct 16 '09 15:10

Andrew Garrison


People also ask

Should each class have its own file C++?

Each class shall have it's own header and implementation file. So if we wrote a class called MyString we would need an associated MyStringh. h and MyString.

How do we implement multiple source program files?

Split the program into three files, main. c, which contains main(), node. h, the header which ensures declarations are common across all the program, and hence is understood by the compiler, and node. c, the functions which manipulate the NODE structure.

What is multiple file compilation?

Multiple File Projects: Most of the time, full programs are not contained in a single file. Many small programs are easy to write in a single file, but larger programs involve separate files containing different modules. Usually, files are separated by related content.


1 Answers

This is fine. In the end, it will be all linked together.

I have even seen code, where every member function was in a different *.cpp file.

like image 191
Gunther Piez Avatar answered Oct 13 '22 17:10

Gunther Piez