m06_CPP - bEgInSlIdE 1 Chapter 6 The C Programming Language...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion. Revised by Dani Kotlar 2009 1 Chapter 6 The C++ Programming Language The Role of Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion. Revised by Dani Kotlar 2009 2 C++ as Enhanced C Stronger typing. Inline functions. Call by reference. Function prototypes and overloading. Default function parameters. Namespaces. Type-safe I/O facilities. High Level Programming (the S tandard T emplate L ibrary) .
Background image of page 2
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion. Revised by Dani Kotlar 2009 3 Strong Typing Many type-conversions are unsafe , but most of them are given for free by C. C++ tries to prevent unsafe type-conversions. The usage of casting is discouraged, and is considered a safety problem (and a bad taste). So is the usage of type void * C++ provides special purpose casting operators , that are much safer, for rare occasions. int i; double x; x = i; // o.k. (usually) i = x; // warning (usually) int *pi; void *ptr; ptr = pi; // o.k. pi = ptr; // syntax error !! We’ll meet them in the future
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion. Revised by Dani Kotlar 2009 4 A special treatment is given to const objects: They cannot be modified. » hence, they must be initialized. They cannot be sent to a function that MAY modify them. » Hence, a function must declare whether it modifies an argument. » Note: An argument that is passed “by value” is never modified. Enumerated constants and const objects replace the need for MACROs that #define constants. Strong Typing ( const ) char *str = “abc”; // o.k (for a transition period) char const *cst = “abc”; // o.k. – the only safe way const int ci = 15; // initialization void func( int *); // a function declaration func(&ci); // syntax error at a function call Will be a syntax error May be a A warning
Background image of page 4
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion. Revised by Dani Kotlar 2009 5 (The Evil of) MACROs MACROs: Advantages: Global constants Common functionality without function call overhead Common functionality on variety of types Disadvantages: Do not obey scope rules Type-unsafe constructs C++ provides substitutes for MACROs without the above disadvantages: enum and const types (with strong typing) - to substitute MACRO constants inline functions - to eliminate function call overhead template functions - for type-safe common functionality on different types
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion. Revised by Dani Kotlar 2009 6 Inline Functions The following function will not incur function call overhead, when called inline int max( int a, int b) { return a < b ? b : a; } The function definition should appear in each file where it is used Hence, it is, usually, written on a header-file, that is #included wherever needed In fact, the compiler is not obliged to substitute the function call by its body but it will, if you use this feature judiciously
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2011 for the course CS 234122 taught by Professor Gillbarequet during the Spring '11 term at Technion.

Page1 / 40

m06_CPP - bEgInSlIdE 1 Chapter 6 The C Programming Language...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online