OOP in C++
Suggested Coding Standard
The following is a
suggested coding standard/style. It is important to follow a standard when
developing code in order to be consistent, readable and maintainable. Hopefully
you will apply this or some thing better than this in your software development
practice.
·
Write only one class per .h and .cpp file
·
For each class that you write, in the top of the
.h file write a paragraph explaining the purpose of the class
·
For each function/method that you write in the
class, right below the declaration of the method, write a few lines explaining
the purpose of the function and the nature/requirement of the input arguments
and the result(s)
·
In the .cpp file, write a few lines of comments
where ever you do some thing that is a little bit more complicated, or not very
obvious.
·
Write your variable names so that they are
readable and meaningful. For instance, instead of i, t, val, tem, etc. use
words that mean something in your program, use names that relate to some logic
in your business logic, like initialTemperature, temperature, etc.
·
Use mixed case for class names, example,
BankAccount instead of Bankaccount
·
Begin class names with an upper case letter,
Bank instead of bank. If using the C in front of class name (like most Microsoft’s
classes), make the second letter caps as well, CBank instead of Cbank.
·
Begin all variables and methods with lower case
letters (methods in MFC begin with upper case letters, I like to begin with
lower case, but what ever you use, be consistent with your own code). Use mixed
case. If your variable name or method name has multiple words, use upper case
for first letter of each word. Examples:
int
temperature;
bool
switchStatus;
double
upperLimit;
If
you use hungarian notation, then the
variable names begin with upper case after the m_typePrefix. Again, be
consistent with your style.
void
closeWindow();
void
takeUmbrella();
·
You may use hungarian notation for variable
names (see below for details)
·
Make all member variables private unless you
have a very justifiable reason
·
Make helper methods private
·
Use proper indentation in your code. For
example:
if
(raining)
{
lockCarWindow();
takeUmbrella();
}
·
Use as much free space and extra lines as you
can to make the code more readable:
if(value<0) { return noWay; }
else{ okToDeposit = true;}
proceedWithDeposit();
You may write the same code as
follows:
if (value < 0)
{
return
noWay;
}
else
{
okToDeposit
= true;
}
proceedWithDeposit();
Commenting:
·
In the header file (.h) write a few lines (4-5)
describing the class.
·
For each function (in .h file) write
·
one line describing the purpose of that
function.
·
one line describing the input requirements and
required state of the object
·
one line describing the output and resulting
state of the object
·
one line with a list of exceptions being thrown
·
For each function (in .cc file) write comments
where ever necessary describing
some
things that are special or that may not be obvious to the general reader.
Hungarian
Notation (This notation has fallen out of flavor. Some people use m_varname ignoring the type):
MSVC++ uses the
Hungarian Notation by Charles Simonyi of Microsoft. In this convention,
variable names are prefixed with type information.
The common prefixes
are given below:
Prefix Type
i int
n short
u unsigned
b boolean
l long
f float
d double
c char
s char
array, string
p pointer
m_ member variable
g_ global variable
Example:
int m_iCount; // member variable
Count of integer type
double g_dPressure; // Pressure is a
global variable, of type double
CAccount* m_pAccount; // a member
variable that is a pointer to Account.
//An example that uses
these conventions:
/************************************************************/
// File: Account.h
/*
The purpose of the
CAccount class is to abstract a simple Bank Account. It provides functions to
deposit money, withdraw money and obtain the balance information. It takes care
of making sure that the balance does not drop below 0. Any thing else that you
want to convey to the developer not familiar with your code (this may be
yourself after a vacation or holiday!). */
class CAccount {
public:
CAccount(double initialBalance);
void withdraw(double amount);
// Purpose: To withdraw money from
the account.
// Requirement: current balance >
0
// Promise: Reduces the balance by
the said amount, provided the
// amount
is less than the current balance.
// Exception: Throws
CAccount::InsufficientFund if balance
< amount.
void deposit(double amount);
// Purpose: To deposit money into
the account.
// Requirement: -
// Promise: Increases the balance by
the given amount.
// Exception: Throws
CAccount::InvalidAmount if amount < 0.
double getBalance() const;
// Purpose: Returns the balance of
the account.
// Requirement: -
// Promise: -
// Exception: -
// The InsufficientFund is an
exception class that indicates the condition when the balance is
//
too low to perform some transactions.
class InsufficientFund {};
// The InvalidAmount is an exception
class...
class InvalidAmount{};
private:
double m_dBalance;
};
/************************************************************/
//File: Account.cpp
#include
<Account.h>
CAccount::CAccount(double
initialBalance)
{
m_dBalance = initialBalance;
}
void withdraw(double
amount)
{
if (m_dBalance < amount)
{
throw
InsufficientFund();
}
m_dBalance -= amount;
}
void deposit(double
amount)
{
// Let us check the unlikely
situation when amount is
// negative
if (amount < 0)
{
throw InvalidAmount();
}
m_dBalance += amount;
}