1 / 44

CS-241 OBJECT-ORIENTED PROGRAMMING

CS-241 OBJECT-ORIENTED PROGRAMMING. Engr. Ruhma Sumbal. Course Description. Course Code: CS-241 Credits: 4 (3,1) Prerequisites: CS-141. Grading Policy Assignments 5% Quizzes 10% Project 10% Sessional 1 10% Sessional 2 15% Final 50%. Course Outline.

anne-tate
Download Presentation

CS-241 OBJECT-ORIENTED PROGRAMMING

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. CS-241 OBJECT-ORIENTED PROGRAMMING Engr. Ruhma Sumbal

  2. Course Description Course Code: CS-241 Credits: 4 (3,1) Prerequisites: CS-141 Grading Policy Assignments 5% Quizzes 10% Project 10% Sessional 1 10% Sessional 2 15% Final 50%

  3. Course Outline • Text Books: • Object Oriented Programming Using C++, 4th edition, Robert Lafore, SamsPublishing • Java 2: The Complete Reference, Herbert Schildt • Reference Book: • “Object Oriented Programming in Java TM ” by RichradL.Halterman Dr. Java

  4. Comparison • Structured Programming • Logical Structure • Flow of execution depend on the structure of the program • Less data security • Less code reusability • Less abstraction • Top-Down approach • OOP • Collection of objects which communicate with each other • More data security • More code reusability • More abstraction • Bottom-Up approach

  5. Getting Started #include <iostream.h> int main() { std::cout<< "Hello,World!\n"; return 0; } • Preprocessor directive # sign is used for that • Standard C++ Library • C++ is case-sensitive • Header files • Note that every statement ends with a semicolon

  6. Compilation Process • Object files are intermediate files that represent an incomplete copy of the program • Each source file only expresses a piece of the program • The object file has some markers indicating which missing pieces it depends on. • The linker takes those object files and the compiled libraries of predefined code that they rely on, fills in all the gaps, and spits out the final program

  7. Tokens Tokens are the minimal chunk of program that have meaning to the compiler – the smallest meaningful symbols in the language

  8. Namespaces #include <iostream> int main() { std::cout<< "Hello,World!\n"; return 0; } #include <iostream> using namespace std; int main() { cout<< "Hello,World!\n"; return 0; } Namespaces make it possible for a program to use different objects with the same name, just as different people can have the same name. 9

  9. Namespaces/ scope resolution operator (::) #include <iostream> using namespace std; namespacefirst_space { voidfunc() { cout << "Inside first_space" << endl; } } namespacesecond_space { voidfunc() { cout << "Inside second_space" << endl; } } int main () { first_space::func(); second_space::func(); return 0; } 10

  10. Variable

  11. Variables int main() { const intLENGTH = 10; const intWIDTH = 5; const char NEWLINE = '\n'; int area; area = LENGTH * WIDTH; cout << area; cout << NEWLINE; return 0; } inti, j, k; char c, ch; float f, salary; double d; int main () { // Variable declaration: int a, b; float f; // initialization a = 10; b = 20; f = 0.234 return 0; } int d = 3, f = 5; long z = 22; double pi = 3.14159; char x = 'x';

  12. Lvalues and Rvalues There are two kinds of expressions in C++: lvalue : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment. rvalue : An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side. Following is a valid statement: int g = 20; But following is not a valid statement and would generate compile-time error: 10 = 20;

  13. Lvalues and Rvalues int main() { const intLENGTH = 10; const intWIDTH = 5; const char NEWLINE = '\n'; int area; LENGTH = 20 area = LENGTH * WIDTH; cout << area; cout << NEWLINE; return 0; } Error : l-value specifies const object

  14. Variables #include <iostream> using namespace std; int main() { int m, n; floatfm,fn; m = 44; fm = 44; cout << "m = " << m; n = m + 33; cout << " and n = " << n << endl; cout << "fm = " << fm; fn = fm/10; cout << " and fn = " << fn ; return 0; } #include <iostream> using namespace std; int main() { int m = 12, n = 5; floatfm,fn; cout << "m%n = " << m%n; return 0; }

  15. Input Operator #include <iostream> using namespace std; int main() { intm,n; cout << "Enter two integers: "; cin >> m >> n; cout << "m = " << m << ", n = " << n << endl; doublex,y,z; cout << "Enter three decimal numbers: "; cin >> x >> y >> z; cout << "x = " << x << ", y = " << y << ", z = " << z << endl; char c1,c2,c3 ; cout << "Enter four characters: "; cin >> c1 >> c2 >> c3; cout << "c1 = " << c1 << ",c 2 = " << c2 << ", c3 = " << c3 << endl; return 0; }

  16. Operators

  17. Some operations #include <iostream> using namespace std; int main() { // prints the results of arithmetic operators intm,n; cout << "Enter two integers: "; cin >> m >> n; cout << "The integers are " << m << " and " << n << endl; cout << "Their sum is " << (m + n) << endl; cout << "Their difference is " << (m - n) << endl; cout << "Their product is " << (m * n) << endl; cout << "Their quotient is " << (m / n) << endl; cout << "Their remainder is " << (m % n) << endl; }

  18. Some operations Enter two integers: 60 7 The integers are 60 and 7 Their sum is 67 Their difference is 53 Their product is 420 Their quotient is 8 Their remainder is 4 #include <iostream> using namespace std; int main() { intm,n; cout << "Enter two integers: "; cin >> m >> n; cout << "The integers are " << m << " and " << n << endl; cout << "Their sum is " << (m + n) << endl; cout << "Their difference is " << (m - n) << endl; cout << "Their product is " << (m * n) << endl; cout << "Their quotient is " << (m / n) << endl; cout << "Their remainder is " << (m % n) << endl; }

  19. Some operations #include <iostream> using namespace std; int main() { inti = 0; cout << "i = " <<i <<endl; cout << "i++ = " <<i++ <<endl; cout << "i = " <<i <<endl; cout << "++i = " <<++i <<endl; cout << "i = " <<i <<endl<<endl; int j; j = 0, i = 0; i = i + j++; cout << "i = i + j++ = " <<i <<endl<<endl; j = 0, i = 0; i = i + ++j; cout << "i = i + ++j = " <<i <<endl; return 0; }

  20. Some operations #include <iostream> using namespace std; int main() { inti = 0; cout << "i = " <<i <<endl; cout << "i++ = " <<i++ <<endl; cout << "i = " <<i <<endl; cout << "++i = " <<++i <<endl; cout << "i = " <<i <<endl<<endl; int j; j = 0, i = 0; i = i + j++; cout << "i = i + j++ = " <<i <<endl<<endl; j = 0, i = 0; i = i + ++j; cout << "i = i + ++j = " <<i <<endl; return 0; } i = 0 i++ = 0 i = 1 ++i = 2 i = 2 i = i + j++ = 0 i = i + ++j = 1

  21. Scope of Variables #include <iostream> using namespace std; int x = 1; // this x is global int main() { int x = 2; { int x = 3; cout << "In block inside main(): x = " << x << endl; } cout << "In main() x = " << x << endl; cout << "In main() ::x = " << ::x << endl; return 0; }

  22. Scope of Variables #include <iostream> using namespace std; int x = 1; // this x is global int main() { int x = 2; { int x = 3; cout << "In block inside main(): x = " << x << endl; } cout << "In main() x = " << x << endl; cout << "In main() ::x = " << ::x << endl; return 0; } In block inside main(): x = 3 In main() x = 2 In main() ::x = 1

  23. #include <iostream> #include <conio.h> using namespace std; void main() { enum day {Monday,Tuesday,Wednesday,Thrusday,Friday,Saturday,Sunday}; int d; cout<<"\n0)Monday"; cout<<"\n1)Tuesday"; cout<<"\n2)Wednesday"; cout<<"\n3)Thrusday"; cout<<"\n4)Friday"; cout<<"\n5)Saturday"; cout<<"\n6)Sunday\n"; cin>>d; if(d==Saturday || d==Sunday) { cout<<"\n\nWEEKENDS !!!!"; } else { cout<<"\n\nWEEKDAYS -----"; } getch(); } Enum By default, the value of the first name is 0, the second name has the value 1. enum color { red, green=5, blue }; blue will have a value of 6 because each name will be one greater than the one that precedes it.

  24. Food for Thoughts • Get the values of a,b,c and implement the quadratic formula. Program should terminate if a = 0. • Input Fahrenheit output celsius

  25. Relational and Logical Operators i,j and k are integer variables whose values are 1, 2 and 3, Expression Value i < j ? (1 + j) >= k ? (j + k) > (i + 5) ? k == 3 ? j == 2 ? Logical operators: i = 6, c = ‘w’, f = 10 Exression Value (i >= 6) && (c == ' w ' ) ? (i >= 6) 1 1 (c == 119) ? (f < 11) && (i > 100) ? (c != ' p ' ) 1 1 ((i + f ) <= 10) ? !(i <=6) ?

  26. inti= 8, j = 5; float x = 0.005, y = -0.01; char c = ' c ' , d = 'd' ; Determine the value of each of the following expressions (1) ( 3 * i - 2 * j ) % ( 2 * d - c ) (2) 2 * ( ( i / 5) + (4 * ( j - 3)) % ( i + j - 2)) (3) ( i - 3 * j ) % ( c + 2 * d ) / ( x - y ) (4) -(i + j ) (5) x >= 0 (6) j != 6 (7) 5 * (i + j ) > ' c ' (8) ( 2 * x + y) == 0 (9) 2 * x + ( y = = 0) (10) 2 * x + y == 0 (11) ( i <= j ) (12) ( c == 99) (13) (i > 0) && ( j < 5) (14) ( i > 0) I ! ( j < 5) (15) (x > y) && (i > 0) ! I ( j < 5) Practice

  27. Decisions

  28. Flow Chart

  29. Decisions #include <iostream> using namespace std; int main () { int a = 10; if( a < 20 ) { cout << "a is less than 20”; } cout << "value of a is : " << a return 0; }

  30. Decisions int main () { int a = 100; if( a == 10 ) { cout << "Value of a is 10"; } else if( a == 20 ) { cout << "Value of a is 20"; } else { cout << "Value of a is not matching"; } cout << "Exact value of a is : " << a; return 0; }

  31. void main () { char grade = 'D'; switch(grade) { case 'A' : cout << "Excellent!" << endl; break; case 'B' : case 'C' : cout << "Well done" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; } cout << "Your grade is " << grade << endl; } Decisions

  32. Decisions int main () { int a = 100; int b = 200; if( a == 100 ) { if( b == 200 ) { cout << "Value of a is 100 and b is 200" << endl; } } cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; return 0; }

  33. int main () { int a = 100, b = 201; switch(a) { case 100: cout << "This is part of outer switch" << endl; switch(b) { case 200: cout << "This is part of inner switch" << endl; break; default: cout << "Inner default"; << endl; } break; default: cout << "Outer default”<< endl; } cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; return 0; } Decisions

  34. Conditional Operator Expression 1 ? Expression 2 : Expression 3 If Expression 1 is true then result is Expression 2 otherwise Expression 3 (i < 0) ? 0 : 100 minmum = (f < g) ? f : g k = ( j ==5) ? 1 : j k = ( j > 5) ? i : j z = (x >= 0) ? x : 0 z = (y >= 0) ? y : 0

  35. For Loop #include <iostream> using namespace std; int main () { for( int a = 10; a < 20; a = a + 1 ) { cout << "value of a: " << a << endl; } return 0; }

  36. While Loop #include <iostream> using namespace std; int main () { int a = 10; while( a < 20 ) { cout << "value of a: " << a << endl; a++; } return 0; }

  37. break int main () { int a = 10; do { cout << "value of a: " << a << endl; a = a + 1; if( a > 15) { break; } }while( a < 20 ); return 0; }

  38. break int main () { int a = 10; do { cout << "value of a: " << a << endl; a = a + 1; if( a > 15) { break; } }while( a < 20 ); return 0; } value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15

  39. Continue int main () { int a = 10; do { if( a == 15) { // skip the iteration. a = a + 1; continue; } cout << "value of a: " << a << endl; a = a + 1; }while( a < 18 ); return 0; }

  40. Continue int main () { int a = 10; do { if( a == 15) { // skip the iteration. a = a + 1; continue; } cout << "value of a: " << a << endl; a = a + 1; }while( a < 18 ); return 0; } value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17

  41. Do-While #include <iostream> using namespace std; int main () { int a = 10; do { cout << "value of a: " << a << endl; a = a + 1; }while( a < 20 ); return 0; }

  42. Nested Loop int main () { inti, j; for(i=2; i<100; i++) { for(j=2; j <= (i/j); j++) if(!(i%j)) break; if(j > (i/j)) cout << i << " is prime\n"; } return 0; }

  43. Nested Loop int main () { inti, j; for(i=2; i<100; i++) { for(j=2; j <= (i/j); j++) if(!(i%j)) { cout <<"Checking" << endl; break; //what if no {} in this program } if(j > (i/j)) cout << i << " is prime\n"; } return 0; }

  44. Functions return_typefunction_name( parameter list ) { body of the function } A C++ function definition consists of a function header and a function body. Here are all the parts of a function: Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body: The function body contains a collection of statements that define what the function does.

More Related