Object-Oriented Programming Lecture 1 PDF

Summary

This lecture introduces object-oriented programming (OOP) concepts and structures in C++. It covers topics like control statements, decision-making, selection, looping, and jump statements with examples.

Full Transcript

Object Oriented Programming Introduction Assoc. Prof. Dr. Tamer Omar Classifications Control statements 1) Decision making statements: 1.If Statement 2. If Else Statement 3.Else if ladder 4.Nested If else 2) Selection Statements: 1.Switch Case 3) Looping...

Object Oriented Programming Introduction Assoc. Prof. Dr. Tamer Omar Classifications Control statements 1) Decision making statements: 1.If Statement 2. If Else Statement 3.Else if ladder 4.Nested If else 2) Selection Statements: 1.Switch Case 3) Looping Statements: 1.While 2.Do While 3.For Loop 4) Jump Statements: 1.break 2.Continue If Statement  if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. Syntax : if(condition) { Statements 1; } If Else Statement:  It include two parts:  True part  False part Syntax: if(condition) { Statements 1; } Else { Statements 2; } Else if ladder:  The else if ladder is used to test set of condition in a sequence.  It is used when there are multiple condition to be check. Syntax: if(condition) { Statements 1; } else if (condition 2) { Statements 2; } else if (condition 3) { Statements 3; } else { Statements n; } Switch Case:  Switch case statement is used when we have multiple condition and we need to perform different action based on the condition. Syntax: switch (expression) ​ { case constant1: // statements break; case constant2: // statements break;... default: // default statements } Looping Statements  While Statement: A while loop is a simple loop that will run the same code over and over as long as a given conditional is true. The condition is checked at the beginning of each run through the loop ( including the first one ). If the conditional is false for the beginning, the while loop will be skipped all together. Syntax: While(condition) { Statement 1; }  Do While: A do-while loop acts just like a while loop, except the condition is checked at the end of each pass through the loop body. This means a do-while loop will execute at least once syntax do { // loop body } while ( condition ); Looping Statements  For Loop: In for loop the initialization, condition ,or increment or decrement of loop variable is implemented in a single statement. Syntax: for( initialization ; conditional ; iteration ) { // loop body } Jump Statements:  break: The break statement can also be used to jump out of a loop  continue: It is used to skipping for a particular part of loop Abstract Data Types  A programmer-created data type that specifies legal values that can be stored operations that can be done on the values  The user of an abstract data type does not need to know any implementation details (e.g., how the data is stored or how the operations on it are carried out)  Abstraction: a definition that captures general characteristics without details An abstract triangle is a 3-sided polygon. A specific triangle may be scalene, isosceles, or equilateral  Data Type: defines the kind of values that can be stored and the operations that can be performed on it Structures  Structure: C++ construct that allows multiple variables to be grouped together  Structure Declaration Format:  struct names commonly begin with an uppercase letter. The structure name is also called the tag  Multiple fields of same type can be in a comma-separated list string name,address; struct structure name { type1 field1; type2 field2; … typen fieldn; }; Structures  struct declaration does not allocate memory or create variables  To define variables, use structure tag as type name Student s1;  Use the dot (.) operator to refer to members of struct variables getline(cin, s1.name); cin >> s1.studentID; s1 s1.gpa = 3.75; studentID  To display the struct Members name cout > s.gpa; return s; } Example //Program to demonstrate the CDAccount structure type. #include using namespace std; //Structure for a bank certificate of deposit: struct CDAccount { double balance; double interest_rate; int term; //months until maturity }; void get_data(CDAccount& the_account) { cout > the_account.balance; cout > the_account.interest_rate; cout the_account.term; } Example (Cont.) //Postcondition: the_account.balance and the_account.interest_rate //have been given values that the user entered at the keyboard. int main( ) { CDAccount account; get_data(account); double rate_fraction, interest; rate_fraction = account.interest_rate / 100.0; interest = account.balance * rate_fraction * (account.term / 12.0); account.balance = account.balance + interest; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout

Use Quizgecko on...
Browser
Browser