1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN...
-
Upload
adrian-howard -
Category
Documents
-
view
213 -
download
0
Transcript of 1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN...
1
Distribution of MarksFor
Third SemesterInternal Sessional Evaluation External
Eval..
Assignment/Project
Quizzes Class Attendance
Mid-Term Test
Total Sessional
Terminal Exam
Final Evaluation
10 10 5 25 50 50 100
Remember: Examination is a chance not
ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
2
Chapter No Chapter NamePage No
1. FUNCTIONS 162
2. OBJECTS AND CLASSES 215
3. ARRAYS AND STRINGS 263
4. OPERATOR OVERLOADING 319
5. INHERITANCE 371
6. POINTERS 429
Course Outline
BCS iii-rd SemesterObject Oriented Programming-ii
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
3
Course Title: OOP Using C++
COURSE INSTRUCTOR:ILTAF MEHDI
Chap
ter N
o: 0
1
Chap
ter N
o: 0
1
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
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
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
5
Types of Functions• There are basically two types of functions
Library functions/pre-defined functionAs 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( ) etcUser 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++.
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
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.
6By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
7
Syntax for defining function
return type function-name (parameters) {
------------------------------------------------------return (value);
}
void line(void) { int j; for (j=1;j<=50;j++)
cout<<“*”;}
7By 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
8 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
9
void line (void); main() { line( ); cout<<“\n I LOVE AFGHANISTAN”; line( ); }
void line(void) { int j;
for (j=1;j<=50;j++)cout<<“*”;
}
************************ I LOVE AFGHANISTAN
************************
function call
function definition
Function prototype
Output
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<<“*”;
}10By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
11
Calling the FunctionCalling 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 DefinitionThe 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<<“*”;
}
11By 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 callingfunction
line() is calledfunction
Write a program that print a message on the screen using function
13
#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; }
13 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
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; }
14 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 the control back to the calling function
ii) It returns 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.
15 By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
Write a prog that show the use of returning value by a function when it is called
16
#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
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
18
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.
parameter passing by value
Sum(int x, int y){ -------------- -------------- cout<<x+y; }
a=8 b=9
x=8 Y=9
main(){ int a=8,b=9;----------------------------Sum(a, b);---------------------------------------}
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
19
#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(); }
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
20
The C++ language uses call by address to pass addresses from calling function to called function. This means that the supplied variable’s address is passed to the function.
parameter passing by address
Sum(int* x, int* y){ -------------- -------------- cout <<x <<y; }
Ram
a=8 b=9
x=100 Y=102
main(){ int a=8,b=9;----------------------------Sum(&a, &b);---------------------------------------}
100 102
Create a c++ prog in which call a function by passing address, the variables will pass with storage address locations
21
#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(); }
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
Create a prog to exchange the values of two variables by using reference arguments in the function.
22
#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; }
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
23
Pre-processor directives
The pre-processor directive 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)
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
24
#define (macro expansion)
This statement is called macro definition or macro. during preprocessing, the preprocessor replaces every occurrence of Pi in the program with 3.14
Syntax: #define Marco-Name value
In this example Pi is macro, whereas, 3.14 is called their macro expansion.
macro can be used instead of function
#define Pi 3.14main(){--------------------------Pi Pi-------------------------------------------Pi-------------------------------------------------------Pi*Pi----------------Pi----------------------}
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
25
macro instead of function
#define cls clrscr();#define w cout<<“***;#define p cout<<“---;main(){ cls p cout<<“\n MIHE\n”; p w }
main(){ clrscr(); cout<<“-----; cout<<“\n MIHE\n”; cout<<“----; cout<<“***;
}
After compilation
Object Code contains theMacro expansion
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
Write a c++ prog that show the use of a macro in the programming by using #define
26
#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(); }
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL
Write a c++ prog that show a largest value in the two given values by using #define or macro
27
#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(); }
By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL