1 / 27

Remember: Examination is a chance not ability .

Remember: Examination is a chance not ability. BCS iii-rd Semester. Object Oriented Programming-ii. Course Outline. Recommended Text Book: Object Oriented Programming By: Robert Lafore. Course Title : OOP Using C++. Chapter No: 01. Chapter No: 01. COURSE INSTRUCTOR : ILTAF MEHDI.

Download Presentation

Remember: Examination is a chance not ability .

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Remember:Examination is a chancenotability.

  2. BCS iii-rd Semester Object Oriented Programming-ii Course Outline Recommended Text Book: Object Oriented Programming By: Robert Lafore

  3. CourseTitle: OOP Using C++ Chapter No: 01 Chapter No: 01 COURSE INSTRUCTOR: ILTAF MEHDI

  4. What is Function • A function is a self-contained block of statements that perform some kind of pre-defined task. • Every C++ program can be thought of as a collection of these functions. Advantages • Writing functions avoids rewriting the same code over and over. Using functions • It becomes easier to write programs and keep track of what they are doing. • If the operation of a program can be divided into separate activities, and each activity placed in a different function • Separating the code into modular functions also makes the program easier to design and understand • Program are easier to debug • Program are well organized • Programmer can create his/her own customized library of functions • The program size is reduces

  5. Types of Functions • There are basically two types of functions Library functions/pre-defined function As the name suggests, library functions are nothing but commonly required functions grouped together & stored in what is called library. These functions are also called as pre- defined functions Examples are: main( ) , clrscr(), getch( ) etc User defined functions User defined functions are those functions which are written by the programmer for a special purpose or task & can be called in main program The user defined functions are written for formulas for which library functions are not available in the library of C++.

  6. Rules for declaring user-define functions The following are some rules keep in mind while declaring the functions: A. in a program two functions don’t have the same identical names. B. in a single program if two functions have the same identical names then their arguments should be different. C. A function name always starts with the same rules for variables name. D. No special characters or symbols other than underscore can be allowed in a function name.

  7. Syntax for defining function return typefunction-name (parameters) { ------------------ ------------------ ------------------ return (value); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  8. For User defined function To define user-defined function we need • Function declaration / Prototype • Calling the function. • Functions definition It can be explained in the following example By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  9. Function prototype void line (void); main() { line( ); cout<<“\n I LOVE AFGHANISTAN”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } function call ************************ I LOVE AFGHANISTAN ************************ function definition Output

  10. Function Declaration/prototype • Function declaration is similar to the declaration of variables. Functions are always declared before starting main() program. void line (void); • 1st void means that function does not return any value and second void means no arguments .line() is the name of the function and at the end semicolon is compulsory in the declaration. void line (void); main() { line( ); cout<<“\n MIHE”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  11. Calling the Function Calling the function means to call the function in the main program and to activate it. In our program function line( ) is called in the body of main ( ) program. Function Definition The function definition is function itself. It tells the compiler that a function is being defined, The actual steps of the function subprogram is written here. void line (void); main() { line( ); cout<<“\n RANA”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout<<“*”; } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  12. Calling function and called function main() { line( ); cout <<“\n I LOVE AFGHANISTAN”; line( ); } void line(void) { int j; for (j=1;j<=50;j++) cout <<“*”; } main() is calling function line() is called function

  13. Write a program that print a message on the screen using function #include <iostream.h> #include <conio.h> void display(void); void main(void) { Clrscr(); cout<<“my first program in function”<<endl; cout<<“************************************”<<endl; display(); cout <<“************************************”<<endl; cout<<“end of program”<<endl; } void display(void) { cout <<“A function is called”<<endl; } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  14. Write a prog that show the use of parameters in the functions #include <iostream.h> #include <conio.h> void sum(int , int); void main(void) { clrscr(); cout<<“my second program in function”<<endl; cout<<“************************************”<<endl; sum(10,15); cout <<“************************************”<<endl; cout<<“end of program”<<endl; getch(); } void sum(int x, int y) { int s; s= x+ y; cout <<“A function is called”<<endl; cout <<“sum=“<<s<<endl; } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  15. The Return Statement The return statement in C++, function serves two purposes i)On executing the return statement if immediately transfers thecontrol back to the calling function ii)Itreturns the value present in the parenthesis after return, to the calling function The return statement need not be at the end of the function. It can occur anywhere in the function There is no restriction on the number of return statements that may be presented in a single function. A return statement can return only one value a time. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  16. Write a prog that show the use of returning value by a function when it is called #include <iostream.h> #include <conio.h> int grams(int); void main(void) { int kg, gm; clrscr(); cout<<“enter value in kilograms”<<endl; cin >> kg; gm = grams(kg); cout <<“grams = “ <<gm<<endl; cout<<“*******end of program******”<<endl; getch(); } int grams(int val) { cout <<“returning value”<<endl; return val *1000; } By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL

  17. Parameter passing to function • A value passed to a function is called parameter. There are two methods for parameter passing to function • parameter passing by value • parameter passing by address

  18. parameter passing by value The C++ language uses call by value to pass information from calling function to called function. This means that the supplied variable’s value is passed to the function, and not the variable itself. main() { int a=8,b=9; -------------- -------------- Sum(a, b); ------------- ------------- ------------- } Sum(int x, int y) { -------------- -------------- cout<<x+y; } a=8 b=9 x=8 Y=9

  19. Create a c++ prog to pass two values from a function then compare these values and print result while no return value from the function #include <iostream.h> void comp(int x, int y) #include <conio.h> { void comp(int, int); if(x>y) void main(void) cout <<“first value is greater { than second value”<<endl; int n1, n2; else clrscr(); cout <<“the second value is cout<<“enter first value ”<<endl; greater than first value”<<endl; cin >> n1; } cout << “enter second value “<<endl; cin >> n2; comp(n1, n2); getch(); }

  20. parameter passing by address The C++ language uses call by address to pass addressesfrom calling function to called function. This means that the supplied variable’saddressis passed to the function. main() { int a=8,b=9; -------------- -------------- Sum(&a, &b); ------------- ------------- ------------- } Sum(int* x, int* y) { -------------- -------------- cout <<x <<y; } a=8 b=9 102 100 x=100 Y=102 Ram

  21. Create a c++ prog in which call a function by passing address, the variables will pass with storage address locations #include <iostream.h> void address(int* x, int* y) #include <conio.h> { void address(int*, int*); void main(void) cout <<“first value address { is<< x<<endl; int v1, v2; clrscr(); cout <<“the second value cout<<“enter first value ”<<endl; address is <<y<<endl; cin >> v1; } cout << “enter second value “<<endl; cin >> v2; cout << “first value is= “<<v1 <<“\n second value =“<<v2; address(&v1, &v2); getch(); }

  22. Create a prog to exchange the values of two variables by using reference arguments in the function. #include <iostream.h> void exch(int& x, int& y) #include <conio.h> { void exch(int &, int &) int t; void main(void) t = x; { x = y; int a, b; y = t; clrscr(); } cout<<“enter first value ”<<endl; cin >> a; cout << “enter second value “<<endl; cin >> b; cout << “first value is= “<<a <<“\n second value =“<<b; exch(a, b); cout <<first value is =“<<a<<“\n second value is =“<<b; }

  23. Pre-processor directives Thepre-processordirective is the instruction begin with#symbol most often placed at the beginning of the program and process before even compilation process begin, we would learn the following pre-processor directives here #define(macro expansion)

  24. #define (macro expansion) #define Pi 3.14 main() { -------------- ------------Pi Pi----------- -------------- -------------- ----Pi-------- -------------- -------------- ------------ -------Pi*Pi ------------- ---Pi--------- ------------- } This statement is called macro definition or macro. during preprocessing, the preprocessor replaces every occurrence of Piin the program with3.14 Syntax: #define Marco-Name value In this example Piismacro, whereas, 3.14is called their macro expansion. macrocan be used instead of function

  25. macro instead of function #define cls clrscr(); #define w cout<<“***; #define p cout<<“---; main() { cls p cout<<“\n MIHE\n”; p w } Object Code contains the Macro expansion After compilation main() { clrscr(); cout<<“-----; cout<<“\n MIHE\n”; cout<<“----; cout<<“***; }

  26. Write a c++ prog that show the use of a macro in the programming by using #define #include <iostream.h> #include <conio.h> #define R x+((y*z) /2) void main(void) { int x, y, z, result; x=10; y= 5; z= 2; clrscr(); result = R; cout <<“result = “<<result <<endl; result = R + 10; cout<<“increment in result = ”<< result<<endl; result = R – 15; cout<<“decrement in the result = “<<result<<endl; cout<<“end of program”<<endl; getch(); }

  27. Write a c++ prog that show a largest value in the two given values by using #define or macro #include <iostream.h> #include <conio.h> #define LARGE (x , y) ((x>y)? x: y) void main(void) { int x, y; clrscr(); cout <<“enter value for x “<<endl; cin >> x; cout<<“enter value for y ”<<endl; cin >> y; cout<<“the Largest value = “<<LARGE (x , y)<<endl; cout<<“end of program”<<endl; getch(); }

More Related