<< Chapter < Page Chapter >> Page >

Preventing multiple inclusion

Large class-based programs are sometimes composed of multiple interface and implementation files. With large program, you need to ensure that you do not include multiple instances of the same header file when you compile the program, since multiple inclusion will make your program unnecessary large.

C++ generates an error if you attempt to compile a program that includes multiple instances of the same header file. To prevent this kind of error, most C++ programmers use the #define preprocessor directive with the #if and #endif preprocessor directives in header files.

The #if and #endif preprocessor directives determine which portions of a file to compile depending on the result of a conditional expression.

The syntax for the #if and #endif preprocessor directives:

#if conditional expression

statements to compile;

#endif

Example:

#if !defined(TIME1_H)

#define TIME1_H

class Time {

public:

Time();

void setTime( int, int, int );

void printMilitary();

void printStandard();

private:

int hour;

int minute;

int second;

};

#endif

Note: Common practice when defining a header file’s constant is to use the header file’s name in uppercase letters appended with H. For example, the constant for the time1.h header file is usually defined as TIME1_H.

Member functions

In this section, we learn how to write member functions for a class.

Inline functions

Although member functions are usually defined in an implementation file, they can also be defined in an interface file. Functions defined inside the class body in an interface file are called inline functions .

Example:

class Stocks {

public:

double getTotalValue(int iShares, double dCurPrice){

double dCurrentValue;

iNumShares = iShares;

dCurrentPricePerShare = dCurPrice;

dCurrentValue = iNumShares*dCurrentPricePerShare;

return dCurrentValue;

}

private:

int iNumShares;

double dPurchasePricePerShare;

double dCurrentPricePerShare;

};

Diagram of class stock

Member functions in implementation file

Member function definitions are always placed in the implementation file.

In the example below, for the class Stocks, the definition of the member function getTotalValue is placed in the source-code file stocks.cpp in which the main program is also included.

Example

//stocks.h ---------------- interface section

#if !defined(STOCKS_H)

#define STOCKS_H

class Stocks{

public:

double getTotalValue(int iShares, double dCurPrice);

private:

int iNumShares;

double dPurchasePricePerShare;

double dCurrentPricePerShare;

};

#endif

// stocks.cpp ----------------- implementation section

#include “stocks.h”

#include<iostream.h>

double Stocks::getTotalValue(int iShares, double dCurPrice){

double dCurrentValue;

iNumShares = iShares;

dCurrentPricePerShare = dCurPrice;

dCurrentValue = iNumShares*dCurrentPricePerShare;

return dCurrentValue;

}

int main(){

Stocks stockPick;

cout<<stockPick.getTotalValue(200, 64.25)<<endl;

return 0;

}

Output of the above program:

12850

Note: The format of member functions included in the implementation section is as follows:

return-type Class-name::functionName(parameter-list)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask