31
Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 1 1

Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

Programming in OOP/C++

ByAssistant Professor Dr. Ali Kattan

IntroductionLecture 1

1

Page 2: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

What is OOP?OOP is short for Object-Oriented Programming. It is a concept that helps to writecode based on objects. This will make the process of software developmentfaster and easier to maintain.

OPP main advantage is “Software Reuse”. It saves considerable softwaredevelopment time by enabling the creation of new classes (code) from existingones.

New classes “inherit” existingfeatures with the ability to add new,augment, modify or remove featuresin the new classes.

Page 3: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

What is Inheritance?Let’s take a simple example for developing a “car” game that has different cars.This is just to explain the concept, we are not going to write code.

In such games you can drive different cars. The question is, do programmersprogram each car code individually? Ask yourself:

• The more cars you have in the game the more code you will write.• If you want to change something to make the car better, you need to go and

change the code for all your cars.• If you want to add a new feature for the car you need to go and do this for all

the cars’ code.• What will happen to the overall size of the program if I have more and more

cars?

Page 4: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

4

Coding without OOP

You have write code for each car individually. If you want to change somethingyou have to open the code for each one. Too much code , not easy to edit andtoo much work.

Page 5: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

5

Think of an object “car” shown in the diagram shown. We call this a “class” inour code.

The “car” has many members that describe “has a” relationship. Thesemembers are called object’s variables. The can have different values.

Example: speed = 0.0 Km/h, wheels = 4, fuel = 35.5L, Steering = 30o , etc.

Coding with OOP: Basic Concept

Page 6: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

6

Object

Notice that all the other types of cars have the same thing as “car”.They have wheels, steering, fuel, doors, windows, etc.

But each of these cars have differences. We write code only for the differentparts not available in the original “car” object. These are “Sub-classes”

Think about each car above?

Sub-Class

Class

Page 7: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

7

We can from the sub-classes make more sub-classes. They will “inherit” thefeatures of the parent class and we can add new code for the differences.

Page 8: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

8

You only write code add features or remove. The original code is still used.

This is FASTER than standard non-OOP programming

Page 9: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

9

C++ is an OOP LanguageOOP is a programming technique and not only for C++.

There are other OOP languages such as Java, Visual Basic, Python, C#, etc.

We will learn how to program OOP in C++

Page 10: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

Review of Important Terminology

10

Before we start our subject it is very important to remember & understandimportant terminology (from last year)

Higher-level programming languages like C++ allowprogrammers to express solutions to programmingproblems in terms that are much closer to a naturallanguage like English

Machine Code? The language the the computer actually understand,0’s and 1’s. This is also known as “Low-levelprogramming”.

Higher-level Programming Language?

Source Code? The program you type in a certain high-levelprogramming language and save in a file.

Compiler? A program (like CodeBlocks or MS Visual Studio) thattranslates your source code to machine code.The compiler will check and verify the “syntax” and makessure that everything is correct.

Page 11: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

11

The IDE is Integrated Development Environment. It is aprogramming development tool includes not only thecompiler but also the preprocessor, editor anddebugger that let you debug your program (find errorsand fix them)

C++ Source Code

ExecutableProgram

Editor, Preprocessor, Compiler, Linker

Debugger

IDE

IDE?

This year we will use two IDEs; the first is CodeBlocks and the second is MS Visual Studio (C++).

Page 12: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

Review important C/C++ code

12

You need to re-read our previouslectures form 1st year.

If you do not understand the basics ofC/C++ YOU WILL HAVE PROBLEMS!

#include <iostream>

int main() {

std::cout << ”Hello World!\n";return 0;

}

Source Code for a Simple C++ Program

Page 13: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

Writing C++ Program

13

Do you know how to Edit, Compile & Run this Source Code.

Page 14: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

14

Values & Variables

14

In C++, as in many other programming languages, a variable must have a specific

type like integer, float and we must know what is the range required for each. If

we want to repeat the same math example in the previous slide:

Programming in C++, Lecture 03, Asst. Prof. Dr. Ali Kattan

Notice that before each variable we have the word “int”. This tells the C++

compiler that x, y, and z will hold only integer values ( .. -4, -3, -2, -1, 0, 1, 2, 3, 4. …)

#include <iostream>int main() {

int x;x = 3;int y;y = 2;int z;z = x + y;std::cout << z << ‘\n’;return 0;

}

x = 3y = 2Find z = x + y

Page 15: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

15

Integer, float, double Variables & Values

15Programming in C++, Lecture 03, Asst. Prof. Dr. Ali Kattan

There are other types of integer used in C++:

This will tell C++ compiler what is the exact type required. L: long integerLL: long long integer u: unsigned integeruL: unsigned long integeruLL: unsigned long long integer.

The example shown on right shows how to declareeach type. Notice the “suffixes” after the number.

The number is the same but the storage and range isdifferent for each.

Page 16: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

1616

C++ supports such non-integer numbers, and they are called floating-point numbers.

Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan

The types float and double represent different types of floating-point numbers.

The type double is used more often, since it stands for “double-precision floating-point,” and it can represent a wider range of values with more digits of precision.

The float type represents single-precision floating-point values that are less precise.

Precision means how accurate we want the number to be in the computer.Remember, more precision means more memory to use.

Page 17: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

17

Variable names (or identifiers)

17Programming in C++, Lecture 03, Asst. Prof. Dr. Ali Kattan

We learned that if want to declare a variable we must give it a name like x, y and z.

But in programming the names x, and y and z are not very useful sometimes:

int x = 25;

Suppose we want to declare the total number of students in a classroom, which is 25.

Which one of the following declarations is better?

int students = 25;

In programming, we call both ‘x’ and ‘students’ identifiers. An identifier is simply the

name we give to any variable in our program.

However, we cannot just use any identifier as a name. There are rules.

• An identifier must have at least one “character”.

• An identifier must start with an alphabetic letter, upper or lower case, or the

underscore “_”.

• An identifier may contain digits after the first character.

• An identifier must NOT have spaces or special symbols like $, #, *, & …

• An identifier must NOT use a word that is part of C++ language.

Page 18: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

18

Constants

18Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan

A “Constant” is a value that does not need to change. For example ! =3.14159

Look at the program below. We declare pi as a variable and assign it 3.14159The value of pi in real life never changes. Thus this is a bad way to declare it.

Here pi is declared as a “Constant”.You cannot change it in the program.You will get compiler error if you try.

Page 19: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

19

Character Variables and Characters

19Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan

In programming we need also to sometimes store letters, which we generally call them“Characters”, like ‘A’, ‘d’, ‘K’, ‘Z’, ‘3’, ‘0’, ‘*’, ‘$’ and so on.The char data type is used to represent single characters: letters of the alphabet (bothupper and lower case), digits, punctuation, and control characters (like newline and tabcharacters).

In C++ source code, characters are enclosed by single quotes ('), as inchar ch = 'A'; We DO NOT use double quotes (") since these are for strings (we will study later). Thus,the following statement would produce a compiler error message:char ch = "A";We can also declare character constants:

Page 20: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

20

Expressions & Arithmetic C++ uses the operators shown on the right to do simplearithmetic operations. Integer division vs. integer modulus.Integer division produces the quotient, and modulusproduces the remainder.

20

Page 21: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

21

Using if / else Statement

21Programming in C++, Lecture 06, Asst. Prof. Dr. Ali Kattan

There is a better way to write this code again by use the if / else statement:

Number

Check?

Number >= 0

Print Greater or Equal to zero

Number < 0

Print less than zero

int main() {double Number;std::cout << “Enter Number?”;std::cin >> Number;if (Number>=0)std::cout <<“Grater or equal to 0”;

elsestd::cout <<“Less than 0”;

return 0;}

Page 22: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2222Programming in C++, Lecture 06, Asst. Prof. Dr. Ali Kattan

Using Nested if / else Statements

/*Simple calculator program that uses a menuNoble InstituteInformation Technology Department

*/#include <iostream>int main(){

double x, y; //first & second numberchar menu; //menu selection character

//input the two numbersstd::cout << "Enter x? ";std::cin >> x;std::cout << "Enter y? ";std::cin >> y;

//print the menustd::cout << "Choose: " << "\n";std::cout << " a : add" << "\n";std::cout << " b : subtract" << "\n";std::cout << " c : multiply" << "\n";std::cout << " d : divide" << "\n";std::cout << " e : exit" << "\n";

std::cout << "Enter Choice? ";std::cin >>menu;//apply choiceif (menu=='a')

std::cout << "add result = " << x+y;else if (menu=='b')

std::cout << "subtract result = " << x-y;else if (menu=='c')

std::cout << "multiply result = " << x*y;else if (menu=='d')

std::cout << "divide result = " << x/y;else if (menu=='e')

std::cout << "Exit .. good bye.";else

std::cout << "Wrong choice!!";return 0;

}

A better way would be to use nested if..else statements such that there is no needto check another if once the one if before is true.Notice that we indent for each if..else . For a large number of if..else this willtake more space to scroll left.

Page 23: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2323

if..else ladder and switch statementBelow is the same code by using “switch” statement:

Page 24: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2424

Compound Boolean ConditionsIn programming conditional statements using if, wesometimes need to specify more than one condition to resulteither true or false. In the table below, A and B representconditions.

Examples:

• If the shirt price is less than $80 AND the size is L, buy the shirt.

• If the temperature is above 35C OR you feel hot, turn on the A/C unit.

• If NOT height more than 180cm, print “Average”

Page 25: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2525

Iterations (loops)Iteration means the repeated execution of some code. This isknown as ”Loop”. In C++, there are 3 types of loop structuresshown below

We will study the basic idea of loops using “for” loop in this lectureand we will cover the other two (while loop and do .. while loop) incoming lectures.

Page 26: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2626

The “for” loopPay attention, how ”for” loop works:

• The initialization defines a loop variable to use by “for” and givethe loop variable a start value (in our it’s an integer starting atvalue 1).

for (int k=1 ; k<=10 ; k=k+1)

• The condition defines when the loop continues. This is“Boolean” and if the result is true, loop continue, if false, loopterminates, meaning stop (in our case k<=10).

• The modification define how to change the loop variable aftercompleting one cycle (in our case k=k+1).

ß Remember, if more than one statement use { and }

Page 27: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2727

The while statementThe while statement in C++ has the following simple form:

It means in English: while the condition is true, do the statement.The loop will stop only when the condition is false.Notice that if the condition is not true, the loop will never run.

If you have more than one statement, then use curly braces:

{

}

Page 28: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2828

The do..while statementThe do..while statement in C++ has the following simple form:

It means in English: do the statement, then check the condition andif true do the statement again. The loop will stop only when thecondition is false.

Notice that the loop will run at least once even if the condition isfalse.

Page 29: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

2929

Using continue and break with loops

continue : will cause the loop to take the next cycle and ignore(skip) any statements that are in the loop after continue.

break: will cause the loop to terminate and exit.

Both statements can be used withfor, while and do..whileloops.

In any of the loops we studied before we can make use of twostatements:

These statements are usually used with if to test a condition andare executed if the condition is true.

Page 30: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

30

Remember:

• If you don’t practice C++programming on the computer,YOUWILL NOT LEARN ANYTHING.

• To become a professionalprogrammer you must try all theexamples by yourself on acomputer.

Page 31: Programming in OOP/C++ · 16 C++ supports such non-integer numbers, and they are called floating-point numbers. Programming in C++, Lecture 04, Asst. Prof. Dr. Ali Kattan The types

How can I get the lecture notes?

31

• Check the website www.alikattan.org

Note: it will be ready AFTER 17-10-2018

• One hardcopy will be provided after the lecture for

photocopying.

• By email: [email protected] (if there is a problem only)