Anda di halaman 1dari 316

www.onlineeducation.bharatsevaksamaj.

net

www.bssskillmission.in

N
I
.
E

C++
(&
C)
Grab
Bag
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
Final Project:
Due in 2 Days
E
V
S
Complete
Something
S
.B
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

Parent
destructors
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

struct Buffer {
Buffer(int s) { buf = new char[s]; }
~Buffer() { delete [] buf; }
char *buf;
};
struct FBuffer : Ipublic
Buffer {
N
.
E : Buffer(s) {
FBuffer(int Vs)
S
f = fopen("file",
"w");
S
B
.
}
W
W
~FBuffer()
{
fclose(f);
}
W
void write() { fwrite(buf, 1, 40, f); }
FILE *f;
};
4

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

struct FBuffer

: public Buffer {
FBuffer(int s);
~FBuffer();
void write();
FILE *f;
};

struct Buffer {
Buffer(int s);
~Buffer();
char *buf;
};

N
I
.
E

V
S

S
.B

BufferW*buf = new Buffer(128);

W
delete buf;

//

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

struct FBuffer

: public Buffer {

FBuffer(int s);
~FBuffer();
void write();
FILE *f;
};

struct Buffer {
Buffer(int s);
~Buffer();
char *buf;
};

N
I
.
E

V
S

S
.B

W *fbuf = new FBuffer(128);

FBuffer
W
delete fbuf;

//

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Construction

Buer
N
I
(buf)
.
E

V
S

S
Uninitialized
.B
W
W
W

Uninitialized

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

Buer
(buf)

FBuer
(f)

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Destruction

Buer
(buf)

Buer
N
I
(buf)
.
E

V
S

S
.B

Uninitialized

W
FBuer
(f)

Uninitialized

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

struct FBuffer

: public Buffer {

FBuffer(int s);
~FBuffer();
void write();
FILE *f;
};

struct Buffer {
Buffer(int s);
~Buffer();
char *buf;
};

N
I
.
E

V
S

S
.B

BufferW*fbuf = new FBuffer(128);

W
delete fbuf;

// only ~Buffer is called

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

10

www.bssskillmission.in

struct FBuffer

struct Buffer {
: public Buffer {
Buffer();
FBuffer();
virtual ~Buffer(); virtual ~FBuffer();
char *buf;
void write();
};
FILE *f;
IN };

.
E

V
S

S
.B

BufferW*fbuf = new FBuffer;

W
delete fbuf;

//

10

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

11

www.bssskillmission.in

N
I
.
E

C++
Casts
V
S

S
.B

11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

12

www.bssskillmission.in

// C cast

char *buf = (char *)malloc(128);

// C-style cast

float b = 98.6;

int a = int(b);

N
I
.
E

V
S

S
.B

12

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

13

www.bssskillmission.in

// C-style cat casts

class Cat { };

class Tiger : public Cat { };

class Persian : public Cat { };

Cat *c = new Persian;


Tiger *t = (TigerN*)c; // whoops!

I
.
E

V
S

S
.B

Cat

Tiger

Persian
13

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

14

www.bssskillmission.in

// valid up-cast

Tiger *t = new Tiger;


Cat *c1 = (Cat *)t;

Cat *c2 = static_cast<Cat


*>(t);

N
I
.
Cat *c3 = dynamic_cast<Cat
*>(t);

V
S

S
.B

14

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

15

www.bssskillmission.in

// almost valid down-cast

Cat *c = new Tiger;

Tiger *t1 = (Tiger *)c;

Tiger *t2 = static_cast<Tiger


*>(c);

N
I
.
Tiger *t3 = dynamic_cast<Tiger
*>(c);

V
S

S
.B

// compile
error

15

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

16

www.bssskillmission.in

// valid down-cast

class Cat { virtual void purr() { } };

class Tiger : public Cat { };

class Persian : public Cat { };

N
I
.
Cat *c = new Tiger;

E
V
S
S
B
.
Tiger *t1
=
(Tiger
*)c;

W
Tiger W
*t2 = static_cast<Tiger *>(c);

W
Tiger *t3 = dynamic_cast<Tiger *>(c);

16

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

17

www.bssskillmission.in

// invalid down-cast
Cat *c = new Persian;

Tiger *t1 = (Tiger *)c;

Tiger *t2 = static_cast<Tiger


*>(c);

N
I
.
Tiger *t3 = dynamic_cast<Tiger
*>(c);

V
S

S
.B

// t1 & W
t2 are invalid pointers

// t3 W
is NULL

17

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

18

www.bssskillmission.in

N
I
.
E

References
V
S

S
.B

18

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

19

www.bssskillmission.in

void swap(int *a, int *b) {


int tmp = *a;
*a = *b;
*b = tmp;N
I
.
}
E

V
S

S
.B

int main() {
W
W int x = 2, y = 3;
W swap(&x, &y);
}

19

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

20

www.bssskillmission.in

void swap(int &a, int &b) {


int tmp = a;
a = b;
b = tmp; N
I
.
}
E

V
S

S
.B

int main() {
W
W int x = 2, y = 3;
W swap(x, y);
}

20

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

21

www.bssskillmission.in

N
I
.
E

Hello,
World!
V
S

S
.B

21

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

22

www.bssskillmission.in

#include <iostream>

N
int main() {
I
.
E
std::cout
<< "Hello, World!"
V
S
S
<<
std::endl;
B
.
W
return
0;
W
W}

22

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

23

www.bssskillmission.in

#include <iostream>

N
int main() {
I
.
E
std::cout
<< "Hello, World!"
V
S
S
<<
std::endl;
B
.
W
return
0;
W
W}

23

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

24

www.bssskillmission.in

N
I
.
E

Namespaces
V
S

S
.B

24

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

25

www.bssskillmission.in

SNDFILE *open(const char *);


count_t seek(SNDFILE *, count_t);
int error(SNDFILE *);

N
I
.
E

V
S

S
.B

25

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

26

www.bssskillmission.in

SNDFILE *sf_open(const char *);


count_t sf_seek(SNDFILE *, count_t);
int sf_error(SNDFILE *);

N
I
.
E

V
S

S
.B

26

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

27

www.bssskillmission.in

namespace sf {
SNDFILE *open(const char *);
count_t seek(SNDFILE *, count_t);
int error(SNDFILE *);
}

N
I
.
E

V
S

S
.B

27

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

28

www.bssskillmission.in

#include <iostream>

N
int main() {
I
.
E
std::cout
<< "Hello, World!"
V
S
S
<<
std::endl;
B
.
W
return
0;
W
W}

28

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

29

www.bssskillmission.in

std::cout << "Hello, World!"


<< std::endl;
using namespace std;
N
cout << "Hello,
World!"
I
.
E
<< Vendl;

S
S

.B

W std::cout;
using
W
Wusing std::endl;
cout << "Hello, World!"
<< endl;
29

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

30

www.bssskillmission.in

g++ -E hello.cpp

iostream
namespace std {
N
extern
istream cin;
I
.
E
extern
ostream cout;
V
S
S
extern
ostream
cerr;
B
.
W}

30

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

31

www.bssskillmission.in

namespace std {
N
extern
istream cin;
I
.
E
extern
ostream cout;
V
S
S
extern
ostream
cerr;
B
.

class ActionLawsuit {
};

31

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

32

www.bssskillmission.in

N
extern
istream cin;

I
.
E
extern
ostream cout;

V
S
S
extern
ostream
cerr;

B
.

class ActionLawsuit {
};

32

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

33

www.bssskillmission.in

namespace super {
namespace std {
N
extern
istream cin;
I
.
E
extern
ostream cout;
V
S
S
extern
ostream
cerr;
B
.

class ActionLawsuit {
};

super::std::ActionLawsuit;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in


33

www.onlineeducation.bharatsevaksamaj.net

34

www.bssskillmission.in

N
I
.
E

extern

V
S

S
.B

34

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

35

www.bssskillmission.in

iostream
namespace std {
N
extern
istream cin;
I
.
E
extern
ostream cout;
V
S
S
extern
ostream
cerr;
B
.
W}

35

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

36

www.bssskillmission.in

iostream
ostream cout;

main.c

foo.c

N
I
.
E

V
#include <iostream>
S

#include <iostream>

W {
int main()
W
W << "i";
cout
foo();
}

int foo() {
cout << "Phone";
}

S
.B

36

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

37

www.bssskillmission.in

main.c (preprocessed) foo.c (preprocessed)

ostream cout;

ostream cout;

int main() {
cout << "i";
foo();
N
I
.
E
}
V

int foo() {
cout << "Phone";
}

S
S

.B

main.o W

foo.o

cout

cout

main

foo

ld: 1 duplicate symbol for architecture x86_64

37

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

38

www.bssskillmission.in

main.c (preprocessed) foo.c (preprocessed)


extern ostream cout; extern ostream cout;

int main() {
cout << "i";
foo();
N
I
.
E
}
V

int foo() {
cout << "Phone";
}

S
S

.B

main.o W

foo.o

main

foo

<standard library>
cout
38

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

39

www.bssskillmission.in

#include <iostream>

N
int main() {
I
.
E
std::cout
<< "Hello, World!"
V
S
S
<<
std::endl;
B
.
W
return
0;
W
W}

39

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

40

www.bssskillmission.in

#include <iostream>

N
int main() {
I
.
E
std::cout
<< "Hello, World!"
V
S
S
<<
std::endl;
B
.
W a = 2 << 1;
int
Wreturn 0;
W
}

40

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

41

www.bssskillmission.in

N
I
.
E

Operator
Overloading
V
S

S
.B

41

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

42

www.bssskillmission.in

struct vec2 {

vec2(float x, float y)

: x(x), y(y) { }

float x, y;

};

N
I
.
E

int main() V{
S
S
vec2 .a(1,
0);
B
W b(1, 3);
vec2
W
Wvec2 c = a + b; // compile error
}
vec.cpp: In function int main():

vec.cpp:12: error: no match for operator+ in a + b

42

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

43

www.bssskillmission.in

vec2 vec2::add(const vec2 &o) {


return vec2(x + o.x, y + o.y);
}
int main() {
N 3);
vec2 a(1, 0), .b(1,
I
E
vec2 c = a.add(b);
V
S
S
}
.B

43

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

44

www.bssskillmission.in

vec2 vec2::operator +(const vec2 &o) {


return vec2(x + o.x, y + o.y);
}
int main() {
N 3);
vec2 a(1, 0), .b(1,
I
E
vec2 c = a V
+ b;
S
vec2 d =.BS
a.operator+(b);
W
}

44

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

45

www.bssskillmission.in

vec2 operator +(vec2 &v, const vec2 &o) {


return vec2(x + o.x, y + o.y);
}
int main() {
N
vec2 a(1, 0), b(1,
3);
I
.
E
vec2 c = a +Vb;
S
S
}
.B

45

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

a
a
a
a
a
a
a
a
a
a

46

www.bssskillmission.in

+ b
a != b
- b
a && b
* b
a || b
/ b
a & b
% b
a | b
< b
a ^ Nb
I
.
<= b
aE<< b
V
S
== b
a >> b
S
B
.
>= b
a,
b
W
>W
b
a[b]

vec2 operator+(const vec2 &o);

46

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

47

www.bssskillmission.in

+a
-a
++a
a++
--a
a-N
I
.
!a
E
V
S
~a
S
B
.
*a
W
&a W

vec2 operator+();

47

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

a
a
a
a
a
a

48

www.bssskillmission.in

= b
+= b
-= b
*= b
/= b
%= b

a
a
a
a
a
a

&= b
|= b
^= b
<<= b
>>= b
= N(b += c)

I
.
E

V
S

S
.B

vec2 &vec2::operator+=(const vec2 &o)


W
{ W
Wx += o.x;
(this is a pointer
y += o.y;
to the object)
return *this;
}
48

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

49

www.bssskillmission.in

N
I
.
E

Streams
V
S

S
.B

49

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

50

www.bssskillmission.in

struct Foo {
char *str() const {
return "Foo!";
}
};

N
ostream &
I
.
E
operator<<(ostream
&os, const Foo &f) {
V
S
S
return os
<<
f.str();
B
.
W
}
W

int main() {
Foo f;
std::cout << f << std::endl;
}
50

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

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.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

51

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

6.S096: Introduction to C/C++


Frank Li, Tom Lieber, Kyle Murray

N
I
.
E

Lecture
8:

V
S
S
Last
Lecture

B
.
Helter
W Skelter Fun!

January 31, 2012

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

52

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today

Standard Template Library (STL)

Crazay Const
Exceptions
Function pointers

N
I
.
E

V
S

S
.B

Brief intro to C++11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

53

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Standard Template Library (STL)

Crazay Const
Exceptions
Function pointers

N
I
.
E

V
S

S
.B

Brief intro to C++11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

54

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Standard Template Library (STL)

Included w/ compiler
Contains containers/data structures, iterators,
and algorithms
http://www.cplusplus.com/reference

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

55

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Vectors

Equivalent to array lists in other languages


(dynamic size)
#include <vector>

std::vector<int> int_list;

int_list.push_back(1);

N
I
.
E

V
S
int tmp = int_list[0]; //
tmp = 1

S
B
.
int_list.pop_back(); // int_list now empty

W
W
W
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

56

www.onlineeducation.bharatsevaksamaj.net

57

www.bssskillmission.in

Map

00

John Smith

Like a dictionary in Python (tree


implementation).

01

521-8976

02

521-1234

03

Lisa Smith

13

Sandra Dee

14

521-9655

15

#include <map>
Keys

Hash
Function

Buckets

Image by MIT OpenCourseWare.

std::map<char, int> letter_to_int;

letter_to_int['a'] = 1;

N
I
.
E

V
letter_to_int['b'] = 2;
S
S
B
.
int pos = letter_to_int['a'] // pos = 1;

W
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Others Containers

Other useful containers:


<array>: array w/ functions

<list>: doubly linked list

<set>: stores only unique elements, tree


implementation

<unordered_map>: actual hash table

<unordered_set>: stores only unique

elements, hash table implementation

N
I
.
E

V
S

S
.B

Look online for more!

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

58

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Iterators

Object that points to elements in a data


structure, and can iterate through. Like a
pointer.
Vector iterator: std::vector<int>::iterator it;

N
I
.
E

Access element at iterator: *it;

V
S

S
.B

Can do add/subtract to iterators: it++, it--

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

59

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Vector Iterators

#include <vector>

#include <iostream>

...

std::vector<int> vec;

for (int i=1; i<=5; i++)

vec.push_back(i);

N
I
.
E

for (std::vector<int>::iterator it = vec.begin();


it != vec.end(); ++it){

S
.B

std::cout << ' ' << *it;

W
1 2 3 4 5
//Will print:W
W
}

V
S

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

60

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Cool/Useful Algorithm

#include <algorithm>

Sort a vector:

Reverse a vector:

std::reverse(vec.begin(), vec.end());

Min/Max:

std::sort(vec.begin(), vec.end());

N
I
.
E

V
S

std::min(3,1) == 1 ; std::max(3,1) == 3

S
So many more online!
.B
W
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

61

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today

Standard Template Library (STL)

Crazay Const
Exceptions
Function pointers

N
I
.
E

V
S

S
.B

Brief intro to C++11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

62

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Const Madness!

const int x == int const x;

// Const int

const int * c1;

// Pointer to const int

N
I
.
E

V
int const * c2; // Same
as c1
S
S
.B
W
int * constW
c3; // Const pointer to variable int
W
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

63

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

More Const Madness!

Const in function parameter


bool cant_touch_this(const myobj & obj);

//cant_touch_this can't modify obj

Const functions are safe for const objects, but can


be called by all objects. Non-const functions can
only be called by non-const objects.

N
I
.
E

V
S
bool catch_change_this()
const {

S
B
.
...

W
}

W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

64

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

When will the madness end!?!?


const int* const

crazay(const int* const & madness) const;

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

65

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Standard Template Library (STL)

Crazay Const
Exceptions
Function pointers

N
I
.
E

V
S

S
.B

Brief intro to C++11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

66

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Exceptions

Exceptions are error objects that are thrown


when things go bad.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

67

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Exceptions

Exception parent object in std::exception

std::runtime_error

std::bad_alloc

std::bad_cast

Can create your own custom exceptions


class MyException : public exception{

N
I
const char * what() const { E.
V
S
return "MyException";
// human-readable
S
.B
}
W
};
W
W
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

68

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Try Catch
try {
// Protected code

throw MyError();

}catch( YourError e1 ){
cout << e1.what() << endl;

N
I
.
E

}catch( MyError e2 ){

cout << e2.what() << endl;

V
S

S
.B

}catch(...) {

// handle all other exceptions


}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

69

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Throwing that Exceptions

Can throw a primitive as an exception


try{
...
throw 20;

}catch(int e) { cout << Error: << e << endl; }

Best way to catch your own exception:


try{

N
I
.
E

V
S

throw MyException();

S
.B

}catch(MyException & e) {

cout << e.what() << endl;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

70

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Functions Throwing

Functions add throw(exceptions-thrown) at end


of declaration.
void ahh(int i) throw() {
//This function assumed to not throw anything

N
I
void blahh(int i) throw(int) { .
E
V
//This function may throw
int if there's an error
S
S
}
.B
W
W
W
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

71

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Standard Template Library (STL)

Crazay Const
Exceptions
Function pointers

N
I
.
E

V
S

S
.B

Brief intro to C++11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

72

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Functions Pointers

void (*foo) (int); // Foo is pointer to void


function that takes 1 int argument.
void *(*foo)(int *, char); //Any guesses?

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

73

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Functions Pointers

void (*foo) (int); // Foo is pointer to void

function that takes 1 int argument.

void *(*foo)(int *, char); //Foo is a pointer to a


function that takes 1 int* argument and 1 char
argument, that returns void*

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

74

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Using Functions Pointers

#include <iostream>
void my_int_func(int x){
std::cout << x << std::endl;
}
int main(){
void (*foo)(int);

N
I
.
(*foo)(2); // Calls my_int_func E
V
foo(2); // Same as above line
S
S
return 0;
B
.
}
W
W
W

foo = &my_int_func; // The ampersand is actually optional

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

75

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Functions Pointers Arguments

#include <iostream>

void call(int x){

std::cout << x << std::endl;


}
void call_me_maybe(int number, void (*call_func)(int)) {
call_func(number);

N
I
.
E

int main(){

void (*foo)(int);

V
S

foo = call;

S
.B

call_me_maybe(911, foo);
return 0;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

76

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Functions Pointers Arguments

#include <iostream>
void call(int x){
std::cout << x << std::endl;
}
void call_me_maybe(int number, void (*call_func)(int)) {
call_func(number);

N
I
.
E

V
S

int main(){

S
.B

call_me_maybe(911, call);
return 0;
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

77

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

For_each
#include <algorithm> // Header file need for for_each()
#include <vector>

#include <iostream>

using namespace std;

void print (int i) { cout << ' ' << i; }

int main(){

N
I
.
E

vector<int> myvector;

myvector.push_back(10);

V
S

myvector.push_back(20);

S
.B

for_each (myvector.begin(), myvector.end(), print);

W
} // Prints W
out 10 20
W
...

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

78

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Standard Template Library (STL)

Crazay Const
Exceptions
Function pointers

N
I
.
E

V
S

S
.B

Brief intro to C++11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

79

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C++11

Latest and greatest standard of C++ w/ fancy


features
Fancy feature 1: Auto typing

N
I
.
E

int x = 1;

auto y = x;

V
S

S
.B

vector<int> vec;

auto itr = vec.begin(); //versus vector<int>::iterator

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

80

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C++11

Latest and greatest standard of C++ w/ fancy


features
Fancy feature 2: Declare typing
int x = 1;

N
I
.
E

decltype(x) y = x; //Makes y the same type as x

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

81

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C++11

Latest and greatest standard of C++ w/ fancy


features
Fancy feature 3: Right angle brackets

Before C++11:

N
I
.
E

vector<vector<int> > vector_of_int_vectors;


C++11:

V
S
vector<vector<int>>
vector_of_int_vectors;

S
.B
W
W
W
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

82

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C++11

Latest and greatest standard of C++ w/ fancy


features
Fancy feature 4: Range for loops
vector<int> vec;

N
I
.
E

vec.push_back( 1 );

vec.push_back( 2 );

V
S

S
.B

for (int& i : vec ) {

i++; // increments the value in the vector


}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

83

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C++11

Latest and greatest standard of C++ w/ fancy features

Fancy feature 5: Lambda functions!!!

Syntax: [] (arg_list) { func_definition }

Ex:

#include <iostream>

using namespace std;

int main(){

auto func = [] (int i) { cout << "Hi " << i << endl; };
func(1); // now call the function
}

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

84

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C++11

Many more fancy features (strongly typed

enums, null pointer constant, etc)!

\(^_^) /

Yay!!!

N
I
.
E

Reference:

V
S

http://www.cprogramming.com/tutorial.html#c++11

S
.B

http://en.wikipedia.org/wiki/C%2B%2B11

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

85

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

It's about that time...

Introduced you to C and C++ syntax, features, and


idoms.

Gave you some experience w/ writing C/C++ code

Much more to learn, best way is to simply code more!

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

86

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

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.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

87

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

C++
Inheritance
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

88

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

Bits
&
Bobs
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

89

www.onlineeducation.bharatsevaksamaj.net

90

www.bssskillmission.in

int x;

int y;

int

int

pt
pt.x
N
pt.y
I
.

struct {
int x;
E
int y;
V
S
S
} pt;
B

int

int

W
union {
int x;
int y;
} pt;

pt
pt.x
pt.y

int

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

int

www.onlineeducation.bharatsevaksamaj.net

91

www.bssskillmission.in

foo

foo.x
N
foo.y
I
.

union {
struct {
int a;
E
char b;

V
S
S
} x;
.B
W
foo.x.a

intWy;
W
} foo;
foo.x.b

int
char

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

int

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

Why
Inheritance?
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

92

www.onlineeducation.bharatsevaksamaj.net

93

www.bssskillmission.in

struct Circle {
int x, y;
int radius;
void draw();
};

struct Square {
int x, y;
int width;
void draw();
};

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

94

www.bssskillmission.in

struct Circle {
int x, y;
int radius;
void draw();
};

struct Square {
int x, y;
int width;
void draw();
};

N
I
.
E

Circle *circles[nc];

V
S
S
Square *squares[ns];

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

95

www.bssskillmission.in

struct Circle {
int x, y;
int radius;
void draw();
};

struct Square {
int x, y;
int width;
void draw();
};

N
I
.
E

Circle *circles[nc];

V
S
S
Square *squares[ns];

.B

W i = 0; i < nc; i++)


for(int
circles[i].draw();
for(int i = 0; i < ns; i++)
squares[i].draw();
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Circle *circles[nc];

Square *squares[ns];

for(int i = 0; i < nc; i++)


circles[i].draw();
for(int i = 0; IiN < ns; i++)
.
E
squares[i].draw();
V

S
S

.B

Wi = 0; i < nc; i++)


for(int
W
W
delete
circles[i];
for(int i = 0; i < ns; i++)
delete squares[i];
for(int i = 0; i < nc; i++)
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

96

www.onlineeducation.bharatsevaksamaj.net

97

www.bssskillmission.in

struct Circle {
int x, y;
int radius;
void draw();
};
?
N

I
.
E

Shape *shapes[ns];
V
S

struct Square {
int x, y;
int width;
void draw();
};
?

S
.B

W
for(int
W i = 0; i < ns; i++)
W
shapes[i].draw();
for(int i = 0; i < ns; i++)
delete shapes[i];
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

Inheritance
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

98

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

99

class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {

private:
N
I
.
int x, y;
E
V
S
int radius;
S
public: .B
W
virtual
W void draw();
}; W
void Circle::draw() {
...
}
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

e
c
i
ct

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
1)ESubclassing
.
V
S
S 2) virtual

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

100

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
1)ESubclassing
.
V
S
S 2) virtual

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

101

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

1) Subclassing
N

I
.
E

V
S

S
.B

inherit
behavior
from
the
parent
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

102

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw();
};

class Circle : public Shape {


public:
N
I
.
int getRadius();
E
V
S
};
S

.B

W
int main()
{
W
W
Circle
circle;
}

circle.draw();

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

103

www.onlineeducation.bharatsevaksamaj.net

Shape

Circle : public Shape

void draw()

N
I
.

E
V

S
S

.B

104

www.bssskillmission.in

void draw()

int getRadius()

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

1) Subclassing
N

I
.
E

V
S

S
.B

inherit
elds
from
the
parent
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

105

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
int x, y;
};

class Circle : public Shape {


public:
N
I
int radius; E.
V
S
};
S

.B

W
int main()
{
W
W
Circle
circle;
}

circle.x = 5;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

106

www.onlineeducation.bharatsevaksamaj.net

Shape

Circle : public Shape

int x

N
I
.

E
V

S
S

int .y

107

www.bssskillmission.in

int x
int y
int radius

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Shape

int x

E
V

S
S

.B

int y
N
.I
int radius

is-a or has-a?
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

108

Circle

www.onlineeducation.bharatsevaksamaj.net

109

www.bssskillmission.in

class Circle
: public Shape {
N
public:
I
.
int radius; VE
S
S
};
.B

class Circle {
public:
Shape shape;
int radius;
};

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

110

www.bssskillmission.in

class Circle
: public Shape {
N
public:
I
.
int radius; VE
S
S
};
.B

class Circle {
public:
Shape shape;
int radius;
};

W
circle.x;
W

circle.shape.x;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

1) Subclassing
N

I
.
E

V
S

S
.B

public/protected/private
elds
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

111

www.onlineeducation.bharatsevaksamaj.net

112

www.bssskillmission.in

class Shape {
public:
int x;
private:
int y;
};

only accessible in
Shape class

N
I
.
void Circle::foo()
E
V
S
printf("%d",
x);
S
.B
printf("%d",
y);
W
}
W
W

{
// compile error

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

113

www.bssskillmission.in

class Shape {
public:
int x;
protected:
int y;
};

accessible in Shape class


and in subclasses

N
I
.
void Circle::foo()
{

E
V
S
printf("%d",
x);

S
.B
printf("%d",
y);

W
}
W
W
int main() {
Circle circle;
circle.x = 0; // compile error
}
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

1) Subclassing
N

I
.
E

V
S

S
.B

public/protected/private
inheritance
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

114

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw();
};

class Circle : public Shape {


};
N

I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

115

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw();
};

class Circle : protected Shape {


};
N

I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

116

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw();
};

class Circle : protected Shape {


protected:
N
I
.
int getRadius();
E
V
S
};
S

.B

W
The W
inheritance is protected.

If you can access getRadius(),


you can access draw()
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

117

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw();
};

class Circle : private Shape {


private:
N
I
.
int getRadius();
E
V
S
};
S

.B

W
The W
inheritance is private.

If you can access getRadius(),


you can access draw()
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

118

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
private
inheritance:
E
V
S
is-a
or
has-a
S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

119

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

1) Subclassing
N

I
.
E

V
S

S
.B

multiple
inheritance
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

120

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Color {

public: virtual void print();

};

class Mood {

public: virtual void print();

};

IN

.
E

V
S

class Blue B:S public Color, public Mood {


.
public: W
W void print() {

virtual
W
this->Color::print();

this->Mood::print();

}
};
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

121

www.onlineeducation.bharatsevaksamaj.net

122

www.bssskillmission.in

1) Subclassing
N

I
.
E

V
S

S
.B

slicing

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

struct Cow {
void speak() {
printf("Moo.\n");
}
};

N
struct Werecow
: public Cow {
I
.
E
bool transformed;
V
S
S
void
speak()
{
B
.
Wif (transformed)
W
printf("Aaooooh!\n");
W
else
printf("Moo.\n");
}
};
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

123

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Werecow wcow;
wcow.transformed = true;
Cow cows[2];
cows[0] = Cow();
cows[1] = wcow;

N
I
.
E

for (int
i = 0; i < 2; i++)
V
S
S
cows[i].speak();
.B
W
wcow.speak();

// Output:
Moo.
//
//
Moo.
//
Aaooooh!
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

124

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

void poke(Cow cow) {


cow.speak();
}
Cow judy;

Werecow bev;

bev.transformed
=
true;

I
.
E

V
S
poke(judy);

S
.B
poke(bev);

W
bev.speak();

// Output:

//
Moo.

//
Moo.

Aaooooh!

//
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

125

www.onlineeducation.bharatsevaksamaj.net

Cow

N
I
.
E

V
S

S
.B

126

www.bssskillmission.in

Werecow
bool transformed

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
Use
pointers
.
E
V
S

Use
virtual

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

127

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
1)ESubclassing
.
V
S
S 2) virtual

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

128

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw() { printf("shape\n"); }
};
class Circle : public Shape {
public:
N
I
void draw() {E.printf("circle\n"); }
V
S
};
S

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

129

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw() { printf("shape\n"); }
};
class Circle : public Shape {
public:
N
I
void draw() {E.printf("circle\n"); }
V
S
};
S

.B

W
CircleW*circle = new Circle;

W
circle->draw();
// "circle"

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

130

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw() { printf("shape\n"); }
};
class Circle : public Shape {
public:
N
I
void draw() {E.printf("circle\n"); }
V
S
};
S

.B

W
Shape W
*shape =
W
shape->draw();

new Circle;

// "shape"

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

131

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Shape {
public:
void draw() { printf("shape\n"); }
};
class Circle : public Shape {
public:
N
I
void draw() {E.printf("circle\n"); }
V
S
};
S

.B

W
Shape W
*shape =
W
shape->draw();

new Circle;

// "shape"

draw() is non-virtual,
so its compiled like a C call
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

132

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Non-virtual functions are

determined at compile-time

class Cat {
public:
void yawn(int
duration);
N
I
.
};
E

V
S

S
.B

CatWcat, *pcat = new SuperCat;

W
cat.yawn(4);

Wpcat->yawn(4);

Both use Cat::yawn

because both have type Cat

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

133

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

134

Virtual functions are


determined at run-time

class Cat {
public:
virtual void Nyawn(int duration);
I
.
};
E

V
S

S
.B

Cat cat,
*pcat
=
new
SuperCat;

W
W
cat.yawn(4);

W
pcat->yawn(4);

Use Cat::yawn and SuperCat::yawn


(pcats type is checked every time its called)
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
non-virtual:
compile-time
.
E
V
S

S
virtual:
run-time
B
.

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

135

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

2) N
virtual

I
.
E

V
S

S
.B

pure
virtual
methods
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

136

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

137

class Shape {

public:

virtual void draw() = 0;


};

N
I
int main()
{

.
E
V
Shape
shape;

S
S
}
.B
W

pure.cpp: In function int main():

pure.cpp:7: error: cannot declare variable shape to be of abstract type Shape

pure.cpp:1: note:
because the following virtual functions are pure within Shape:

pure.cpp:3: note: ! virtual void Shape::draw()

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Drawable {
public:
virtual void draw() = 0;
};
class Fish : public Drawable {
public:
N
I
.
virtual void
draw();
E
V
S
};
S

.B

W
intWmain()
WDrawable

{
*drawables[3];
drawables[0] = new Fish;
drawables[1] = new Salami;
drawables[2] = new JackSparrow;
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

138

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

2) N
virtual

I
.
E

V
S

S
.B

destructors

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

139

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Make virtual destructors

class Fish {
public:
Fish() {
gills[0] = new Gill;
N
I
gills[1]
=
new
Gill;
.
E
} SV
S
B
~Fish() {
.virtual
W delete gills[0];
W
W
delete gills[1];
}
private:
Gill *gills[2];
};
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

140

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

2) N
virtual

I
.
E

V
S

S
.B

virtual inheritance

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

141

www.onlineeducation.bharatsevaksamaj.net

142

www.bssskillmission.in

class Goofball {
int x;
};
class Stupid : public Goofball { };
class Dumb : public Goofball { };

N
I
.
E

class Oops : public


Stupid, public Dumb {
V
S
S
};
B

Oops

Stupid::x

Dumb::x

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

class Goofball {
int x;
};
class Stupid : public Goofball { };
class Dumb : public Goofball { };

N
I
.
E

class Oops : public


Stupid, public Dumb {
V
S
S
int fail();
B
.
};
W

int Oops::fail() {
Stupid::x = 1; Dumb::x = 2;
return Stupid::x + Dumb::x; // 3
}
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

143

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

144

class Goofball {
int x;
};
class Stupid : virtual public Goofball { };
class Dumb : virtual public Goofball { };

N
I
.
E

class Oops : public


Stupid, public Dumb {
V
S
S
int fail();
B
.
};
W

int Oops::fail() {
Stupid::x = 1; Dumb::x = 2;
return Stupid::x + Dumb::x; // 4
}
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

Conclusion
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

145

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

146

class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {

private:
N
I
.
int x, y;
E
V
S
int radius;
S
public: .B
W
virtual
W void draw();
}; W
void Circle::draw() {
...
}
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

e
c
i
ct

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

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.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

147

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

6.S096: Introduction to C/C++


Frank Li, Tom Lieber, Kyle Murray

N
I
.
E

V
S

S
Lecture 3: C.B
Memory Management

WJanuary 15, 2013

W
W
1

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

148

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Computer Memory

Pointers/Addresses

N
I
.
E

Arrays

V
S

S
.B

Memory Allocation

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

149

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Computer Memory

Pointers/Addresses

N
I
.
E

Arrays

V
S

S
.B

Memory Allocation

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

150

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Heap
Heap is a chunk of memory that users can use to
dynamically allocated memory.
Lasts until freed, or program exits.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

151

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

152

Stack

Stack contains local variables from functions and


related book-keeping data. LIFO structure.
Function variables are pushed onto stack when
called.
Functions variables are popped off stack when

return.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Memory Layout

N
I
.
E

V
S

S
.B

Memory Layout diagram courtesy of bogotobogo.com, and used with permission.

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

153

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Call Stack

Example: DrawSquare called from main()

void DrawSquare(int i){


int start, end, . //other local variables
DrawLine(start, end);
}

N
I
.
E

void DrawLine(int start, int end){

//local variables

V
S

S
.B

...

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

154

www.onlineeducation.bharatsevaksamaj.net

155

www.bssskillmission.in

Call Stack

Lower address

Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}

N
I
.
E

void DrawLine(int start,


int end){
//local variables

S
.B

...
}

V
S

Top of Stack

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

156

www.bssskillmission.in

Call Stack

Lower address

Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}

void DrawLine(int start,

int end){

//local variables

S
.B

...

V
S

Top of Stack

int i (DrawSquare arg)

N
I
.
E

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

157

www.bssskillmission.in

Call Stack

Lower address

Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}

N
I
.
E

void DrawLine(int start,


int end){
//local variables

S
.B

...
}

V
S

main() book-keeping

int i (DrawSquare arg)

Top of Stack

10

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

158

www.bssskillmission.in

Call Stack

Lower address

Example:
void DrawSquare(int i){
int start, end, ...

DrawLine(start, end);
Top of Stack

N
I
.
E

local variables (start, end)

void DrawLine(int start,


int end){
//local variables

S
.B

...
}

V
S

DrawSquare
stack frame

int i (DrawSquare arg)

main() book-keeping

11

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

159

www.bssskillmission.in

Call Stack

Lower address

Example:
void DrawSquare(int i){

int start, end, ...

Top of Stack

DrawLine(start, end);

start, end (DrawLine args)

N
I
.
E

local variables (start, end)

void DrawLine(int start,


int end){
//local variables

S
.B

...
}

V
S

DrawSquare
stack frame

int i (DrawSquare arg)

main() book-keeping

12

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

160

www.bssskillmission.in

Call Stack

Lower address

Example:

void DrawSquare(int i){

Top of Stack

int start, end, ...

DrawSquare book-keeping

DrawLine(start, end);

start, end (DrawLine args)

N
I
.
E

local variables (start, end)

void DrawLine(int start,


int end){
//local variables

S
.B

...
}

V
S

DrawSquare
stack frame

int i (DrawSquare arg)

main() book-keeping

13

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

161

www.bssskillmission.in

Call Stack

Lower address
Top of Stack

Example:

DrawLine local vars

void DrawSquare(int i){

DrawLine

DrawSquare book-keeping
stack

frame

int start, end, ...

DrawLine(start, end);

start, end (DrawLine args)

void DrawLine(int start,

int end){
//local variables

DrawSquare
stack frame

S
.B

...

V
S

main() book-keeping

int i (DrawSquare arg)

N
I
.
E

local variables (start, end)

14

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

162

www.bssskillmission.in

Call Stack

Lower address
Top of Stack

Example: DrawLine returns

DrawLine local vars

void DrawSquare(int i){

DrawLine
DrawSquare book-keeping
stack
frame

int start, end, ...

DrawLine(start, end);

start, end (DrawLine args)

void DrawLine(int start,

int end){
//local variables

DrawSquare
stack frame

S
.B

...
}

V
S

main() book-keeping

int i (DrawSquare arg)

N
I
.
E

local variables (start, end)

15

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

163

www.bssskillmission.in

Call Stack

Lower address

Example: DrawLine returns

void DrawSquare(int i){


int start, end, ...
DrawLine(start, end);
Top of Stack

N
I
.
E

local variables (start, end)

void DrawLine(int start,


int end){
//local variables

S
.B

...
}

V
S

DrawSquare
stack frame

int i (DrawSquare arg)

main() book-keeping

16

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

164

www.bssskillmission.in

Call Stack

Lower address

Example: DrawSquare returns

void DrawSquare(int i){


int start, end, ...
DrawLine(start, end);
}

Top of Stack

N
I
.
E

void DrawLine(int start,


int end){
//local variables

local variables (start, end)

V
S

DrawSquare
stack frame

S
.B

...

int i (DrawSquare arg)

main() book-keeping

17

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

165

www.bssskillmission.in

Call Stack

Lower address

Example: DrawSquare returns


void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}

N
I
.
E

void DrawLine(int start,

int end){

//local variables

V
S

S
.B

...

Top of Stack

18

Higher address

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Computer Memory
Pointers/Addresses

N
I
.
E

Arrays

V
S

S
Memory Allocation
.B
W
W
W

19

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

166

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Pointers and Addresses

N
I
.
E

V
S

S
.B

Courtesy of xkcd at http://xkcd.com/138/, available under a CC by-nc license


20

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

167

www.onlineeducation.bharatsevaksamaj.net

168

www.bssskillmission.in

Addresses

Each variable represents an address in memory


and a value.
Address: &variable = address of variable

N
I
.
E

&X = SSVAddress of X
.B

Image by MIT OpenCourseWare.


21

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Pointers

A pointer is a variable that points to the block


of memory that a variable represent.
Declaration: data_type *pointer_name;
Example:
char x = 'a';

N
I
.
E

char *ptr = &x; // ptr points to a char x

V
S

S
.B

22

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

169

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Pointers

A pointer is a variable that points to the block


of memory that a variable represent.
Declaration: data_type *pointer_name;
Example:
char x = 'a';

N
I
.
E

char *ptr = &x; // ptr points to a char x


Pointers are integer variables themselves, so can
have pointer to pointers: char **ptr;

V
S

S
.B

23

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

170

www.onlineeducation.bharatsevaksamaj.net

171

www.bssskillmission.in

Data type sizes


Name
char

Description
Character or small
integer.

Size*

Range*
signed: -128 to 127
unsigned: 0 to 255

1byte

short int (short) Short Integer.

2bytes

signed: -32768 to 32767


unsigned: 0 to 65535

int

Integer.

4bytes

signed: -2147483648 to 2147483647


unsigned: 0 to 4294967295

long int (long)

Long integer.

4bytes

signed: -2147483648 to 2147483647


unsigned: 0 to 4294967295

bool

Boolean value. It can


take one of two values: 1byte
true or false.

float

Floating point number. 4bytes

+/- 3.4e +/- 38 (~7 digits)

double

Double precision
8bytes
floating point number.

+/- 1.7e +/- 308 (~15 digits)

long double

N
I
.
E

V
S

S
.B

Long double precision


floating point number.

true or false

8bytes
+/1.7e +/- 308 (~15
www.bsscommunitycollege.in www.bssnewgeneration.in
www.bsslifeskillscollege.in
24

digits)

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

172

Dereferencing = Using Addresses


Also uses * symbol with a pointer. Confusing? I know!!!
Given pointer ptr, to get value at that address, do: *ptr
int x = 5;

int *ptr = &x;

*ptr = 6; // Access x via ptr, and changes it to 6

N
I
.
E

printf(%d, x); // Will print 6 now

V
S
Can use void pointers, just cannot
dereference without casting

S
.B
W
W
W
25

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

173

www.bssskillmission.in

Pointer Arithmetic

Can do math on pointers

Ex: char* ptr

ptr
a

N
I
.
E

V
S

S
.B

26

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

174

www.bssskillmission.in

Pointer Arithmetic

Can do math on pointers

Ex: char* ptr

ptr + 1

N
I
.
E

V
S

S
.B

27

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

175

www.bssskillmission.in

Pointer Arithmetic

Can do math on pointers

Ex: char* ptr

ptr + 1

N
I
.
E

V
S

S
.B

ptr+i has value: ptr + i * sizeof(data_type of ptr)

28

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Pointer Arithmetic
Can do math on pointers
p1 = p2: sets p1 to the same address as p2
Addition/subtraction:
p1 + c , p1 - c
Increment/decrement:

N
I
.
E

p1++, p1--

V
S

S
.B

29

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

176

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Why use pointers? They so confuzin

Pass-by-reference rather than value.


void sample_func( char* str_input);
Manipulate memory effectively.

N
I
.
E
Useful for arrays (next topic).
V
S
S
.B
W
W
W
30

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

177

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Computer Memory

Pointers/Addresses

N
I
.
E

Arrays

V
S

S
Memory Allocation

.B
W
W
W

31

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

178

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C Arrays (Statically Allocated)

Arrays are really chunks of memory!


Declaration:
Data_type array_name[num_elements];

N
I
.
E

Declare array size, cannot change.

V
S

S
.B

32

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

179

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C Arrays (Statically Allocated)

Can initialize like:


int data[] = {0, 1, 2}; //Compiler figures out size
int data[3] = {0, 1, 2};
int data[3] = {1}; // data[0] = 1, rest are set to 0

int data[3]; //Here, values in data are still junk

N
I
.
E

data[0] = 0;

V
S

data[1] = 1;

S
.B

data[2] = 2;

33

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

180

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Array and Pointers

Array variables are pointers to the array start!

char *ptr;

char str[10];

ptr = str; //ptr now points to array start

ptr = &str[0]; //Same as above line

N
I
.
E

V
S

Array indexing is same as dereferencing after


pointer addition.

S
.B

str[1] = a is same as *(str+1) = a

34

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

181

www.onlineeducation.bharatsevaksamaj.net

182

www.bssskillmission.in

C-Style Strings
No string data type in C. Instead, a string is

interpreted as a null-terminated char array.

Null-terminated = last char is null char \0, not


explicitly written
char str[] = abc;
a

N
I
.
E

\0

V
S

String literals use S. Compiler converts literals


to char array.

.B

35

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

183

www.bssskillmission.in

C-Style Strings

Char array can be larger than contained string


char str[5] = abc;
a

\0

Special chars start with \:


\n, \t, \b, \r: newline, tab, backspace, carriage
return

N
I
.
E

V
S

\\, \, \: backslash, apostrophe, quotation mark

S
.B

36

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

String functionalities
#include <string.h>
char pointer arguments: char str1[14]
char* strcpy(char* dest, const char* source);

strcpy(str1, hakuna );

N
I
.
strcat(str1, matata); //str1 now
has hakuna matata
E
V
S
S
B
.
More in documentation
W
W
W
char* strcat(char* dest, const char* source);

37

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

184

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Today
Computer Memory
Pointers/Addresses

N
I
.
E

Arrays and Strings

V
S

S
Memory Allocation

.B
W
W
W

38

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

185

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

186

Dynamic Allocation
#include <stdlib.h>
sizeof (a C language keyword) returns number
of bytes of a data type.
malloc/realloc finds a specified amount of free
memory and returns a void pointer to it.
char * str = (char *) malloc( 3 * sizeof(char) );

N
I
.
E

strcpy(str, hi);

V
str = (char *) realloc( S
str , 6 * sizeof(char) );

S
.B
strcpy(str, hello);

W
W
W
39

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Dynamic Deallocation
#include <stdlib.h>
free declares the memory pointed to by a

pointer variable as free for future use:

char * str = (char *) malloc( 3 * sizeof(char) );

N
I
.
E

strcpy(str, hi);

... use str ...

free(str);

V
S

S
.B

40

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

187

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Dynamically Allocated Arrays


Allows you to avoid declaring array size at

declaration.

Use malloc to allocate memory for array when


needed:
int *dynamic_array;

N
I
.
dynamic_array = malloc( sizeof(
int ) * 10 );
E
V
Snow points to an array
dynamic_array[0]=1;S//
.B
W
W
W
41

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

188

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Summary
Memory has stack and heap.
Pointers and addresses access memory.
Arrays are really chunks of memory. Strings are
null-terminated char arrays.

N
I
.
C allows user memory allocation.
Use malloc,

E
V
realloc and free.

S
S
.B
W
W
W
42

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

189

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

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.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

190

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

sort.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Sort an array A using insertion sort.
// Notice it is passed by reference.
void sort(int *A, int array_size) {
int cur_elem, insert_index;
for (int cur_index = 1; cur_index < array_size; cur_index++) {
cur_elem = *(A+cur_index);
insert_index = cur_index - 1;
// For each element in A, search for where it belong in the subarray
preceeding it's current location
while (insert_index >= 0 && cur_elem < *(A+insert_index)) {
*(A+insert_index+1) = *(A+insert_index);
insert_index -= 1;
}
*(A+insert_index+1) = cur_elem;
}
}

N
I
.
E

int main() {
// Allows us to generate random numbers
srand(time(NULL));
// Read a user input integer and store it in n
int n;
printf("Enter an integer n: ");
scanf("%d", &n);

V
S

S
.B

// An array named array. Change this to a dynamic array using malloc.


int *array = malloc(n * sizeof(int));

// Assign each element in the array a random number between 0 and 31,999
for (int i=0; i<n; i++) {
*(array+i) = rand() % 32000; // assigns random numbers
}

// Prints out the elements of the unsorted array


printf("The unsorted array is: ");
for (int x = 0; x < n; x++) {
printf("%d ", *(array+x));
}
printf("\n");
// Calls the sort function to sort the array
sort(array, n);
// Print out the elements of the now (supposedly) sorted array.
printf("The sorted array is: ");
for (int x = 0; x < n; x++) {
printf("%d ", *(array+x));
}
printf("\n");
free(array); //Making sure to free what I malloc!

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

191

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

return 0;
}

resize.c
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main() {
// Allows you to generate random number
srand(time(NULL));
// Allows user to specify the original array size, stored in variable n1.
printf("Enter original array size: ");
int n1 = 0;
scanf("%d", &n1);
// Create a new array of n1 ints
int *a1 = malloc(n1 * sizeof(int));

N
I
.
E

for(int i = 0; i < n1; i++) {


// Set each value in a1 to 100
*(a1+i) = 100;

// Print each element out (to make sure things look right)
printf("%d ", *(a1+i));

V
S

S
.B

// User specifies the new array size, stored in variable n2.


printf("\nEnter new array size: ");
int n2 = 0;
scanf("%d", &n2);

// Dynamically change the array to size n2


a1 = realloc(a1, n2* sizeof(int)); // Resize the array

// If the new array is a larger size, set all new members to 0.


// Reason: dont want to use uninitialized variables.

if (n2 > n1) {


for(int j = n1; j<n2; j++) {
*(a1+j)=0;
}
}
for (int i = 0; i < n2; i++) {
//Print each element out (to make sure things look right)
printf("%d ", *(a1+i));
}
printf("\n");
// Done with array now, done with program :D
free(a1);
return 0;
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

192

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

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.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

193

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

194

6.s096
Introduction to C and C++
N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Why?

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

195

www.onlineeducation.bharatsevaksamaj.net

196

www.bssskillmission.in

N
I
.
E

You.Bseek
performance

S
V
S

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

197

www.bssskillmission.in

N
I
.
E

You.Bseek
performance

S
V
S

zero-overhead principle

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

198

www.bssskillmission.in

N
I
You seek
to
interface

.
E
V
S
S
B
.
directly
with
hardware

W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

199

www.bssskillmission.in

N
I
.
E

That
s
kinda
it

S
.B
V
S

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

200

www.bssskillmission.in

N
I
a nice way
to
avoid
writing

.
E
V
S
S
B
.
assembly
language
directly

W
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

C++

N
I
respondsVto
the
demands
of

.
E
S
S
B
.
maintaining
large
C
projects

W
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

201

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

202

C++11

N
I
respondsVto
the
demands
of

.
E
S
S
B
.
maintaining
large
C++
projects

W
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
Maintain power
and
flexibility

.
E
V
S
S
B
.
of
W what came before

W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

203

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

204

Compilation

Pipeline
Today:

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

205

www.bssskillmission.in

Source Code

N
I
.
E

V
S

S
.B

Program

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

206

www.bssskillmission.in

main.c

prog

int a = 1;

@*)!%

N
I
.

./prog

E
V

S
S

.B

gcc -o prog main.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

207

www.bssskillmission.in

main.c

int a = 1;

main2.c .IN
E
V
S
S
int
b
=
2;

B
.

prog

@*)!%

./prog

gcc -o prog main.c main2.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

$ gcc -o prog main.c

N
I
$ ./prog

.
E
V
S
S
Hello,
.B World!

W
W
$ W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

208

www.onlineeducation.bharatsevaksamaj.net

209

www.bssskillmission.in

$ gcc -o prog main.c

N
I
$ ./prog

.
E
V
S
S
Hello,
.B World!

W
W
$ W

E
N
O
D

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

210

www.bssskillmission.in

To debug the
sausage,
one

N
I
.
E
V
S
S
mustWsee
how
it
is
made.

B
.
W

Someone, probably

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

211

www.bssskillmission.in

Main.java

Main.class

int a = 1;

%!(*@

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

n
i
a
M
a
v
ja

www.onlineeducation.bharatsevaksamaj.net

212

www.bssskillmission.in

Main.java

Main.class

int a = 1;

%!(*@

main.py

main.pyc
N
.I

E
V

S
S

a=1

.B

%!(*@

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

n
i
a
M
a
v
ja

m
n
o
h
t
py

y
p
.
ain

www.onlineeducation.bharatsevaksamaj.net

213

www.bssskillmission.in

Main.java

Main.class

int a = 1;

%!(*@

main.py

main.pyc
N
.I

E
V

S
S

a=1

.B

n
i
a
M
a
v
ja

y
p
.
ain

%!(*@

m
n
o
h
t
py

main.c W
int a = 1;

int a = 1;

main.o

prog

%!(*@

@*)!%

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

g
o
r
./p

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

214

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

215

www.onlineeducation.bharatsevaksamaj.net

main.c
int a = 1;

216

www.bssskillmission.in

int a = 1;

main.o

prog

%!(*@

@*)!%

Pre-Process E.IN
V
S

S
.B

Compile
Link

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

./prog

www.onlineeducation.bharatsevaksamaj.net

main.c
int a = 1;

217

www.bssskillmission.in

int a = 1;

main.o

prog

%!(*@

@*)!%

main2.o

Pre-Process E.IN %!(*@

V
S

S
.B

Compile
Link

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

./prog

www.onlineeducation.bharatsevaksamaj.net

218

www.bssskillmission.in

Pre-Process

N
I
.
E

V
S

S
.B

Compile

Link

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

219

www.bssskillmission.in

Pre-Process
N
I
.
E

V
S

S
.B

Compile

Link
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

#include

#define

N
I
.
E

V
S

S
.B

#ifdef

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

220

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

rimshot.txt

ba-dum chh

joke.txt

N
I
.
E

A man walksVinto

S
S
a bar. Ouch!

.B
W "rimshot.txt"

#include

cpp -P joke.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

221

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:

A man walks into

a bar. Ouch!

ba-dum chh

N
I
.
E

V
S

S
.B

cpp -P joke.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

222

www.onlineeducation.bharatsevaksamaj.net

223

www.bssskillmission.in

double.py

#define fosho def

#define kthx return

#define wutz print

These are called


macros

N
I
.
fosho double(x):

E
V
S
kthxBx
* 2

S
.
wutz double(6)

W
W
W
cpp -P double.py

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:

def double(x):

return x * 2

print double(6)

N
I
.
E

V
S

S
.B

cpp -P double.py

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

224

www.onlineeducation.bharatsevaksamaj.net

225

www.bssskillmission.in

output:

def double(x):

return x * 2

print double(6)

cpp -P double.py | python

S
S

.B

12

AWE
S

N
I
.
E

OME

cpp -P double.py

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

beer.txt

#define beer(x) x bottles of \

beer on the wall...

beer(99)

N
I
.
beer(98)

E
V
beer(97)
BSS
.
...

cpp -P beer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

226

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

beer.txt

#define beer(x) x bottles of \

beer on the wall...

beer(99)

N
I
.
beer(98)

E
V
beer(97)
BSS
.
...

cpp -P beer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

227

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:

99 bottles of beer on the wall...

98 bottles of beer on the wall...

97 bottles of beer on the wall...

...

I
.
E

V
S

S
.B

cpp -P beer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

228

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

answer.txt

Whats 7 times 6?

#ifdef REVEAL

42

#endif

I
.
E

V
S

S
.B

cpp -P answer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

229

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:

Whats 7 times 6?

N
I
.
E

V
S

S
.B

cpp -P answer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

230

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:
Whats 7 times 6?

? ??
N
I
.
E

V
S

S
.B

cpp -P answer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

231

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

#define REVEAL

N
I
.
E

V
S

S
.B

cpp -P answer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

232

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

#define REVEAL

or:

N
I
.
E

cpp -P -D REVEAL
answer.txt

V
S
S

.B

cpp -P answer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

233

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:

Whats 7 times 6?

42

N
I
.
E

V
S

S
.B

cpp -P -D REVEAL answer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

234

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

answer.txt

Whats 7 times 6?

#ifndef REVEAL

42

#endif

I
.
E

V
S

S
.B

cpp -P answer.txt

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

235

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

(Fancy)

236

String Substitution

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

237

How is this used in C?

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

hello.c

#include <stdio.h>

int main() {

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

N
I
.
return 0;

E
V
S
}

S
B

gcc -E hello.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

238

www.onlineeducation.bharatsevaksamaj.net

hello.c

www.bssskillmission.in

* angle brackets -> use the system search path

#include <stdio.h>

int main() {

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

N
I
.
return 0;

E
V
S
}

S
B

gcc -E hello.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

239

www.onlineeducation.bharatsevaksamaj.net

hello.c

www.bssskillmission.in

* angle brackets -> use the system search path

#include <stdio.h>

int main() {

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

N
I
.
return 0;

E
V
S
}

S
B

gcc -E hello.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

240

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:

int printf(const char * , ...)

__attribute__((__format__

(__printf__, 1, 2)));

N
I
.
int main() {
E
V
S
printf("Hello,
World!\n");

S
B
.
}

W
W
W

* pretending printf is all thats defined in stdio.h

gcc -E hello.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

241

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

output:

int printf(const char * , ...);

int main() {

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

N
I
.
}

V
S

S
.B

* pretending printf is all thats defined in stdio.h

gcc -E hello.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

242

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

#include is not
N
I
.
E

V
S
import
pickle

S
.B
W
W
W

import java.io.*;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

243

fib.c

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

#define MAX_FIB 20

int fib[MAX_FIB];

int main() {

fib[0] = 0;

N
I
.
fib[1] = 1;

E
V
S = 2; i < MAX_FIB; i++)

for(int Si
B
.
fib[i]
= fib[i-1] + fib[i-2];

W
W
return
0;

W
}

gcc -E fib.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

244

www.onlineeducation.bharatsevaksamaj.net

output:

www.bssskillmission.in

int fib[20];

int main() {

fib[0] = 0;

fib[1] = 1;
IN
.
for(int iV=E 2; i < 20; i++)

S
S
fib[i]
=
fib[i-1]
+
fib[i-2];

B
.
W
}

gcc -E fib.c

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

245

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

debug.c

#include <stdio.h>

int main() {

#ifdef DEBUG
N
I
.
printf("Hello,
World!\n");

E
V
#endif
BSS
.
return
0;

W
W
}
W

gcc -DDEBUG debug.c -o debug

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

246

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

debug.c

#include <stdio.h>

int main() {

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

N
I
.
return 0;

E
V
S
}

S
B

gcc -DDEBUG debug.c -o debug

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

247

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

debug.c

#include <stdio.h>

int main() {

return 0;
N
I
.
}

V
S

S
.B

gcc debug.c -o debug

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

248

www.onlineeducation.bharatsevaksamaj.net

249

www.bssskillmission.in

Pre-Process
N
I
.
E

V
S

S
.B

Compile

Link
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

main.c
int a = 1;

250

www.bssskillmission.in

int a = 1;

main.o

prog

%!(*@

@*)!%

N
Compile

I
.
E
V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

./prog

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
Compile

I
.
E
V
S

S
.B

Type-checking
Linear processing

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

251

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Type-checking

int reptile() {

return "frog";

N
}

I
.

E
V

S
S

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

252

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

253

Type-checking

int reptile() {

return "frog";

N
}

I
.

E
V

S
S

reptile.c:
.B In function reptile:

W
reptile.c:2:5:
warning:
return
makes

W
W
integer
from pointer without a cast

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Type-checking

def vegetable(day):

if day != "Tuesday":

return.IN"tomato"

E
else:
SV
S
1000

B
.return

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

254

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

255

Type-checking

def vegetable(day):

if day != "Tuesday":

return.IN"tomato"

E
else:
SV
S
1000

B
.return

Python says: no problem

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int reptile() {

return "frog";

N
}

I
.

E
V

S
S

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

256

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int () {

return char*;

N
}

I
.

E
V

S
S

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

257

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

258

int vegetable(char *day) {

if (strcmp(day, "Tuesday") != 0){

N
return "tomato";

I
.
E
} else {
SV
S
return
.B 1000;

W
}
W
}
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int (char*) {

if (int){

N
return char*;

I
.
E
} else {
SV
S
return
.B int;

W
}
W
}
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

259

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int (char*) {

if (int){

N
return char*;

I
.
E
} else {
SV
S
return
.B int;

W
}
W
}
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

260

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int (char*) {
if (int){
N
return char*;
I
.
E
} else { SV
S
return
.B int;
W
} W
} W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

261

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
Everything
has
I
.
E
V
S
S
.B
W
a single,
fixed
type
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

262

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

def foo(a, b):

return
a
+
b

IN
.
E

V
S

S
.B

W
foo(2,
3)

W
W
foo("2", "3")

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

263

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Variable Declarations

int foo;
float foo;
double foo; .IN
E
char foo; SV

S
.B

W
int foo[42];
W
int *foo;
struct Bar foo;
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

264

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Function Declarations

double fmin(double, double);

return type.BS

N
I
.
E

V
S argument types

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

265

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

Function Declarations

void exit(int);

N
I
.
E

V
returns nothing

S
S
.B

intWrand(void);

takes no arguments

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

266

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

267

int foo(int
a,
int
b){

N
I
.
E
V
return
a
+
b;

S
S
.B
}
WW
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

reptile.c: In function
reptile:

N
I
.
E
reptile.c:2:5:
warning:
return
makes

V
S
S
integer from
.B pointer without a cast

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

268

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

reptile.c: In function
reptile:

N
I
.
E
reptile.c:2:5:
warning:
return
makes

V
S
S
integer from
.B pointer without a cast

int a = 4;

float b = (float)a;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

269

www.onlineeducation.bharatsevaksamaj.net

int
float
double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

all theoretical casts

270

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

int
float
double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

allowed casts

271

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

272

www.onlineeducation.bharatsevaksamaj.net

int

float

double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

allowed casts

273

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

int
float
double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

allowed casts

274

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

int
float
double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

implicit casts
275

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

int
float
double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

implicit casts

276

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

int
float
double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

implicit casts
277

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

int
float
double
N
I
.
char
E
V
S
S
int[] W.B
W
int*W
void (*f )(int)
struct X

implicit casts
278

www.bssskillmission.in

int
float
double
char
int[]
int*
void (*f )(int)
struct X

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
Compile

I
.
E
V
S

S
.B

Type-checking
Linear processing

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

279

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

V
S

S
.B

Linear processing

( just a small note)


www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

280

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

You can only use


whats declared above
N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

281

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int main() {

printf("%d\n", answer());

return 0;

N
I
.
}

V
S

S
.B

int answer()
{

W
W
return
1337;

W
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

282

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int main() {
printf("%d\n", answer());
return 0;
N
I
.
}
E

V
S

S
.B

int answer()
{
W
W
return
1337;
W
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

283

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

int answer() {

return 1337;

N
I
.
E

V
int main()SS{

B
.
printf("%d\n",
answer());

W
W
return
0;

W
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

284

www.onlineeducation.bharatsevaksamaj.net

285

www.bssskillmission.in

int answer();

declaration

int main() {

N
I
.
printf("%d\n",
answer());

E
V
returnSS0;

B
.
}

int answer() {

return 1337;

definition

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

HMM

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

286

www.onlineeducation.bharatsevaksamaj.net

287

www.bssskillmission.in

int answer();

declaration

int main() {

N
I
.
printf("%d\n",
answer());

E
V
returnSS0;

B
.
}

int answer() {

return 1337;

definition

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

288

www.bssskillmission.in

int answer();

declaration

int main() {
N
I
.
printf("%d\n",
answer());
E
V
returnSS0;
B
.
}
W

int answer() {
return 1337;
}

definition

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

289

www.bssskillmission.in

int answer();

declaration

#include "answer.h"
int main() {
N
I
.
printf("%d\n",
answer());
E
V
returnSS0;
B
.
}
W

int answer() {
return 1337;
}

definition

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

290

www.bssskillmission.in

Pre-Process
N
I
.
E

V
S

S
.B

Compile

Link
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

291

www.bssskillmission.in

main.o
N
I
int main() VE.
S
S
.B
W
intWanswer()
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

main.o

N
I
int main() VE.
S
S
.B
W
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

292

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

293

answer.c: In function main:

answer.c:4: warning: implicit declaration of

function answer

N
I
. architecture x86_64:

Undefined symbols E
for
V
"_answer", referenced
from:

S
S
_main in
.B ccuzmRrm.o

W not found for architecture x86_64

ld: symbol(s)
W
collect2:
W ld returned 1 exit status

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

294

answer.c: In function main:

answer.c:4: warning: implicit declaration of

function answer

N
I
. architecture x86_64:

Undefined symbols E
for
V
"_answer", referenced
from:

S
S
_main in
.B ccuzmRrm.o

W not found for architecture x86_64

ld: symbol(s)
W
collect2:
W ld returned 1 exit status

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

295

answer.c: In function main:

answer.c:4: warning: implicit declaration of

function answer

N
I
. architecture x86_64:

Undefined symbols E
for
V
"_answer", referenced
from:

S
S
_main in
.B ccuzmRrm.o

W not found for architecture x86_64

ld: symbol(s)
W
collect2:
W ld returned 1 exit status

Compiler: I dont know what answer is.


Ill assume it returns an int.
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

296

answer.c: In function main:

answer.c:4: warning: implicit declaration of

function answer

N
I
. architecture x86_64:

Undefined symbols E
for
V
"_answer", referenced
from:

S
S
_main in
.B ccuzmRrm.o

W not found for architecture x86_64

ld: symbol(s)
W
collect2:
W ld returned 1 exit status

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

297

answer.c: In function main:

answer.c:4: warning: implicit declaration of

function answer

N
I
. architecture x86_64:

Undefined symbols E
for
V
"_answer", referenced
from:

S
S
_main in
.B ccuzmRrm.o

W not found for architecture x86_64

ld: symbol(s)
W
collect2:
W ld returned 1 exit status

Linker: I looked in all the object files,


but I couldnt find answer.
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

main.o

N
I
int main() VE.
S
S
.B
W
W
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

298

www.onlineeducation.bharatsevaksamaj.net

299

www.bssskillmission.in

main.o
N
I
int main() VE.
S
S
.B
W
W
W

answer.o

int answer()

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

300

www.bssskillmission.in

main.o
N
I
int main() VE.
S
S
.B
W
W
W
int main() {
printf("%d\n", answer());
return 0;
}

answer.o

int answer()
int answer() {
return 1337;
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

301

www.bssskillmission.in

main.o
N
I
int main() VE.
S
S
.B
W
W
W

answer.o

int answer()

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

302

www.bssskillmission.in

prog
N
I
.
int
E main()

V
S

S
.B

int answer()

gcc -o prog main.c answer.c


www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

answer.c: In function
main:

N
I
.
E
answer.c:4: warning:
implicit declaration of

V
S
function answer

.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

303

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

answer.c: In function
main:

N
I
.
E
answer.c:4: warning:
implicit declaration of

V
S
function answer

.B

Compiler: I dont know what answer is.


Ill assume it returns an int.
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

304

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

answer.h

int answer();
main.c

N
#include "answer.h"
I
.
E
V

S
S

.B {
int main()
W
printf("%d\n",
answer());

W
Wreturn 0;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

305

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

answer.h

int answer();

answer.c

N
#include "answer.h"

I
.
E
V

S
S

.B
int answer()
{

W
return
1337;

W
W
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

306

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

307

Summary

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

answer.h

int answer();

main.c

N
#include "answer.h"

I
.
E
V

S
S

.B {

int main()
W
printf("%d\n",
answer());

W
Wreturn 0;

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

308

www.onlineeducation.bharatsevaksamaj.net

Preprocess:

www.bssskillmission.in

gcc -E main.c

int answer();

N
I
int main() {
E.
V
S
printf("%d\n",
answer());

S
B
.
return
W 0;

}
WW

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

309

www.onlineeducation.bharatsevaksamaj.net

Compile:

www.bssskillmission.in

gcc -c main.c main.c

main.o
%!(*@

N
I
.
E

answer.o

V
S

S
.B

%!(*@
W

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

310

www.onlineeducation.bharatsevaksamaj.net

Link:

www.bssskillmission.in

311

gcc -o prog main.o main.o

prog
%!(*@

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

312

www.bssskillmission.in

Pre-Process

N
I
.
E

V
S

S
.B

Compile

Link

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

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.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

313

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

The assignment was split into 2 problems, but below we've included only the final
versions of all the files.
The header files remained unchanged.

N
I
.
E

FIBONACCI.C

#include <stdio.h>
#include "fibonacci.h"
void print_fib(int n) {
printf("%d\n", fib(n));
}
int fib(int n) {
if (n <= 0) {
return
} else if(n ==
return
} else {
return
}
}

V
S

S
.B

0;
1) {
1;

fib(n-1) + fib(n-2);

REVERSE.C

#include <stdio.h>
void reverse(char *str, int len) {
char reversed[len+1];
int pos = 0;
int caret = len;
reversed[len] = '\0';
for (int i = len - 1; i >= -1; --i){
www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

314

www.onlineeducation.bharatsevaksamaj.net
www.bssskillmission.in
if (i == -1 || str[i] == ' '){
for (int j = i + 1; j < caret; ++j){
reversed[pos++] = str[j];
}
if (i != -1) {
reversed[pos++] = ' ';
}
caret = i;
}

printf("%s\n", reversed);

FIBEVERSE.C

#include
#include
#include
#include

<stdlib.h>
<string.h>
"fibonacci.h"
"reverse.h"

N
I
.
E

int main(int argc, char *argv[]) {


int i = 1;

V
S

#ifdef FIBONACCI
if (i < argc) {

print_fib(atoi(argv[i]));

i++;

}
#endif

S
.B

#ifdef REVERSE
if (i < argc) {
reverse(argv[i], strlen(argv[i]));
i++;
}
#endif

return 0;
}

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

315

www.onlineeducation.bharatsevaksamaj.net

www.bssskillmission.in

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.

N
I
.
E

V
S

S
.B

www.bsscommunitycollege.in www.bssnewgeneration.in www.bsslifeskillscollege.in

316