Divide Large Class?

I

Immortal Nephi

A programmer designs one interface and tens or hundreds
implementations. They do not need to use inheritance hierarchy and
polymorphism for critical performance reasons. They only need
encapsulation and hiding information. You can link or plug Interface
object to inheritance hierarchy as long as you wish.
One class is called CInterface in header file. Some implementations
are in multiple source code files. Multiple source code files help to
be readable for the programmers so they don’t have to scroll down over
50,000 lines!!
One problem is on header file because CInterface class contains
hundreds or thousands member functions on over 50,000 lines or more.
The best practice is to break large code into smaller codes. Some
sub-member functions have small codes and one main member function
contains some sub-member functions.
My question is – how can one big class break into smaller sub-
classes. One sub-class is in each header file. All multiple header
files and source code files are hidden from the programmer.
The programmer can only use to include Interface.h if they want to
create one or more objects. They only need to use Run() member
function. The Run() member function does its own job to process
hundreds variables and member functions inside hiding information.

Let me give you an example of my code below. It may give you an idea
what it looks like. It can be CPU Emulator, Robot with hundreds
commands, game solver, etc...
The training C++ book makes a note. You can use STL first. If it
does not meet your requirement, then you can go ahead to write your
own codes for critical performance reasons.
Sometimes, critical performance tries to avoid complex logic and you
only need to write sequence order in each sub-member function.
Please let me know how you can break large class into sub-classes for
readability.

#ifndef __INTERFACE_H__
#define __INTERFACE_H__

class CInterface
{
public:
static bool stop_running;

CInterface() {}
~CInterface() {}
void Run() {}

private:
void Implementation_1();
void Implementation_2();
// ...
// ...
void Implementation_1000();

int variable_1;
int variable_2;
// ...
// ...
int variable_100;
};

bool CInterface::stop_running = false;

#endif // __INTERFACE_H__


// __BEGIN__IMPLEMENTATION_A_CPP__
#include "Interface.h"

void CInterface::Implementation_1() {}
// ...
// ...
void CInterface::Implementation_100() {}

// __END__IMPLEMENTATION_A_CPP__


// __BEGIN__IMPLEMENTATION_B_CPP__
#include "Interface.h"

void CInterface::Implementation_101() {}
// ...
// ...
void CInterface::Implementation_200() {}

// __END__IMPLEMENTATION_B_CPP__


// __BEGIN__IMPLEMENTATION_C_CPP__
#include "Interface.h"

void CInterface::Implementation_201() {}
// ...
// ...
void CInterface::Implementation_300() {}

// __END__IMPLEMENTATION_C_CPP__


// __BEGIN__MAIN_CPP__
#include "Interface.h"

int main()
{
CInterface interfaces[4];
int each_process = 0;

do
{
interfaces[each_process++].Run();
each_process &= 0x3;
}
while (CInterface::stop_running != true);

return 0;
}

// __END__MAIN_CPP__
 
I

Immortal Nephi

Immortal said:
[..]
   My question is – how can one big class break into smaller sub-
classes.  [..]

The answer is "inheritance".

     struct A { int a; void foo_a(); void bar_a(); };
     struct B { double b; void foo_b(); void bar_b(); };
     struct C { char c; void foo_c(); void bar_c(); };

     struct ABC: A, B, C {};

That can be seen as a better alternative to

     struct ABC {
         int a;
         double b;
         char c;

         void foo_a();
         void bar_a();
         void foo_b();
         void bar_b();
         void foo_c();
         void bar_c();
     };

Whether it makes sense or not, it's for the programmer to decide.  Of
course the problem with inheriting those classes is that 'A::foo_a' has
no way of getting to the 'C' part (so to speak).  So, if it needs to,
you would have to keep them together.  A possible alternative is to
extract all data into a separate class, a virtual base to all "parts":

     struct VeryBase { int a; double b; char c; };

     struct A : virtual VeryBase { void foo_a(); void bar_a(); };
     struct B : virtual VeryBase { void foo_b(); void bar_b(); };
     struct C : virtual VeryBase { void foo_c(); void bar_c(); };

     struct ABC: A, B, C {};
V,

I agree with you. Virtual base is needed like I described earlier on
another post before. I refer multiple diamonds meaning you can have
more than 10 sub-classes derived from VeryBase class. Then, all
variables and member functions are inherited to bottom subclass.
Variable as member data is always placed on VeryBase class because
each subclass need to modify data member from VeryBase class.

You can use Run() from ABC subclass as long as you wish. One problem
is that member function can't communicate between A, B, and/or C
subclasses so you have to go through ABC subclass.

For example.

ABC::Run()
{
(this->*pFunc)();
}

A::foo()
{
// do something
ABC::pFunc = &B:hoo; // Error pFunc is not member function to A, B, or
C.
}

ABC::Run()
{
(this->*pFunc)();
}

ABC::pFunc finished running A::foo() and then it assigns a pointer
function to B::Hoo(). Repeat ABC::pFunc inside ABC::Run(). It
repeats and choose one of hundred member function to be reassigned to
pFunc and goes again.

Do you understand what I am trying to explain?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top