Avatar of XPUSR
XPUSR

asked on 

Design Question mem vars

I have two very basic quesions which I hope someone could help me out with:

Say I have a class

CAccount
with member function PrintAccountName()
with private member variable m_dbAccountNumber.

Is it good practice to access the member variable directly in its own class or should I have a GetAccount() and SetAccount public member functions to access the variablei.e.

 CAccount::PrintAccountNumber()
{
             cout<<m_dbAccountNumber;
}



2.
System Programming

Avatar of undefined
Last Comment
Knut Hunstad
Avatar of XPUSR
XPUSR

ASKER

Is there any good book/site that deals with such basic class design issues?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

What is probably the *better* way would be

LPCTSTR CAccount::GetAccountNumber()
{
             return m_dbAccountNumber;
}

(assuming the account number is a string)

Reason - if you wanted to check validity then it is much easier to do it in one place rather than all the places you use the account number.

Also should you need to make a change then you only need to change the code in one place - the class.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Similar code for setting the account number.
Avatar of XPUSR
XPUSR

ASKER

Thanks AndyAinscow,

But that doesn't really answer my question. If I access m_dbAccountNumber directly I still only need to change the code once. My question is is there a neet to use a get method?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

There is no need to use functions to Get/Set the variables however there are advantages (as I said initially).
ASKER CERTIFIED SOLUTION
Avatar of Knut Hunstad
Knut Hunstad
Flag of Norway image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
System Programming
System Programming

Kernel and system programming is the process of creating the software necessary for a computer or device to function and operate other programs. Some operating systems (such as Microsoft Windows) are proprietary, but others, such as the various Linux distributions, are open source.

41K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo