Object Oriented Programming with C++ Dr. Debasish Jana BE(CS)(JU), MMATH(CS)(UW, Canada), MBA(Fin)(IGNOU), PhD(CS, JU), FIE(I), FIETE, SMIEEE, SMACM
debasishj at gmail dot com 2019
Books • The C++ Programming Language By Bjarne Stroustrup • C++ and Object-Oriented Programming Paradigm By Debasish Jana
DJ
2
3/22/2019
Binary Number Conversion Binary Decimal 1001010two = ?ten Binary Digit
Decimal Binary 74ten = ?two
Decimal Value
Decimal
Binary (odd?)
0
0 x 20 =
0
74
0
1
1 x 21 =
2
/2 = 37
1
0
0 x 22 =
0
/2 = 18
0
1
1 x 23 =
8
/2 =
9
1
0
0 x 24 =
0
/2 =
4
0
0
0 x 25 =
0
/2 =
2
0
1
1 x 26 = 64
/2 =
1
1
S = 74ten
3/22/2019
Collect
DJ
1001010two
3
Signed Integer Representation How to represent -5? Sign & magnitude (8-bit example): sign
7-bit magnitude (0 … 127)
Rules for addition, a + b: • • • •
If (a>0 and b>0): add b to a If (a>0 and b<0): subtract b from a … +0, -0 are they equal? If so, comparator must handle special case! (If not equal???)
Cumbersome • ”Complicated” hardware: reduced speed / increased power • Is there a better way?
3/22/2019
DJ
4
4-bit Example Decimal
7
7
+ -3
+ -3
4 4
Binary
+ 16
+
+ 16
7
0111
13
+ 1101
4+ 16
1 0100
0100 + 1 0000
• Map negative positive numbers
Extra bit is ignored as Example for N=4-bit: -3 24 – 3 = 13 doesn’t fit in 4 bits. “Two’s complement” No special rules for adding positive and negative numbers
-8
-7
…
-1
0
1
…
7
0
1
…
7
8
9
…
15
+ 24 = 16 3/22/2019
DJ
5
Two’s Complement (8-bit example) As Signed Decimal
As Unsigned Decimal
Binary Number
-128
128
1000 0000
129
1000 0001
…
…
…
-2
254
1111 1110
-1
255
1111 1111
0
0
0000 0000
1
0000 0001
…
...
-127
1 …
+256
+0
127 127 0111 1111 Most-significant bit (MSB) equals sign when interpreted as a signed number
3/22/2019
DJ
6
Signed versus Unsigned Binary Numbers • Both are stored as a collection of bits • The same set of bits can be used to represent a signed number or an unsigned number (or a string, or a picture, or a….) • It’s up to the operation interpreting the bits 1101two 1101two 1101two 1101two 1101two 3/22/2019
= +13ten (as an unsigned number) = -3ten (as a signed number) = orange (as a color) = cat (as a type of animal) = whatever you want it to mean… DJ
7
Unary Negation (Two’s Complement) 4-bit Example (-8ten … +7ten) Brute Force & Tedious Clever & Elegant ”largest” 4-bit number + 1
-
16ten
10000two
3ten
- 0011two
13ten
1101two
16ten
10000two
- 13ten
- 1101two
3ten
0011two
3/22/2019
15ten
01111two
3ten
- 0011two
12ten
1100two
+ 1ten
+ 0001two
13ten
1101two
-
DJ
invert
8
Your Turn • What is the decimal value of the following binary 8-bit 2’s complement number? 1110 0001two
Answer RED ORANGE GREEN YELLOW 3/22/2019
Value 33ten -31ten 225ten -33ten DJ
9
Addition 4-bit Example Unsigned
Signed (Two’s Complement) 3ten
0011two
4ten
+ 0100two
0111two
7ten
0111two
3ten
0011two
3ten
0011two
+ 11ten
+ 1011two
+ -5ten
+ 1011two
14ten
1110two
-2ten
1110two
+
3ten
0011two
4ten
+ 0100two
7ten
+
No special rules for two’s complement signed addition 3/22/2019
DJ
10
Overflow 4-bit Example Signed addition (Two’s Complement)
Unsigned addition
+
13ten
1101two
14ten
+ 1110two
27ten
1 1011two
+
-3ten
1101two
-2ten
+ 1110two
-5ten
1 1011two
carry-out and overflow
carry-out but no overflow
7ten
0111two
7ten
0111two
+ 1ten
+ 0001two
+ 1ten
+ 0001two
8ten
0 1000two
-8ten
0 1000two
no carry-out but overflow
no carry-out and no overflow
Carry-out Overflow 3/22/2019
Carry-out Overflow DJ
11
Addition Overflow Detection 4-bit Example Unsigned addition
Signed addition (Two’s Complement)
• Carry-out indicates overflow
• Overflow if – Signs of operands are equal AND – Sign of result differs from sign of operands
• No overflow is possible when signs of operands differ
Overflow rules depend on the operation (signed vs unsigned addition)
3/22/2019
DJ
12
Your Turn • Which range of decimals can be expressed with a 6-bit two’s complement number?
3/22/2019
Answer
Range
RED GREEN ORANGE YELLOW
-32 … 32 -64 … 63 -31 … 32 -32 … 31
DJ
13
Levels of Representation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp;
High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., RISC-V)
lw lw sw sw
Assembler Machine Language Program (RISC-V)
0000 1010 1100 0101
1001 1111 0110 1000
Current Focus
t0, 0(s2) Anything can be represented t1, 4(s2) as a number, t1, 0(s2) i.e., data or instructions t0, 4(s2) 1100 0101 1010 0000
0110 1000 1111 1001
1010 0000 0101 1100
1111 1001 1000 0110
0101 1100 0000 1010
1000 0110 1001 1111
Machine Interpretation Hardware Architecture Description (e.g., block diagrams) Architecture Implementation Logic Circuit Description (Circuit Schematic Diagrams) 3/22/2019
DJ
14
Hello World C
Java
C++ #include using namespace std; int main() { cout << “Hello World!” << endl; return 0; }
3/22/2019
#include int main() { std::cout << “Hello World!” << std::endl; return 0; }
DJ
15
Compilation & Running $ g++ HelloWorld.cpp $ ./a.out Hello World! $
3/22/2019
DJ
16
C++ Compilation Simplified Overview (more later in course)
foo.cpp
bar.cpp
C++ source files (text)
Compiler
Compiler
Compiler/assembler combined here
foo.o
bar.o
Machine code object files
lib.o
Linker
a.out 3/22/2019
Pre-built object file libraries
Machine code executable file DJ
17
Compilation versus Interpretation C++ (compiled) • Compiler (& linker) translates source into machine language • Machine language program is loaded by OS and directly executed by the hardware
3/22/2019
Python (interpreted) • Interpreter is written in some high-level language (e.g. C) and translated into machine language • Loaded by OS and directly executed by processor • Interpreter reads source code (e.g. Python) and “interprets” it
DJ
18
Java “Byte Code” • Java compiler (javac) translates source to “byte code” • “Byte code” is a particular assembly language – Just like i86, RISC-V, ARM, … – Can be directly executed by appropriate machine • implementations exist(ed), not commercially successful
– More typically, “byte code” is • interpreted on target machine (e.g. i86) by java program • compiled to target machine code (e.g. by JIT)
– Program runs on any computer with a “byte code” interpreter (more or less) 3/22/2019
DJ
19
Compilation • Excellent run-time performance: – Much faster than interpreted code (e.g. Python) – Usually faster than Java (even with JIT)
• Note: Computers only run machine code – Compiled application program, or – Interpreter (that interprets source code)
3/22/2019
DJ
20
Compilation: Disadvantages • Compiled files, including the executable, are – architecture-specific, depending on processor type • e.g., RISC-V vs. ARM
– and the operating system • e.g., Windows vs. Linux
• Executable must be rebuilt on each new system – I.e., “porting your code” to a new architecture
• “Change Compile Run [repeat]” iteration cycle can be slow during development – Recompile only parts of program that have changed – Tools (e.g. make) automate this 3/22/2019
DJ
21
C++ Pre-Processor foo.cpp
foo.i
C++ PP
Compiler
• C++ source files pass through macro processor, C++ PP, before compilation • C++ PP replaces comments with a single space • C++ PP commands begin with “#” • #include “file.h” /* Inserts file.h */ • #include <stdio.h> /* Loads from standard loc */ • #define M_PI (3.14159) /* Define constant */ • #if/#endif /* Conditional inclusion of text */ • Use –save-temps option to gcc/g++ to see result of preprocessing • Full documentation at: http://gcc.gnu.org/onlinedocs/cpp/ 3/22/2019
DJ
22
Typed Variables in C/C++ • Declare before use • Type cannot change • Like Java
int a = 4; float f = 1.38e7; char c = ‘x’; Type
Description
Examples
int
integers, positive or negative
0, 82, -77, 0xAB87
unsigned int
ditto, no negatives
0, 8, 37
float
(single precision) floating point
3.2, -7.9e-10
char
text character or symbol
’x’, ‘F’, ‘?’
double
high precision/range float
1.3e100
long
integer with more bits
427943
3/22/2019
DJ
23
Constants and Enumerations in C/C++ • Constants – Assigned in typed declaration, cannot change – E.g. • const float pi = 3.1415; • const unsigned long addr = 0xaf460;
• Enumerations
3/22/2019
DJ
24
Integers: Python vs. Java vs. C/C++ Language Python Java C/C++
sizeof(int) >=32 bits (plain ints), infinite (long ints) 32 bits Depends on computer; 16 or 32 or 64
• C/C++: int – integer type that target processor works with most efficiently
• Only guarantee: – sizeof(long long) ≥ sizeof(long) ≥ sizeof(int) ≥ sizeof(short) – Also, short >= 16 bits, long >= 32 bits – All could be 64 bits
•3/22/2019 Impacts portability between architectures DJ
25
Variable Sizes: Machine Dependent! sizeof ... (bytes) char: 1 short: 2 int: 4 unsigned int: 4 long: 8 long long: 8 float: 4 double: 8 3/22/2019
DJ
26
Boolean
• No boolean datatype in C
– Declare if you wish: typedef int boolean; const boolean false = 0; const boolean true = 1;
• What evaluates to FALSE in C? – 0 (integer) – NULL (a special kind of pointer: more on this later)
• What evaluates to TRUE in C? – Anything that isn’t false is true – Similar to Python: only 0’s or empty sequences are false, everything else is true!
3/22/2019
DJ
27
Functions in C/C++ • Like Java • Declare return & argument types • void for no value returned • Functions MUST be declared before they are used
int number_of_people() { return 3; } void news() { printf(”no news”); } int sum(int x, int y) { return x + y; }
3/22/2019
DJ
28
Uninitialized Variables Code
Output
$ gcc test.c $ x x x x x
3/22/2019
DJ
./a.out = 0 = 16807 = -4 = 282475249 = -16
29
Struct’s in C • Struct’s are structured groups of variables • A bit like Java classes, but no methods • E.g.
3/22/2019
DJ
30
So far … • Signed integers represented in 2’s complement • C Programming Language – Popular (still!) – Similar to Java, but • no classes • explicit pointers (next lecture)
– Beware • variables not initialized • variable size (# of bits) is machine & compiler dependent
• C is compiled to machine code – Unlike Python or Java, which are interpreted – Compilation is faster than interpretation 3/22/2019
DJ
31
What is a Computer Program? • To exactly know, what is data structure? We must know: – What is a computer program?
Input DJ
Some mysterious processing 32
Output 3/22/2019
Example • Data structure for storing data of students:– Arrays – Linked Lists
• Issues – Space needed – Operations efficiency (Time required to complete operations) • Retrieval • Insertion • Deletion
DJ
33
3/22/2019
What data structure to use? Data structures let the input and output be represented in a way that can be handled efficiently and effectively. array Linked list
tree DJ
queue stack 34
3/22/2019
Abstract Data Types
Data types I • We type data--classify it into various categories--such as int, char, float, double – A data type represents a set of possible values, such as {..., -2, -1, 0, 1, 2, ...}, or {true, false}
• By typing our variables, we allow the computer to find some of our errors – Some operations only make sense when applied to certain kinds of data--multiplication, searching
• Typing simplifies internal representation – A char array requires more and different storage than a float 36 3/22/2019
DJ
Data types II • A data type is characterized by: – a set of values – a data representation, which is common to all these values, and – a set of operations, which can be applied uniformly to all these values
37 3/22/2019
DJ
Primitive types in C/C++ • C/C++ provides primitive types, e.g. – char, short, int, long – float, double – unsigned int, int
• Each primitive type has – a set of values – a data representation – a set of operations
• These are “set in stone”—there is nothing the programmer can do to change anything about them 38 3/22/2019
DJ
bool Option 1 typedef int bool; #define true 1 #define false 0 Option 2 typedef int bool; enum { false, true }; Option 3 typedef enum { false, true } bool; Option 4 (C99) #include <stdbool.h> 3/22/2019
DJ
39
Primitive types as data types Type
Values
Representation Operations
bool
true, false
Single byte
&&, ||, !
char, short, int, long
Integers of varying sizes
Two’s complement
+, -, *, /, others
float, double
Floating point Two’s complement numbers of with exponent and varying sizes mantissa and precisions
+, -, *, /, others
3/22/2019
40 DJ
Methods and operators • An operator typically – Is written with non-alphabetic characters: +, *, ++, +=, &&, etc. – Is written as prefix, infix, or postfix: -x, x+y, x++ – Has only one or two arguments, or operands
• A function typically – Is written with letters, and its arguments are enclosed in parentheses: fun(), abs(n) – Has any (predetermined) number of arguments 41 3/22/2019
DJ
Abstract Data Types • An Abstract Data Type (ADT) is: – a set of values – a set of operations, which can be applied uniformly to all these values
• To abstract is to leave out information, keeping (hopefully) the more important parts – What part of a Data Type does an ADT leave out? 42 3/22/2019
DJ
Data Structures • Many kinds of data consist of multiple parts, organized (structured) in some way • A data structure is simply some way of organizing a value that consists of multiple parts – Hence, an array is a data structure, but an integer is not
• When we talk about data structures, we are talking about the implementation of a data type • If I talk about the possible values of, say, complex numbers, and the operations I can perform with them, I am talking about them as an ADT • If I talk about the way the parts (“real” and “imaginary”) of a complex number are stored in memory, I am talking about a data structure • An ADT may be implemented in several different ways – A complex number might be stored as two separate doubles, or as an array of two doubles, or even in some bizarre way 43 3/22/2019
DJ
Data representation in an ADT • An ADT must obviously have some kind of representation for its data – The user need not know the representation – The user should not be allowed to tamper with the representation – Solution: Make all data private (c++)
• But what if it’s really more convenient for the user to have direct access to the data? – Solution: Use setters and getters 44 3/22/2019
DJ
Example of setters and getters class Pair {
private: int first, last; public: int getFirst() { return first; } void setFirst(int first) { this.first = first; }
};
int getLast() { return last; } Void setLast(int last) { this.last = last; }
45 3/22/2019
DJ
Naming setters and getters • Setters and getters should be named by: – Capitalizing the first letter of the variable (first becomes First), and – Prefixing the name with get or set (setFirst) – For boolean variables, replace get with is (for example, isRunning)
• This is more than just a convention—if and when you start using JavaBeans, it becomes a requirement 46 3/22/2019
DJ
What’s the point? • Setters and getters allow you to keep control of your implementation • For example, you decide to define a Point in a plane by its x-y coordinates:
– class Point { public int x; public int y; } • Later on, as you gradually add methods to this class, you decide that it’s more efficient to represent a point by its angle and distance from the origin, θ and ρ • Sorry, you can’t do that—you’ll break too much code that accesses x and y directly • If you had used setters and getters, you could redefine them to compute x and y from θ and ρ 47 3/22/2019
DJ
Contracts • Every ADT should have a contract (or specification) that: – Specifies the set of valid values of the ADT – Specifies, for each operation of the ADT: • • • •
Its name Its parameter types Its result type, if any Its observable behavior
– Does not specify: • The data representation • The algorithms used to implement the operations 48 3/22/2019
DJ
Importance of the contract • A contract is an agreement between two parties; in this case – The implementer of the ADT, who is concerned with making the operations correct and efficient – The applications programmer, who just wants to use the ADT to get a job done
• It doesn’t matter if you are both of these parties; the contract is still essential for good code • This separation of concerns is essential in any large project 49 3/22/2019
DJ
Promise no more than necessary • For a general API, the implementer should provide as much generality as feasible • But for a specific program, the class author should provide only what is essential at the moment – In Extreme Programming terms, “You ain’t gonna need it!” – In fact, XP practice is to remove functionality that isn’t currently needed! – Your documentation should not expose anything that the application programmer does not need to know
• If you design for generality, it’s easy to add functionality later—but removing it may have serious consequences 50 3/22/2019
DJ
Implementing an ADT • To implement an ADT, you need to choose: – a data representation • must be able to represent all necessary values of the ADT • should be private
– an algorithm for each of the necessary operations • must be consistent with the chosen representation • all auxiliary (helper) operations that are not in the contract should be private
• Remember: Once other people (or other classes) are using your class: – It’s easy to add functionality – You can only remove functionality if no one is using it! 51 3/22/2019
DJ
C/C++ : Declarations and Expressions • • • • •
Structure of a C/C++ program Compilation and Linking with single file, multiple files Return type of main – strictly int (not void) – reasons? Data types – fundamental (built-in) and derived Fundamental data types – – – – – – –
3/22/2019
Type – char, int, float, double, bool Number storage and reprsentation Numerical ranges of values Size dependency on platform/machine architecture Qualifiers e.g. unsigned, short, long as applicable Effect of qualifiers on numerical ranges, size? Effect on performance / size on choice of data type on different architecture
DJ
52
C/C++ : Declarations and Expressions.. Contd/2 • • • • •
Variables Constants using Macro Side-effects of macro Macro function vs. inline function Macro constant vs. declared constant through const – which one is preferred and why • Enumerated constants
3/22/2019
DJ
53
C/C++ : Declarations and Expressions.. Contd/3 • Operators and Expressions – Relational operators – Assignation Operator – Arithmetic operators – Increment and decrement operators – post and pre – Compound assignment operators – Bitwise operators – Logic operators – Conditional operator – Cast operator – sizeof() operator and its benefits on portable code – Operator precedence and associativity – Lvalue vs rvalue e.g. pre-increment is lvalue but post-increment is not 3/22/2019
DJ
54
C/C++ : Declarations and Expressions.. Contd/4 • Pointer – Storage of address as data in terms of flat memory model – Address-of operator – Indirection Operator – Pointer arithmetic fundamentals – Void and void pointer as exception
3/22/2019
DJ
55