25
6.S096 Lecture 4 – Style and Structure Transition from C to C++ Andre Kessler Andre Kessler 6.S096 Lecture 4 – Style and Structure 1/ 24

Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

6.S096 Lecture 4 – Style and StructureTransition from C to C++

Andre Kessler

Andre Kessler 6.S096 Lecture 4 – Style and Structure 1 / 24

Page 2: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

Outline

1 Assignment Recap

2 Headers and multiple files

3 Coding style

4 C++

5 Wrap-up

Andre Kessler 6.S096 Lecture 4 – Style and Structure 2 / 24

Page 3: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

Assignment Recap

Assignment Recap

Floating point

Layout of 32-bit floating point numbers in memory.

Matrix multiply (1 & 2)

Static vs. dynamic allocation

Using a data structure

Optimizing for the cache

Transposition cipher

Safely allocating and reading an arbitrary-size string

Test data

Andre Kessler 6.S096 Lecture 4 – Style and Structure 3 / 24

Page 4: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

Headers and multiple files

Header files

Separation of declaration and definition

// in header (.h)

void increment( int *a );

// in source (.c)

void increment( int *a ) {

++*a;

}

Include guards (file myheader.h)

#ifndef _MYHEADER_H

#define _MYHEADER_H

void increment( int *a );

// ...your header stuff

#endif // _MY_HEADER_H

Andre Kessler 6.S096 Lecture 4 – Style and Structure 4 / 24

Page 5: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

Coding style

Coding style

Consistency and clarity above all.

Vertical space is precious; horizontal space is good

Always using { and } even if not needed

Again: be consistent.

Andre Kessler 6.S096 Lecture 4 – Style and Structure 5 / 24

Page 6: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

The Minimal C Program

nothing.c: takes no arguments, does nothing, returns 0 (“exit success”)

int main(void) {

return 0;

}

1 To compile: make nothing

2 Previous step produced an executable named nothing

3 To run: ./nothing

Andre Kessler 6.S096 Lecture 4 – Style and Structure 6 /

1

2

3

24

Page 7: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

The Minimal C++ Program

nothing.cpp: takes no arguments, does nothing, returns 0.

int main() {

return 0;

}

1 To compile: make nothing

2 Previous step produced an executable named nothing

3 To run: ./nothing

Andre Kessler 6.S096 Lecture 4 – Style and Structure 7 /

1

2

3

24

Page 8: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world!

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

int main() {

return 0;

}

Andre Kessler 6.S096 Lecture 4 – Style and Structure 8 / 24

Page 9: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world!

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <cstdio>

int main() {

return 0;

}

Andre Kessler 6.S096 Lecture 4 – Style and Structure 9 / 24

Page 10: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world!

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <cstdio>

int main() {

printf( "Hello, world!\n" );

return 0;

}

Andre Kessler 6.S096 Lecture 4 – Style and Structure 10 / 24

Page 11: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world!

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <cstdio>

int main() {

printf( "Hello, world!\n" );

return 0;

}

1 To compile: make hello

2 Previous step produced an executable named hello

3 To run: ./hello

4 Hello, world!

Andre Kessler 6.S096 Lecture 4 – Style and Structure 11 /

1

2

3

24

Page 12: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world!

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <cstdio>

int main() {

printf( "Hello, world!\n" );

return 0;

}

1 To compile: make hello

2 Previous step produced an executable named hello

3 To run: ./hello

4 Hello, world!

Andre Kessler 6.S096 Lecture 4 – Style and Structure /

1

2

3

4

2412

Page 13: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world!

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <cstdio>

int main() {

printf( "Hello, world!\n" );

return 0;

}

1 To compile: make hello

2 Previous step produced an executable named hello

3 To run: ./hello

4 Hello, world!

Andre Kessler 6.S096 Lecture 4 – Style and Structure /

1

2

3

4

2413

Page 14: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world! done better

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <iostream>

int main() {

std::cout << "Hello, world!\n";

return 0;

}

1 To compile: make hello

2 Previous step produced an executable named hello

3 To run: ./hello

4 Hello, world!

Andre Kessler 6.S096 Lecture 4 – Style and Structure /

1

2

3

2414

Page 15: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world! done better

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <iostream>

int main() {

std::cout << "Hello, world!\n";

return 0;

}

1 To compile: make hello

2 Previous step produced an executable named hello

3 To run: ./hello

4 Hello, world!

Andre Kessler 6.S096 Lecture 4 – Style and Structure /

1

2

3

4

2415

Page 16: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Hello, world! done better

hello.cpp: takes no arguments, prints “Hello, world!”, returns 0

#include <iostream>

int main() {

std::cout << "Hello, world!\n";

return 0;

}

1 To compile: make hello

2 Previous step produced an executable named hello

3 To run: ./hello

4 Hello, world!

Andre Kessler 6.S096 Lecture 4 – Style and Structure /

1

2

3

4

2416

Page 17: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

What is C++?

Multiparadigm programming language

Has procedural, object-oriented, functional, generic, andmetaprogramming features.

Procedural: underneath the surface, C++ is mostly C

Object-oriented: classes, encapsulation, inheritance, andpolymorphism

Generic: template metapgramming, programs that run duringcompilation

Standard Template Library (STL): common idioms, containers, andalgorithms

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2417

Page 18: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

The Key Differences

Generally, can compile C code with a C++ compiler

References vs pointers

C++ is more strongly typed

Stronger notions of casting, static cast<> and so on

At the same time, auto variables

C++ supports notions of immutability

const correctness and constexpr.

C++ has extensive abstraction mechanisms

Classes and wrapping our memory management

Templates template<typename T>

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2418

Page 19: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Immutability and References

In addition to the good old pointer, C++ also has a notion of a reference.Let’s look at an increment function written in C:

void increment( int *a ) {

++*a;

}

// later: call it with

int a = 5;

increment( &a );

The C++ way:

void increment( int &a ) {

++a;

}

// later: call with

int a = 5;

increment( a );

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2419

Page 20: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

An example of abstraction

A safer array

// Interface

class Array {

size_t _size;

double *_elem;

public:

Array( size_t theSize );

~Array();

inline size_t size() const { return _size; };

double& operator[]( size_t i );

};

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2420

Page 21: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

C++ Resources

The C++ Programming Language, 4th ed. by Bjarne StroustropEffective C++, More Effective C++, and Effective STL by Scott Meyershttp://cplusplus.com

http://cppreference.com

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2421

Page 22: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

C++

Examples

Time for some examples...

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2422

Page 23: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

Wrap-up

Second Assignment

Second assignment is posted before midnight: three problems total 1000points

linklist (300, C)

geom (300, C++)

mst (400, C++)

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2423

Page 24: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

Wrap-up

Wrap-up & Monday

Class on Friday

Object-oriented programming in C++

Questions?

Andre Kessler 6.S096 Lecture 4 – Style and Structure / 2424

Page 25: Lecture 4: Style and Structure - MIT OpenCourseWareAndre Kessler 6.S096 Lecture 4 { Style and Structure 17 /24. C++ The Key Di erences Generally, can compile C code with a C++ compiler

MIT OpenCourseWarehttp://ocw.mit.edu

6.S096 Effective Programming in C and C++IAP 2014

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.