# Lecture 6

Lecture 6

Lecture 6

- No tags were found...

### Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introduction to C++Programming – 2Adhi Harmoko S, M.Komp

ArithmeticArithmetic calculationsUse * for multiplication and / for divisionInteger division truncates remainder7 / 5 evaluates to 1Modulus operator returns the remainder7 % 5 evaluates to 2Operator precedenceSome arithmetic operators act before others (i.e., multiplicationbefore addition)Be sure to use parenthesis when neededExample: Find the average of three variables a, b and cDo not use: a + b + c / 3Use: (a + b + c ) / 3

ArithmeticRules of operator precedence:Operator(s)Operation(s)Order of evaluation (precedence)() Parentheses Evaluated first. If the parentheses arenested, the expression in the innermostpair is evaluated first. If there are severalpairs of parentheses “on the same level”(i.e., not nested), they are evaluated leftto right.*, /, or % Multiplication DivisionModulus+ or - AdditionSubtractionEvaluated second. If there are several,they reevaluated left to right.Evaluated last. If there are several, theyare evaluated left to right

Decision Making: Equality and RelationalOperatorsif structureTest conditions truth or falsity. If condition met execute,otherwise ignoreEquality and relational operatorsLower precedence than arithmetic operatorsTable of relational operators on next slide

Decision Making: Equality and RelationalOperatorsStandard algebraicequality operator orrelational operatorRelational operatorsC++ equalityor relationaloperatorExampleof C++conditionMeaning ofC++ condition> > x > y x is greater than y< < x < y x is less than y≥ >= x >= y x is greater than or equal to y≤

Decision Making: Equality and RelationalOperatorsusing statementsEliminate the need to use the std:: prefixAllow us to write cout instead of std::coutTo use the following functions without the std::prefix, write the following at the top of the programusing std::cout;using std::cin;using std::endl;

Using if statements, relational, operators,and equality operators1234567891011121314151617181920212223// Fig. 1.14: fig01_14.cpp// Using if statements, relational// operators, and equality operators.#include usingusingusingstd::cout; // program uses coutstd::cin; // program uses cinstd::endl; // program uses endl// function main begins program executionint main(){int num1; // first number to be read from userint num2; // second number to be read from usercout num1 >> num2; // read two integersif ( num1 == num2 )cout

Using if statements, relational, operators,and equality operators2425262728293031323334353637383940414243if ( num1 != num2 )cout

Using if statements, relational, operators,and equality operators1234567891011121314151617181920212223// Fig. 1.14: fig01_14.cpp// Using if statements, relational// operators, and equality operators.#include using std::cout; // program uses coutusing std::cin; // program uses cinusing std::endl; // program uses endl// function main begins program executionint main(){int num1; // first number to be read from userint num2; // second number to be read from usercout num1 >> num2; // read two integersif ( num1 == num2 )cout

Using if statements, relational, operators,and equality operators1234567891011121314151617181920212223// Fig. 1.14: fig01_14.cpp// Using if statements, relational// operators, and equality operators.#include using std::cout; // program uses coutusing std::cin; // program uses cinusing std::endl; // program uses endl// function main begins program executionint main(){int num1; // first number to be read from userint num2; // second number to be read from usercout num1 >> num2; // read two integersNotice the using statements.if ( num1 == num2 )Enter two integers, and I will tell youcout

Using if statements, relational, operators,and equality operators1234567891011121314151617181920212223// Fig. 1.14: fig01_14.cpp// Using if statements, relational// operators, and equality operators.#include using std::cout; // program uses coutusing std::cin; // program uses cinusing std::endl; // program uses endl// function main begins program executionint main(){int num1; // first number to be read from userint num2; // second number to be read from usercout num1 >> num2; // read two integersif ( num1 == num2 )cout

Using if statements, relational, operators,and equality operators2425262728293031323334353637383940414243if ( num1 != num2 )cout

Using if statements, relational, operators,and equality operators2425262728293031323334353637383940414243if ( num1 != num2 )cout

Using if statements, relational, operators,and equality operatorsEnter two integers, and I will tell youthe relationships they satisfy: 3 73 is not equal to 73 is less than 73 is less than or equal to 7Enter two integers, and I will tell youthe relationships they satisfy: 22 1222 is not equal to 1222 is greater than 1222 is greater than or equal to 12Enter two integers, and I will tell youthe relationships they satisfy: 7 77 is equal to 77 is less than or equal to 77 is greater than or equal to 7

Thinking About Objects: Introduction to ObjectTechnology and the Unified Modeling LanguageObject orientationNatural way to think about the world and to write computerprogramsAttributes - properties of objectsSize, shape, color, weight, etc.Behaviors - actionsA ball rolls, bounces, inflates and deflatesObjects can perform actions as wellInheritanceNew classes of objects absorb characteristics from existing classesInformation hidingObjects usually do not know how other objects areimplemented

Thinking About Objects: Introduction to ObjectTechnology and the Unified Modeling LanguageAbstraction - view the big pictureSee a photograph rather than a group of colored dotsThink in terms of houses, not bricksClass - unit of programmingClasses serve as a “Blueprint" of objectsObjects are created from a classClasses contain functionsUsed to implement behaviorsClasses contain dataUsed to implement attributesClasses are reusable

Thinking About Objects: Introduction to ObjectTechnology and the Unified Modeling LanguageUnified Modeling Language (UML)Used to model object-oriented systems and aid withtheir designComplex, feature-rich graphical language