Академический Документы
Профессиональный Документы
Культура Документы
with C++
Dr. Debasish Jana
BE(CS)(JU), MMATH(CS)(UW, Canada), MBA(Fin)(IGNOU), PhD(CS, JU),
FIE(I), FIETE, SMIEEE, SMACM
DJ 2 3/22/2019
Binary Number Conversion
Binary Decimal Decimal Binary
1001010two = ?ten 74ten = ?two
Binary Decimal Value
Decimal Binary (odd?)
Digit
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 Collect 1001010two
3/22/2019 DJ 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 Binary
7 7 7 0111
+ -3 + -3 + + 13 + 1101
4 16 4+ 1 0100 + 1
4 + 16 0100 0000
16
• 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 As Unsigned Binary Number
Decimal Decimal
-128 128 1000 0000
-127 +256 129 1000 0001
… … …
-2 254 1111 1110
-1 255 1111 1111
0 0 0000 0000
+0
1 1 0000 0001
… … ...
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 = +13ten (as an unsigned number)
1101two = -3ten (as a signed number)
1101two = orange (as a color)
1101two = cat (as a type of animal)
1101two = whatever you want it to mean…
3/22/2019 DJ 7
Unary Negation (Two’s Complement)
4-bit Example (-8ten … +7ten)
3ten 0011two
3/22/2019 DJ 8
Your Turn
• What is the decimal value of the following
binary 8-bit 2’s complement number?
1110 0001two
Answer Value
RED 33ten
ORANGE -31ten
GREEN 225ten
YELLOW -33ten
3/22/2019 DJ 9
Addition
4-bit Example
Unsigned Signed (Two’s Complement)
3/22/2019 DJ 12
Your Turn
• Which range of decimals can be expressed
with a 6-bit two’s complement number?
Answer Range
RED -32 … 32
GREEN -64 … 63
ORANGE -31 … 32
YELLOW -32 … 31
3/22/2019 DJ 13
Levels of Representation
C++
#include <iostream.h>
#include <iostream>
using namespace std;
int main() {
int main() {
std::cout << “Hello World!” << std::endl;
cout << “Hello World!” << endl;
return 0;
return 0;
}
}
3/22/2019 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)
3/22/2019 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 C++ PP foo.i 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++
int a = 4;
• Declare before use
float f = 1.38e7; • Type cannot change
char c = ‘x’; • Like Java
3/22/2019 DJ 24
Integers: Python vs. Java vs. C/C++
Language sizeof(int)
Python >=32 bits (plain ints), infinite (long ints)
Java 32 bits
C/C++ 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
DJ architectures 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++
int number_of_people() { • Like Java
return 3;
} • Declare return &
argument types
void news() {
• void for no value
printf(”no news”);
}
returned
• Functions MUST be
int sum(int x, int y) { declared before
return x + y;
}
they are used
3/22/2019 DJ 28
Uninitialized Variables
Code
Output
$ gcc test.c
$ ./a.out
x = 0
x = 16807
x = -4
x = 282475249
x = -16
3/22/2019 DJ 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?
Some mysterious
processing Output
Input
DJ 32 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
queue
tree stack
DJ 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 &&, ||, !
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; }
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
3/22/2019 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