Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

The Approaches

 

Approach 1

All code in main

Approach 2

Separate functions - function prototypes

Approach 3

Classes

Modularization

One function to do one thing

Reusability of Code

Avoid duplication of code

 

Approach 1

Everything in the main function - How we began

 

#include <iostream.h>
int main ()
{
    int numberIn;
    int doubleNumberIn;
    cout <<"Please enter an integer"<<endl;
    cin >>numberIn;
    doubleNumberIn = 2*numberIn;
    cout<<"The number entered times 2 is "<<doubleNumberIn<<endl;
    return 0;
}

 

 

Approach 2

Separate functions outside the main function

 

#include <iostream.h>       //preprocessor directive
int getInput();                        //function prototype
int doubleInput(int);            //function prototype
void printResult(int);          //function prototype
 

int main ()                                //main function
{
    int numberIn = getInput();
    int twiceNumber = doubleInput(numberIn);
    printResult(twiceNumber);
    return 0;
}


int getInput()                                        //getInput function
{
    int input;
    cout <<"Please enter an integer"<<endl;
    cin >> input;
    return input;
}
int doubleInput(int x)                        //doubleInput function
{
    int doubleX = 2*x;
    return doubleX;
}


void printResult(int result)            //printResult function
{
    cout <<"The number entered times 2 is "<<result<<endl;
}

 

Notes:

  Function prototypes

     Required for all functions placed below the main function

     Must contain the return type (void, int, double), the name of the function, and the data type of

        the parameters passed (possibly multiple).

     Including the name of the variable being passed in the function prototype is optional

  Main function

     Short as possible

     Work is performed in the functions placed below the main (called modularization)

     A function is "called" from the main by using the name of the function, along with the

        variables being passed as a parameter.

     Be careful about duplicate definitions in the main (define data type of variables being passed

        one time only)

     Return values (from a function called) are assigned to the name - then should be assigned to

        a variable

  Functions below the main function

     Must be identical to the function prototype and must contain a name for variable passed, if

        any

     Name of variable in function does not have to match name used in calling the function

     These functions can be called many times throughout a program - this is very advantageous

 

Approach 3

Using classes - encapsulation of functions and data - create object in main to access

 

header or interface file: .h extension

    contains a public and private section

    variables placed in private section

    constructor functions and other member functions placed in public section

 

    Preprocessor directives and function prototypes from Approach 3 above would be placed in

    the interface file

 

definition file: .cpp extension

    contains body of above functions

 

    The functions below the main from Approach 3 above would be placed in the definition file

 

driver file: .cpp extension

    creates an object of the class and access member functions

 

    Assuming the interface file was named dataExample, the main function would contain code

    similar to the following

   

    dataExample myExample;                                        //create an object of the class

    int numberIn = myExample.getInput();    //access method in class using the object
    int twiceNumber = myExampledoubleInput(numberIn);    //access method in class using object
    myExample.printResult(twiceNumber);    //access method in class using the object
    return 0;

 

 

Modularization

 

#include "stdafx.h"

#include <iostream>

using namespace std;

 

void greeting();

double numberHours();

double payPerHour();

double weeklyPay(double, double);

void reportResults(double);

 

int main()

{

    greeting();

    double numberHoursWorked = numberHours();

    double payRate = payPerHour();

    double employeePay = weeklyPay(numberHoursWorked, payRate);

    reportResults(employeePay);

    return 0;

}

void greeting()

{

    cout <<"This program prompts for hours worked and pay per hour for employees"<<endl;

    cout <<"It then calculates and prints the weekly pay for the employee"<<endl;

    cout <<endl;

}

double numberHours()

{

    double hoursWorked;

    cout <<"How many hours did you work during the week?"<<endl;

    cin >> hoursWorked;

    return hoursWorked;

}

double payPerHour()

{

    double yourPayPerHour;

    cout <<"What is your pay per hour?"<<endl;

    cin >>yourPayPerHour;

    return yourPayPerHour;

}

double weeklyPay(double hours, double rate)

{

    return hours*rate;

}

void reportResults(double pay)

{

    cout <<"Your pay for the week is: "<<"$"<<pay<<endl;

}

 

 

 

Reusability of Code

 

This involves using the same function repeatedly. For example, if you want to print a data type with an

explanation, you do need separate functions. Call the same one and pass it the appropriate parameter(s).

 

If you are calculating the same thing (pay, for example) using the same math then call the same function

for different employees.


 

#include "stdafx.h"

#include <iostream>

using namespace std;

 

int getNumber();

double calculatePay(int number);

void printResult(double pay1, double pay2, double pay3);

 

int main()

{

    int employee1Hrs = getNumber();

    int employee2Hrs = getNumber();

    int employee3Hrs = getNumber();

 

    double payEmployee1 = calculatePay(employee1Hrs);

    double payEmployee2 = calculatePay(employee2Hrs);

    double payEmployee3 = calculatePay(employee3Hrs);

 

    printResult (payEmployee1, payEmployee2, payEmployee3);

    return 0;

}

int getNumber()

{

    int hours;

    cout <<"How many hours did you work?"<<endl;

    cin >> hours;

    return hours;

}

 

double calculatePay(int number)

{

    double pay = number*126.53;

    return pay;

}

 

void printResult(double pay1, double pay2, double pay3)

{

    double totalPay = pay1 + pay2 + pay3;

    cout <<"The total money owed is: "<<totalPay<<endl;

}