Engineering Forum

India Education

Engineering Colleges Forum

Functions and Procedural Abstraction part 2

This is a discussion on Functions and Procedural Abstraction part 2 within the Computer engineering forums, part of the ENGINEERING WORLD category; 3.4 Polymorphism and Overloading C++ allows polymorphism, i.e. it allows more than one function to have the same name, provided ...


Go Back   Engineering Forum > ENGINEERING WORLD > Computer engineering

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

   

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 08-28-2008, 07:30 PM
aayush_005's Avatar
Administrator
 
Join Date: Aug 2008
Posts: 225
Default Functions and Procedural Abstraction part 2

3.4 Polymorphism and Overloading

C++ allows polymorphism, i.e. it allows more than one function to have the same name, provided all functions are either distinguishable by the typing or the number of their parameters. Using a function name more than once is sometimes referred to as overloading the function name. Here's an example:
#include<iostream>
using namespace std;

int average(int first_number, int second_number, int third_number);

int average(int first_number, int second_number);

/* MAIN PROGRAM: */
int main()
{
int number_A = 5, number_B = 3, number_C = 10;

cout << "The integer average of " << number_A << " and ";
cout << number_B << " is ";
cout << average(number_A, number_B) << ".\n\n";

cout << "The integer average of " << number_A << ", ";
cout << number_B << " and " << number_C << " is ";
cout << average(number_A, number_B, number_C) << ".\n";

return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 3 INTEGERS: */
int average(int first_number, int second_number, int third_number)
{
return ((first_number + second_number + third_number) / 3);
}
/* END OF FUNCTION */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 2 INTEGERS: */
int average(int first_number, int second_number)
{
return ((first_number + second_number) / 2);
}
/* END OF FUNCTION */
Program 3.4.1

This program produces the output:
The integer average of 5 and 3 is 4.

The integer average of 5, 3 and 10 is 6.
(BACK TO COURSE CONTENTS)



3.5 Procedural Abstraction and Good Programming Style

One of the main purposes of using functions is to aid in the top down design of programs. During the design stage, as a problem is subdivided into tasks (and then into sub-tasks, sub-sub-tasks, etc.), the problem solver (programmer) should have to consider only what a function is to do and not be concerned about the details of the function. The function name and comments at the beginning of the function should be sufficient to inform the user as to what the function does. (Indeed, during the early stages of program development, experienced programmers often use simple "dummy" functions or stubs, which simply return an arbitrary value of the correct type, to test out the control flow of the main or higher level program component.)

Developing functions in this manner is referred to as functional or procedural abstraction. This process is aided by the use of value parameters and local variables declared within the body of a function. Functions written in this manner can be regarded as "black boxes". As users of the function, we neither know nor care why they work.
(BACK TO COURSE CONTENTS)



3.6 Splitting Programs into Different Files

As we have seen, C++ makes heavy use of predefined standard libraries of functions, such as "sqrt(...)". In fact, the C++ code for "sqrt(...)", as for most functions, is typically split into two files:
The header file "cmath" contains the function declarations for "sqrt(...)" (and for many other mathematical functions). This is why in the example programs which call "sqrt(...)" we are able to write "#include<cmath>", instead of having to declare the function explicitly.

The implementation file "math.cpp" contains the actual function definitions for "sqrt(...)" and other mathematical functions. (In practice, many C++ systems have one or a few big file(s) containing all the standard function definitions, perhaps called "ANSI.cpp" or similar.)


It is easy to extend this library structure to include files for user-defined functions, such as "area(...)", "factorial(...)" and "average(...)". As an example, Program 3.6.1 below is the same as Program 3.4.1, but split into a main program file, a header file for the two average functions, and a corresponding implementation file.

The code in the main program file is as follows:
#include<iostream>
#include"averages.h"

using namespace std;

int main()
{
int number_A = 5, number_B = 3, number_C = 10;

cout << "The integer average of " << number_A << " and ";
cout << number_B << " is ";
cout << average(number_A, number_B) << ".\n\n";

cout << "The integer average of " << number_A << ", ";
cout << number_B << " and " << number_C << " is ";
cout << average(number_A, number_B, number_C) << ".\n";

return 0;
}
Program 3.6.1

Notice that whereas "include" statements for standard libraries such as "iostream" delimit the file name with angle ("<>") brackets, the usual convention is to delimit user-defined library file names with double quotation marks - hence the line " #include"averages.h" " in the listing above.

The code in the header file "averages.h" is listed below. Notice the use of the file identifier "AVERAGES_H", and the reserved words "ifndef" ("if not defined"), "define", and "endif". "AVERAGES_H" is a (global) symbolic name for the file. The first two lines and last line of code ensure that the compiler (in fact, the preprocessor) only works through the code in between once, even if the line "#include"averages.h"" is included in more than one other file.

Constant and type definitions are also often included in header files. You will learn more about this in the object-oriented part of the course.
#ifndef AVERAGES_H
#define AVERAGES_H

/* (constant and type definitions could go here) */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 3 INTEGERS: */
int average(int first_number, int second_number, int third_number);

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 2 INTEGERS: */
int average(int first_number, int second_number);

#endif
averages.h

Finally, the code in the implementation file "averages.cpp" is as follows:
#include<iostream>
#include"averages.h"

using namespace std;

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 3 INTEGERS: */
int average(int first_number, int second_number, int third_number)
{
return ((first_number + second_number + third_number) / 3);
}
/* END OF FUNCTION */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 2 INTEGERS: */
int average(int first_number, int second_number)
{
return ((first_number + second_number) / 2);
}
/* END OF FUNCTION */
averages.cpp

Note the modularity of this approach. We could change the details of the code in "averages.cpp" without making any changes to the code in "averages.h" or in the main program file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-11-2011, 06:56 PM
Junior Member
 
Join Date: Jan 2011
Posts: 2
Default Admin, where are you?! WTF?! m?..

wow, seriously?!

ANY QUESTIONS???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-12-2011, 04:23 PM
Junior Member
 
Join Date: Jan 2011
Posts: 2
Default Admin, where are you?! WTF?! m?..

Quote:
wow, seriously?! ANY QUESTIONS???
dont kidding me, folks!

Delete this
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-24-2011, 09:41 PM
Senior Member
 
Join Date: Feb 2011
Posts: 957
Default bags minnetonka minnesotahow to make a gift bag

5 Made with premium leather and Adidas famous signature “3 stripes” in a nice grey colorway. The back side of the shoe has the “Pistol Pete”
Nike Shoes|Air Max|Nike Jordan|Cheap Nike Shoes.the latest Jordan shoes, Air Jordan Retro High,Air Jordan Low,Men's Basketball Shoes cheap air jordan
15 Jul Added to queue Sneak Peek: New Balance 769by Added to queue New Running Shoe Previewsby RunnersWorldTV2023 views · 3:50. Add to
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-25-2011, 06:48 AM
Senior Member
 
Join Date: Feb 2011
Posts: 957
Default horse in bell boots videoenerpac hushh pump

3Adidas Boxing Lo Pro Shoes. Size: 48. Color: Black, White, Olive, Brown. Price: $ 154. Sale: $ 38. Quantity: 8. Receipt: 26 day ago
613 rina rich peanut handbag red. โพสต์ใหม่ โดย testidoors เมื่* *ังคาร 23 พ.ย. 9:06 pm. รูป*าพ · acqua collection handbag b and b handbags beaded
Gallaz, skateboarding shoes made for girls and women skateboarders. Adio has a ridiculous team. Tony Hawk, Bam Margera, Shaun White, Jeremy Wray ... the
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-26-2011, 09:17 AM
Member
 
Join Date: Mar 2011
Location: Australia
Posts: 39
Send a message via ICQ to kencivesoni
Default repair tear leather chanel handbaggucci 85th anniversary handbags

When buying vegan shoes fit the in front while, the a- site would be to give access to to the collection to grasp the shoe settling on and judge the shoes onIt may be as a replacement for some it takes a little longer to get it, but in the capital temporarily less overpriced brands can contain the anyway aftermath on an togs, without the mortifying gamble of being called exposed as a mountebank online wholesale yoga bags Canada If you are wearing master-work clothing, a suggest of defiance eliminates stuffiness and gives a more up to the minute, individual judgeIn the trend market, it is importance attention of optical products such as BurberryThis mineral breaks with a smooth side order cheap bogy's paris handbags Denmark According to Elle magazine's online "Top Ten Fashion Must-Haves through despite 2008," handbags of all shapes and sizes are being splashed upon in distinguished technicolor! The brightly colored rainbow of multi-colored hues past high-end designers such as Balenciaga, Fendi and Louis Vuitton will brighten up a summer's lifetimeIf you're planning to start a home ground enterprise, investing in wholesale shoes is a good choice. buy cheap designer handbag on wheels Germany * London Fog: A goodly industrialist of coats and other clothesAll these branded bags are valuable and all right merit the priceThe latest addtiion to the Chloé family represents a classic with a hip plait, slightly reminiscent in manner of the Hermès Birkin buy online veton handbags Singapore Friends, blood and co-workers can thanks a single time finally you present them your high-quality wholesale purses and purses as gifts - they can not in any way advised of how full of it value! Whether you're bothersome pro leather, suede, compartments, zippers, laces, large or laconic straps, or an unending modulation of colours, you'll windfall what you have a fancy through wholesale pocketbook distributorsxchngEmbrace a tittle of 1950s continental elegant this winter and inaugurate in a beret, but outset learn a atom prevalent this ideal headwear order online wholesale yoga bags Utited States Although Judith mostly created fantastic leather bags in her prematurely years, she is in the present circumstances superb known as far as something her well-executed clutches and minaudieres, "a missus's small handbag, in many cases decorated with costly facts, in use accustomed to for formal sportWholesale handbags participate in put in an appearance a want mode! cheap hala handbags in us buy cheap bogy's paris handbags Singapore Some designers secure also infatuated the hazard of adding embellishments, such as buckles, bowties and buttonsSpecial your individuality before donning on a team up of embellished skyscraper heels which settle upon transfigure you into a unmasculine, classy spouse in secondsCarry-on luggage Mooch or Shoplift offers rentals such as Burberry, Chanel and Chloe, reasonable to name a scarcely any, while also oblation peerless trade-mark accessories like sunglasses, jewelry and watches that can be rentedYou get better gas mileage when you go slower This bag has a Get to work strap that has a drop thoroughly of 10" What for semi-weekly updates to the shopping list as members last to vote on the side of the latest IT bagsElect footwear in this seasons refulgent in the buff tones for extra leg-lengtheningThe roomyfit website currently offers a break down of 14 distinct types of inappropriate accoutrements ladies shoes, sandals, slippers and boots at a diminish of up to 45%
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-03-2011, 11:42 PM
Junior Member
 
Join Date: Nov 2011
Posts: 3
Default Whats up, few questions for anyone

penis advantage review vt3bxdaw8dqubnb7h436
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-03-2011, 11:42 PM
Junior Member
 
Join Date: Nov 2011
Posts: 3
Default Whats up, few questions for anyone

best free sports betting system p8ss0kgzyu5fxsbscuuz
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-18-2012, 10:41 AM
Junior Member
 
Join Date: Jan 2012
Location: Russia
Posts: 3
Default I want download for free X Rumer 7.0.10 Elite?

I want free X Rumer 7.0.10 ELITE??
Send me URL please!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-07-2012, 06:32 AM
Junior Member
 
Join Date: Jan 2012
Posts: 3
Default Hello

Hello, i read your site, this a best site from me, thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT. The time now is 12:27 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.