6.S096 Lecture 5: C++ Introduction, Classes, and Templates

11 downloads 22915 Views 801KB Size Report
6.s096. Lecture 5. Tuesday, January 22, 13. 1 ... Page 5 ... template class mypair {. T mypair::getmax(){. T a, b; return a > b ? a : b; public: } mypair( T ...
6.s096 Lecture 5

1

Tuesday, January 22, 13

Today

• C++ (!) • Compiling • Memory management Classes

• • Templates

2

Tuesday, January 22, 13

C++

3

Tuesday, January 22, 13

C++ • • • • •

Bjarne Stroustrup 1983 Object-oriented (but later than Simula, Smalltalk) Like C, but introduces real objects and classes (plus loads of other features (kitchen sink?))

4

Tuesday, January 22, 13

g++ (C++ compiler) •

Very similar to gcc.

#include

g++ -­‐o test test.cpp ./test => hi from C++

int main(){

printf("hi from C++\n");

}

5

Tuesday, January 22, 13

Wait... was that really C++? • • •

Yes. C++ is pretty close to being a superset of C. We know C, thus we’ll build from that knowledge to learn C++.

6

Tuesday, January 22, 13

new memory management syntax

• •

The new operator allocates space on the heap. new and delete take the place of malloc and free. int * numArray = new int[100];

delete numArray;

struct foo * bar = new struct foo; // delete later

7

Tuesday, January 22, 13

Classes

8

Tuesday, January 22, 13

Why classes? • • •

Modularity Objects (data + behavior) Lets programmers (you) define behavior for your own data

9

Tuesday, January 22, 13

Basic Class Example Rectangle::Rectangle(int w, int h){

// constructor definition

width = new int;

height = new int;

*width = w;

*height = h;

}

#include class Rectangle { int * width; int * height;

int main(){

Rectangle box(5, 7);

box.printMe();

}

public: Rectangle(int, int); // constructor ~Rectangle(); // destructor void printMe(){ // 'method' / member function

printf("Dimensions: %d by %d.\n", *width, *height);

}

};

10

Tuesday, January 22, 13

Constructors and Destructors •

This destructor should have fit on the last slide...:



Since we explicitly allocated something with new, we must also explicitly de-allocate it.



Rectangle itself is automatically deallocated when it goes out of scope. 11

Tuesday, January 22, 13

Rectangle::~Rectangle(){ delete width; delete height; }

Default constructors Rectangle::Rectangle(){ width = new int; height = new int; *width = 5; *height = 5; }

// no arguments needed! Rectangle box;

12

Tuesday, January 22, 13

Templates • • • • •

Syntax for making code more flexible. Similar in spirit to Java’s generics. Applied at compile-time, like C macros (the preprocessor). Can be applied to classes, functions. Trivia: language of templates is Turing complete.

13

Tuesday, January 22, 13

Function Template Example template typeParam max(typeParam a, typeParam b){ return (a > b ? a : b); } int main(){ int a = 3, b = 7; double c = 5.5, d = 1.5; printf("%d\n", max(a, b)); // 7 printf("%f\n", max(c, d)); // 5.5 } 14

Tuesday, January 22, 13

Class Template Example template template

class mypair { T mypair::getmax(){

T a, b; return a > b ? a : b;

public: }

mypair(T first, T second){

a = first; int main(){

b = second; mypair myints(100, 75);

} printf("%d\n",

T getmax(); myints.getmax()); // 100

};

15

Tuesday, January 22, 13



MIT OpenCourseWare http://ocw.mit.edu

6.S096 Introduction to C and C++ IAP 2013

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