11
More C++ Bryce Boe 2013/10/23 CS24, Fall 2013

More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

Embed Size (px)

Citation preview

Page 1: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

More C++

Bryce Boe2013/10/23

CS24, Fall 2013

Page 2: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

Outline

• Project 1 Review• Alignment and Padding• Finish C++ Introduction

Page 3: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

PROJECT 1 REVIEW

Page 4: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

Sample Solution

• <In class explanation of sample code>– Solution will NOT be posted

Page 5: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

Alignment and padding

• Structures and classes are padded so that attributes are aligned on a 4-byte boundary– A char followed by an int would require 2 memory

accesses to read the entire integer if there was no padding

Page 6: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

Placement & padding – word

• Compiler places data at word boundaries– e.g., word = 4 bytes

• Imagine:struct { char a; int b;} x;

• Classes too

variable x

x.a x.b

01001001 10010110000000101101010001101101

a machine word a machine word

datacompletelyignored, junk

paddingCompilers do it this way

variable x

0100100110010110000000101101010001101101

x.a x.b

a machine worda machine word

Not like this!

Page 7: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

C++ INTRODUCTION CONTINUED

Page 8: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

Data Hiding

• Declaring member (instance) variables as private, why?– Assists in separation of implementation and

interface– Allows for input validation and state consistency

Page 9: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

Compilation differences

• Save C++ files with .cpp extension (many people also use .cc)

• Use clang++ rather than clang (really important otherwise odd errors)– All the arguments (for our purposes) are the same– Use g++ rather than gcc if you use that compiler– You can compile any C code with a C++ compiler

(it’s backwards compatible)

Page 10: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

New Types

• bool• std::string (must #include <string>)

Page 11: More C++ Bryce Boe 2013/10/23 CS24, Fall 2013. Outline Project 1 Review Alignment and Padding Finish C++ Introduction

For Monday

• Study for exam (see midterm from last quarter)

• All material from lecture, labs, example code, projects up to C++ (not included) will be on the exam

• Simple C++ I/O lab will be on Monday