473,738 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to design C++ OOP better?

I have an idea how to design an object in a better way. I would like
to give you
my thought example. Please let me know what you think if it is best
object design.
Please recommend me any book which it teaches me how to design C++ OOP
better as long
as I know how to program OOP in C++.
Think of ancient 6502 microprocessor which it was used for Commodore,
Atari, and
Apple II. This MPU_6502 is an object of MPU_6502 class. All member
variables are
hidden like internal registers of A, X, and Y. Only address bus and
data bus
member variables are available using Set() and Get() functions.
This MPU_6502 object can be reused to the client users. What happen
if
programmers want to create another object which it is called
MPU_6502_Debug. This
MPU_6502 object guards against MPU_6502_Debug from accessing internal
registers
using Set() and Get() functions. MPU_6502_Debug object needs to find
a way how to
obtain internal registers from MPU_6502 object.
The friend keyword may be the option so friend class MPU_6502_Debug
can be added
into MPU_6502 class and you create a pointer in MPU_6502_Debug object
to access
MPU_6502 object's internal registers.
If you think that friend keyword is a bad OOP design, please offer me
another
good example how MPU_6502 object and MPU_6502_Debug object can be done
to process
internal registers inside main() function.
Please advise. I appreciate your help. Take a look at my C++ OOP
code below. They are too short codes as examples.

class MPU_6502
{
public:
MPU_6502() {}
~MPU_6502() {}
void Run() { /* Process Address Bus and Data Bus to modify internal
A, X, and Y registers */ }

unsigned int Get_Address_Bus () { return Address_Bus; }
unsigned int Get_Data_Bus() { return Data_Bus; }
void Set_Data_Bus(un signed int data_bus) { Data_Bus = data_bus; }

// You are not allowed to use Set() & Get() functions below as a true
black box.

// void Set_A(unsigned int a) { A = a; }
// void Set_X(unsigned int x) { X = x; }
// void Set_Y(unsigned int y) { Y = y; }

// unsigned int Get_A() const { return A; }
// unsigned int Get_X() const { return X; }
// unsigned int Get_Y() const { return Y; }

private:
unsigned int A;
unsigned int X;
unsigned int Y;

unsigned int Address_Bus;
unsigned int Data_Bus;
};

class MPU_6502_Debug
{
public:
MPU_6502_Debug( ) {}
~MPU_6502_Debug () {}
void Run()
{ /* Need to get internal registers from MPU_6502 object to process
debugging */ }

void Set_A(unsigned int a) { A = a; }
void Set_X(unsigned int x) { X = x; }
void Set_Y(unsigned int y) { Y = y; }

private:
unsigned int A;
unsigned int X;
unsigned int Y;
};

int main(void)
{
unsigned char* RAM = new unsigned char [0x10000];

MPU_6502 mpu;

mpu.Run(); // Run many times every time address bus comes.
mpu.Set_Data_Bu s( RAM[ mpu.Get_Address _Bus() ] ); // Get Data bus
from RAM's address bus
mpu.Run(); // Continues running as long as it can.

/* This MPU_6502 object is a black box like a chip. This chip has
only address bus and
data bus. You send message to MPU_6502 object to retreive address
bus and data
bus. It does not allow you to access internal registers, but only
Run() can do process
internal registers. No Get internal registers functions and set
internal registers
functions are allowed to be used to the client. This MPU_6502 object
is a good
object design for best data protection. */

MPU_6502_Debug mpu_debug; // You can define MPU_6502_Debug object
anytime you want.

for (int loop = 0; loop < 10; loop++) // can be endless loop as you
want.
{
mpu.Run();
mpu_debug.Set_A ( 0x40 ); // How can you do it?
mpu_debug.Set_X ( 0xC1 ); // How can you do it?
mpu_debug.Set_Y ( 0x80 ); // How can you do it?
mpu_debug.Run() ;
}

delete [] RAM;

return 0;
}

Nephi
Aug 30 '08 #1
7 1718
Immortal Nephi wrote:
I have an idea how to design an object in a better way. I would like
to give you
my thought example. Please let me know what you think if it is best
object design.
Can you fix your like wrap and post again? This is very had to follow.

--
Ian Collins.
Aug 30 '08 #2
Sam
Immortal Nephi writes:
[ somewhat complicated design and situation ]
This MPU_6502 object can be reused to the client users. What happen
if
programmers want to create another object which it is called
MPU_6502_Debug. This
MPU_6502 object guards against MPU_6502_Debug from accessing internal
registers
using Set() and Get() functions. MPU_6502_Debug object needs to find
a way how to
obtain internal registers from MPU_6502 object.
[ … ]
I think I understand what you're trying to do. There are a number of ways of
approaching this design that's probably cleaner, and avoids having to
declare a friend class.

Consider placing all the registers as protected members of MPU_6502_Debug:

class MPU_6502_debug {

protected:
unsigned int A;
unsigned int X;
unsigned int Y;

unsigned int Address_Bus;
unsigned int Data_Bus;

public:

// Your various get and set methods go here.
};

Then, privately subclass MPU_6502_Debug,

class MPU_6502 : private MPU_6502_Debug {

public:
// Your MPU_6502 functions

MPU_6502_Debug &getDebug() { return *this; }

const MPU_6502_Debug &getDebug() const { return *this; }
};

The MPU_6502 subclass has direct access to the registers, from its
superclass, which is private and cannot be directly accessed. Use the
getDebug() method to retrieve the reference to the superclass, which gives
you full access. The nice thing about this approach is that in contexts
where your MPU_6502 object is constant, you'll only have access to the
constant debug superclass object reference, and, consequently, only the
constant get() methods in the superclass.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAki 4vW0ACgkQx9p3GY HlUOIiJgCggCGJ3 v+MQRFLH90L9xQn 0ii9
VEcAn2Xaq7D39Wv 3DLjOafq+LvKbKx Fp
=CRmg
-----END PGP SIGNATURE-----

Aug 30 '08 #3
On Aug 29, 10:24*pm, Sam <s...@email-scan.comwrote:
*application_pg p-signature_part
< 1KViewDownload

Immortal Nephi writes:
[ somewhat complicated design and situation ]
* *This MPU_6502 object can be reused to the client users. *What happen
if
programmers want to create another object which it is called
MPU_6502_Debug. *This
MPU_6502 object guards against MPU_6502_Debug from accessing internal
registers
using Set() and Get() functions. *MPU_6502_Debug object needs to find
a way how to
obtain internal registers from MPU_6502 object.
[ … ]

I think I understand what you're trying to do. There are a number of waysof
approaching this design that's probably cleaner, and avoids having to
declare a friend class.

Consider placing all the registers as protected members of MPU_6502_Debug:

class MPU_6502_debug {

protected:
* * unsigned int A;
* * unsigned int X;
* * unsigned int Y;

* * unsigned int Address_Bus;
* * unsigned int Data_Bus;

public:

* * // Your various get and set methods go here.

};

Then, privately subclass MPU_6502_Debug,

class MPU_6502 : private MPU_6502_Debug {

public:
* * * // Your MPU_6502 functions

* * * MPU_6502_Debug &getDebug() { return *this; }

* * * const MPU_6502_Debug &getDebug() const { return *this; }

};

The MPU_6502 subclass has direct access to the registers, from its
superclass, which is private and cannot be directly accessed. Use the
getDebug() method to retrieve the reference to the superclass, which gives
you full access. The nice thing about this approach is that in contexts
where your MPU_6502 object is constant, you'll only have access to the
constant debug superclass object reference, and, consequently, only the
constant get() methods in the superclass.
Thanks for provding me subclass example. Can you please add main()
function example? This main() function helps you to define MPU_6502
object and MPU_6502_Debug object. What do this look like? The client
user can decide if they want to define only MPU_6502 object in this
main() function. Sometimes, they want to define both MU_6502 object
and MPU_6502_Debug object if they need MPU_6502_Deug object.

I take more time to learn how to write C++ OOP in a better design way.

Thanks again.

Nephi
Aug 30 '08 #4
In article <1872f022-3fc0-4ff6-bc94-
92**********@d1 g2000hsg.google groups.com>, Im************@ satx.rr.com
says...
I have an idea how to design an object in a better way.
The first part of good design (OO or otherwise) is applying knowledge of
the domain, first acquiring that knowledge if necessary.

In this case, it looks to me like you're trying to create a design that
just doesn't work much like a real 6502 microprocessor did. For example,
you mention making the address bus available to external code via get
and set methods. On the 6502, the address bus was output-only, so an
emulator that took inputs on the address bus wouldn't be very accurate
at all.

An accurate emulation of a microprocessor starts with its data sheet.
Generally speaking, you have inputs that correspond to its input pins,
and outputs that correspond to its output pins. Depending on how
detailed an emulation you want to provide, you may or may not want to
emulate (for one example) its address bus directly -- you may prefer to
simply have an array (or vector, etc.) available as its memory, and the
"address bus" will be implicit in the subscripts supplied to that array.

I'm not sure, but it sounds like your real question was about whether it
was a good idea to make the debugging class a friend of the
microprocessor class. While that's certainly a valid possibility, I
think I'd use inheritance:

class MPU_6502 {
// Note: incomplete, untested, etc.
protected:
char A, X, Y, F, SP;
short PC;
enum flags { carry=1,
zero=2,
IRQ_mask = 4,
decimal = 8,
BRK = 16,
OV = 64,
negative = 128
};
public:
reset() {
A=F=X=Y=0;
SP = 0xff; // ?
PC=0xFFFC;
}
SO() { F |= OV; }

// Add functions for IRQ, NMI, etc.
};

class ICE_6502 : public MPU_6502 {
// extra debug access here
};

This corresponds fairly closely to reality: the real 6502 provides only
certain specific access to the internals, but there is also such a thing
as a ICE (In-circuit emulator) that can be substituted for the real
processor, which also provides more access to processor internals. This
does require making most of the internals protected instead of private,
but IMO this is a fairly minor problem -- you're not dealing with a
large class hierarchy here.

From there, you're left with questions about the emulation you want to
provide. This includes both whether you emulate the processor bugs or
not, and the level of detail you want to provide. An emulator to play
Apple/Atari/Commodore games will generally be quite different from one
that lets you see how whether (for example you can use a clock signal
with a given amount of noise or not.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 30 '08 #5
On Aug 30, 9:45*am, Jerry Coffin <jcof...@taeus. comwrote:
In article <1872f022-3fc0-4ff6-bc94-
9237327b4...@d1 g2000hsg.google groups.com>, Immortal_Ne...@ satx.rr.com
says...
Jerry,
* *I have an idea how to design an object in a better way.

The first part of good design (OO or otherwise) is applying knowledge of
the domain, first acquiring that knowledge if necessary.

In this case, it looks to me like you're trying to create a design that
just doesn't work much like a real 6502 microprocessor did. For example,
you mention making the address bus available to external code via get
and set methods. On the 6502, the address bus was output-only, so an
emulator that took inputs on the address bus wouldn't be very accurate
at all.

An accurate emulation of a microprocessor starts with its data sheet.
Generally speaking, you have inputs that correspond to its input pins,
and outputs that correspond to its output pins. Depending on how
detailed an emulation you want to provide, you may or may not want to
emulate (for one example) its address bus directly -- you may prefer to
simply have an array (or vector, etc.) available as its memory, and the
"address bus" will be implicit in the subscripts supplied to that array.

I'm not sure, but it sounds like your real question was about whether it
was a good idea to make the debugging class a friend of the
microprocessor class. While that's certainly a valid possibility, I
think I'd use inheritance:
I want to thank you for a feedback how 6502 microprocessor
works. Fortunately, I have already written 6502 simulator as a real
6502 MPU chip. My test shows that Run() function processes address
bus, data bus, and internal registers once as one clock cycle. You
can execute Run() function many times as you wish so it processes each
clock cycle.
According to my 6502 research, my MPU_6502 object behaves as real
chip as 6502 simulator very accurately. Unlikely, other emulators
don't do process each clock cycle.
class MPU_6502 {
* * * * // Note: incomplete, untested, etc.
protected:
* * * * char A, X, Y, F, SP;
* * * * short PC;
* * * * enum flags { carry=1,
* * * * * * * * * * * * zero=2,
* * * * * * * * * * * * IRQ_mask = 4,
* * * * * * * * * * * * decimal = 8,
* * * * * * * * * * * * BRK = 16,
* * * * * * * * * * * * OV = 64,
* * * * * * * * * * * * negative = 128
* * * * };
public:
* * * * reset() {
* * * * * * * * A=F=X=Y=0;
* * * * * * * * SP = 0xff; * * *// ?
* * * * * * * * PC=0xFFFC;
* * * * }
* * * * SO() { F |= OV; }

* * * * // Add functions for IRQ, NMI, etc.

};

class ICE_6502 : public MPU_6502 {
* * * * // extra debug access here

};
Nice class design. It is much similiar as my written MPU_6502
class. Unfortunately, it does not work. If you want to define
MPU_6502 object and ICE_6502 object in main() function, inheritance is
not an answer because MPU_6502 object and ICE_6502 object have their
own copy of member variables.
You can create two base classes of MPU_6502 and ICE_6502. You
don't need inheritance. You define MPU_6502 object first and then
ICE_6502 object second. You need to put friend class ICE_6502 into
MPU_6502 class. You set up a pointer inside ICE_6502 class so
ICE_6502 class' member function can access MPU_6502 class' member
variables through a pointer.
Another programmer in this post mentioned differently. He does
not recommend friend. He thinks that ICE_6502 class should be base
and then MPU_6502 class should be derived through private
inheritance. I can't sustain their opinions to think if C++ OOP has a
better design.

Nephi
This corresponds fairly closely to reality: the real 6502 provides only
certain specific access to the internals, but there is also such a thing
as a ICE (In-circuit emulator) that can be substituted for the real
processor, which also provides more access to processor internals. This
does require making most of the internals protected instead of private,
but IMO this is a fairly minor problem -- you're not dealing with a
large class hierarchy here.

From there, you're left with questions about the emulation you want to
provide. This includes both whether you emulate the processor bugs or
not, and the level of detail you want to provide. An emulator to play
Apple/Atari/Commodore games will generally be quite different from one
that lets you see how whether (for example you can use a clock signal
with a given amount of noise or not.

--
* * Later,
* * Jerry.

The universe is a figment of its own imagination.
Aug 30 '08 #6
Ian Collins wrote:
Immortal Nephi wrote:
> I have an idea how to design an object in a better way. I would like
to give you
my thought example. Please let me know what you think if it is best
object design.

Can you fix your like wrap and post again? This is very had to follow.
I agree with you, it's hard to read.
Sep 1 '08 #7
On Aug 30, 11:53 am, Immortal Nephi <Immortal_Ne... @satx.rr.com>
wrote:
This MPU_6502 object can be reused to the client users.
What happen if programmers want to create another object which
it is called MPU_6502_Debug. This MPU_6502 object guards against
MPU_6502_Debug from accessing internal registers
using Set() and Get() functions. MPU_6502_Debug object needs to find
a way how to obtain internal registers from MPU_6502 object.

The friend keyword may be the option so friend class
MPU_6502_Debug can be added...
friend is appropriate here. It documents that a specific Debug object
is granted exceptional access. The derivation approaches mentioned in
other responses create an entirely different impression: that MPU_6502
is intended as a base class. On reading the code, a completely
different set of questions and impressions is raised regarding
MPU_6502's design and intended usage. In general, that's probably not
desirable - supporting some debugging functionality shouldn't be so
invasive, driving your OO model.

Tony
Sep 1 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
6394
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
1
1255
by: Chua Wen Ching | last post by:
Hi there, I had a scenario here. I had many tonnes of C codes in my office. Each C codes are used in low level stuff. Like connecting to smart card readers or even the OS itself. I was being hired to push .NET into it and fully object oriented. I had some questions to ask.
17
2710
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
105
5333
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does not work. It only works on lists and strings, for no obvious reason. Why not on all sequence types? Or, another example, the index() method has start and end parameters for lists and strings. The count() method also has start and end parameters...
5
1768
by: A_M_IS | last post by:
Dear valuable experts, I truly hope than You can suggest for me Your ideas how to resolve design. I developing relative small Access VB tool, for single user use only. Access version 2003, but db saved for compatibility as ver. 2000. Basic structure of this tool is dependent from my house departments structure. Each department owes their own table, where they can write inputs necessary and dependent for annual budget analysis and...
0
2508
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
17
4849
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This Page Is Valid XHTML 1.0 Transitional!" but it still wouldn't look the same. It is on http://www.dvdnowkiosks.com/new/theproduct.php scroll down and recognize the black bottom bar when you go ewith firefox(2.0) which isn't there with IE7. Why does...
6
2136
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
0
1995
by: AMDRIT | last post by:
I am looking for better concrete examples, as I am a bit dense, on design patterns that facilitate my goals. I have been out to the code project, planet source code, and microsoft's patterns and practices site and it just isn't sinking in all that clearly to me. Currently we have code in production and it all works well, however it is not the way we want it. We know that we can implement a better design plan to improve performance,...
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.