lecture31-apr17 - Lecture 31 Announcements Summer: teaching...

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

View Full Document Right Arrow Icon
Lecture 31 Announcements • Summer: teaching ee322c –> 11:30-1pm MWF • Assignment 6 – Q&A; due today • Topics of the day - C++ – Variable declarations – Reference parameters – Overloaded functions – Namespaces – C++ strings – Introduction to OO thinking Introduction to C++ • Invented in the 1980’s to support modern programming principles - e.g. abstract data types • It incorporates Standard C, but has additional capabilities • Big differences – Class based – Object oriented – Templates – Overloaded operators – Exception handling • Little differences –Keywo rds –IO – Strings – Booleans and wide characters – Call by reference – Explicit namespaces – Different way of doing dynamic storage allocation – One line comments // this for example Last time we looked at: Variable Declarations variables can be defined anywhere and are valid within the scope of the block in which they are defined They should be defined right before their first use, but within the scope of all their uses #include <iostream> using namespace std; int main () { cout << “Enter a number” << endl; { int number = 0; cin >> number ; cout <<“you entered ” << number << endl; } cout << “number entered =” << number <<endl; // will fail on compile since it’s out of scope return 0; } Reference Parameters You write: void blah (int x, int y) { x = x + y; } int main(void) { int a = 0; blah(a, 42); } The compiler rewrites this as : void blah (int * x, int y) { *x = *x + y; } int main(void) { int a = 0; blah(&a, 42); } •A reference parameter ( & ) tells the compiler that you really meant for the parameter to be a pointer –The compiler automatically inserts all Restrictions are: –You cannot obtain the address of a reference –You cannot create an array of references –You cannot reference a bit field –Null references are prohibited Overloaded Function Names #include <iostream> using namespace std; // abs is overloaded three ways int abs(int i); double abs(double d); long abs(long l); int main() { cout << abs(-10) << endl; cout << abs(-11.0) << endl; cout << abs(-9L) << endl; return 0; } •Two or more functions can share the same name as long as their parameters are different •This makes the function call context sensitive int abs(int i) { cout << "Using integer abs()\n"; if ( i<0) return -i; else return i; } double abs(double d) { cout << "Using double abs()\n"; if ( d<0) return -d; else return d; } long abs(long l) { cout << "Using long abs()\n"; if ( l<0) return -l; else return l; } Namespaces ± Namespaces allow the same names (e.g. identifiers) to be reused under different qualifying contexts w
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

lecture31-apr17 - Lecture 31 Announcements Summer: teaching...

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

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