Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.

Similar presentations


Presentation on theme: "C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11."— Presentation transcript:

1 C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11

2 Scope Programs are built of nested "scopes". –Sometimes called "scope blocks" or "name spaces" Inner scopes "hide" or "shadow" outer scopes. Variables are only accessible when they are "in scope".

3 Kinds of scope Three main kinds of scope in C++ –Local: function or block –Class: extent of the class –File: only available within a file Also –Function scope: labels and gotos –Global scope

4 Scope Examples long x; float y; int z; void fn(char c, int x) {// parameter hides global x extern int z;// refer to global z double y = 3.14;// shadows global y { char y;// shadows double y from above y = c;// assigns to inner y ::y = 0.3;// assign to global y } y = y/3.0;// assign to local y z++;// increment global z }

5 Class Scope Class members and methods are available anywhere in the class without forward declaration (a.k.a. prototype) Examples: Vector::Vector(int x, int y) { Vector::x = x; Vector::y = y; }

6 New Topic: Optional Arguments Arguments to a function can be given default values. Example: int echo(int value=0) { return value; } echo(10); // returns 10 echo(); // returns 0

7 Optional Arguments (cont’d) Optional arguments must be at the end of the argument list When might this be useful?

8 New Topic: Overloading Functions Functions have signatures –A function's signature is the function's name its parameter list (types and orders) C++ allows functions with different signatures to have different definitions.

9 Overloading Functions Examples: int power(int a, int n); int power(double a, int n); What's a problem here?

10 Function Call Resolution: 1. If there's an exact match, call that version 2. Match through standard type promotions 3. Match through standard conversions 4. Match through user supplied conversions (section 8.7) 5. No match (error)

11 C++ is Strongly Typed. C++ is strongly typed –All variables must have a type before being used. Types must match before function calls can be allowed

12 Standard Promotions Promotions take types and increase their resolution Examples: –short to int –int to long –...

13 Standard conversions Conversions convert a variable to a different type Examples: –int to float –float to int –...

14 Example What happens in the following fragment: float x, a; int y; a = x + y;

15 C++ is Strongly Typed Types still need to match –Consider two functions like: int operator +(int lhs, int rhs); float operator +(float lhs, float rhs); –One of these must match for a = x + y;

16 When Does Conversion Happen? When does the compiler do this? –Arithmetic –Function calls (argument type conversion) –Return values

17 Explicit Type Conversion (Type Casting) In C++, casts look like: ( ) int x = int(3.14);

18 New Topic: More About References References can be variables as well as parameters to functions They must be initialized when declared Example: –int a; –int &ra = a;// ra is a reference to a

19 References (cont’d) A reference must be initialized to an "lvalue" –an lvalue is anything that can appear on the left- hand side of an assignment statement

20 New Topic: Read-only Variables (const) Variables can be declared read-only Example: const = ; const int forever_one = 1; Read-only variables are initialized at declaration but cannot be changed at run- time.

21 Read-only Parameters (const) Parameters can be read-only too. Example: int foo(const int x) {... } How does this come in handy?

22 New Topic: Dynamic Allocation in C++ Use operator new in place of malloc() = malloc( ); = new ; new understands about class constructors!

23 new [] new can be used for arrays too Example: –int * array = new int [10];

24 delete vs. free free( ); delete ;

25 Dynamic Allocation Examples int * int_ptr = new int; int * ten_ptr = new int(10); int [] int_array = new int [10]; Vector * vptr = new Vector(5, 6); // calls vector constructor! delete int_ptr; delete ten_ptr; delete [] int_prt; delete vptr;

26 A Longer Example: Fractions Fractions are numbers of the form: numerator/denominator Fractions have several properties: –The numerator and denominator are both integers. –The denominator cannot be 0 –Arithmetic operations are well defined for fractions.

27 Questions about Fractions How should a fraction be stored? –A pair of integers, numerator and denominator What operations should we support for fractions? –+, -, - (unary), etc. –, == –reduce(), print()

28 class Fraction { public: Fraction(); Fraction(int); Fraction(int, int); print(); Fraction operator +(Fraction); Fraction operator -(); Fraction operator -(Fraction); int operator <(Fraction); int operator >(Fraction); int operator ==(Fraction); private: int numerator, denominator; };

29 Constructor Considerations What if denominator is zero? –Exit the program We have three cases: –no arguments (default to 0/1) –one argument (numerator) –two arguments (numerator and denominator) Anything else?

30 Constructors Fraction::Fraction() { numerator = 0; denominator = 0; } Fraction::Fraction(int num) { numerator = num; denominator = 1; } Fraction::Fraction(int num, int denom) { numerator = num; if (denom == 0) exit(1); denominator = denom; }

31 print() Print() should print the fraction in the form numerator/denominator to stdout void print() { cout << numerator << “/” << denominator; }

32 Fraction Fraction::operator +(Fraction right) { Fraction answer; int left_num, right_num; left_num = right.denominator * numerator; right_num = denominator * right.numerator; answer.denominator = right.denominator * denominator; answer.numerator = left_num + right_num; return answer; }

33 Fraction Fraction::operator -() { Fraction answer(-numerator, denominator); return answer; }

34 Fraction Fraction::operator -(Fraction right) { Fraction answer; int left_num, right_num; left_num = right.denominator * numerator; right_num = denominator * right.numerator; answer.denominator = right.denominator * denominator; answer.numerator = left_num - right_num; return answer; }

35 An Easier Way to Subtract Fractions Fraction Fraction::operator -(Fraction right) { Fraction left = *this; return (left + -right); }

36 int Fraction::operator <(Fraction right) { int left_int = numerator * right.denominator; int right_int = right.numerator * denominator; return (left_int < right_int); } int Fraction::operator >(Fraction right) { int left_int = numerator * right.denominator; int right_int = right.numerator * denominator; return (left_int > right_int); }

37 int Fraction::operator ==(Fraction right) { Fraction left = (*this); return !(left right); }


Download ppt "C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11."

Similar presentations


Ads by Google