Can you please advise me how to write two separated objects?
Sometimes, two objects have the same variable name and function name, but
they have different algorithm to perform. The C++ Compiler allows you to
create two classes. Two classes bind their own local variables and local
functions.
For example:
Class Blue
{
int a;
int b;
int Run_Color (void);
};
Class Red
{
int a;
int b;
int Run_Color (void);
};
int main (void)
{
Blue blue;
Red red;
blue.a = 10;
blue.b = 20;
blue.Run_Color ();
red.a = 2;
red.b = 4;
red.Run_Color ();
return 0;
}
I am not going to use C++ Compiler. I stick C Compiler for
critical performance reason. The same global variable and global function
will be in two separated header / source codes. The main source code
includes two headers.
One problem is that main source code can't access the same global
variable name and global function name in both headers. It is only the
solution if you want to name two separated global scopes. C++ Compiler has
the feature to use namespace keyword.
How would you suggest to write this way using C Compiler? It
should look like this below.
namespace Blue
{
int a;
int b;
int Run_Color (void);
}
namespace Red
{
int a;
int b;
int Run_Color (void);
}
int main (void)
{
Blue::a = 10;
Blue::b = 20;
Blue::Run_Color (void);
Red::a = 2;
Red::b = 4;
Red::Run_Color (void);
Return 0;
}
If it does not have the solution, then write a separated function
name using underscore like this - Blue__Run_Color (), Red__Run_Color ().
Blue__a = 2; Red__a = 10.
It does not look neat design. What do you recommend?
Yours Truly,
Bryan Parkoff
Sometimes, two objects have the same variable name and function name, but
they have different algorithm to perform. The C++ Compiler allows you to
create two classes. Two classes bind their own local variables and local
functions.
For example:
Class Blue
{
int a;
int b;
int Run_Color (void);
};
Class Red
{
int a;
int b;
int Run_Color (void);
};
int main (void)
{
Blue blue;
Red red;
blue.a = 10;
blue.b = 20;
blue.Run_Color ();
red.a = 2;
red.b = 4;
red.Run_Color ();
return 0;
}
I am not going to use C++ Compiler. I stick C Compiler for
critical performance reason. The same global variable and global function
will be in two separated header / source codes. The main source code
includes two headers.
One problem is that main source code can't access the same global
variable name and global function name in both headers. It is only the
solution if you want to name two separated global scopes. C++ Compiler has
the feature to use namespace keyword.
How would you suggest to write this way using C Compiler? It
should look like this below.
namespace Blue
{
int a;
int b;
int Run_Color (void);
}
namespace Red
{
int a;
int b;
int Run_Color (void);
}
int main (void)
{
Blue::a = 10;
Blue::b = 20;
Blue::Run_Color (void);
Red::a = 2;
Red::b = 4;
Red::Run_Color (void);
Return 0;
}
If it does not have the solution, then write a separated function
name using underscore like this - Blue__Run_Color (), Red__Run_Color ().
Blue__a = 2; Red__a = 10.
It does not look neat design. What do you recommend?
Yours Truly,
Bryan Parkoff
Comment