0% found this document useful (0 votes)
15 views15 pages

Lecture 3

OOP Lecture 3

Uploaded by

Noor Ghazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

Lecture 3

OOP Lecture 3

Uploaded by

Noor Ghazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Object Oriented Programming –

CS/CE 224/272
Nadia Nasir

Habib University, Fall 2024


Content
• Switch statements
• Enum
• Functions
• Cast and type conversion
Switch
• C++ provides switch construct for decisions
• Anywhere you use switch, you can if/else if/else blocks
• However, switch block is a more concise representation
• They are useful when conditions are simple and a variable takes on
predetermined number of values e.g.
Enumerations – named ints
• Enums can be treated as named ints (old way).
• We will consider this
• Modern and better way is to use ‘enum class’
• I will leave this as a reading for you

• Unless specified, the first item in


enum list is assigned a value of 0
implicitly as shown below

enum DAYS {MONDAY = 0, TUESDAY, WEDNESDAY,


THURSDAY, FRIDAY, SATURDAY, SUNDAY};
Enumeration – named ints
• What should happen with the following code?
Casting
• We have seen type conversion in python.
• Casts are used to obtain a different type e.g.
• If you want to convert int type to double type
• Or float type to double type
• There are quite a few casts available in C++.
• For now we will only use ‘static_cast<typename>(var)’
Comparing double and float
• Floating point equal comparisons are unreliable!!

Fall 2023, Page 3


Comparing double and float
• You should compare floating point to a certain precision

• You can read up on other ways to do floating point comparison


Fall 2023, Page 4
Functions revisited

We will come back to functions in this course many times!


Functions in C++ = functions in python
• Functions help organise your code logically
• Prevents duplication of code
• C++ function syntax
Parameter Parameter 3
Function Parameter 1
Function 2 name & name &
Parameter 1 Parameter return Function name & type
return Function 2 name & name type
name & type type
type name type
type

Body of the
function
Declaration vs definition

• A function without body is called a declaration


Note semicolon

• Where is the function body?


Declaration vs definition
• Declaration and definition in one shot
• computeArea is declared prior to call from main. This is what you have seen before
Declaration vs definition
• In LHS code, function declared and defined in one shot
• Error: FUNCTION MUST BE DECLARED before it is called
• In RHS code, function declared first and defined after main
• NoError: because computeArea DECLARED BEFORE the call to main

CODE COMPILES
CODE DOESN’T
COMPILE
Declaration vs definition
• We usually “DECLARE” functions in the header file

• and the function is “DEFINED” in the source file

• Separating declaration and definition


• reduces code pollution hence improves readability
• Improves compile time
• Enables you to hide implementation details

• We will see separating declaration and definitions in different files in


future! But remember this concept
Your Task:

Write a program for the following output using switch case:

Sample output 1: Sample output 2:


------------------------- -------------------------
MENU CARD MENU CARD
1.COFFEE Rs:15 1.COFFEE Rs:15
2.TEA Rs:10 2.TEA Rs:10
3.COLD COFFEE Rs:25 3.COLD COFFEE Rs:25
4.MILK SHAKE Rs:50 4.MILK SHAKE Rs:50
Enter Your choice : 2 Enter Your choice : 4

You have selected Tea Milkshake Flavors:


Enter the quantity : 5 1.Pina Colada Rs:200
2. Strawberry marshmallow Rs: 250
Total amount :50 3. Blueberry cheesecake Rs: 320
4. Vanilla special Rs: 540

Enter Your choice : 2


You have selected Strawberry marshmallow
Enter the quantity : 2

Total amount :500

You might also like