Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2 - C++
Santiago Baldrich
Santiago Baldrich
Arrays
Manipulate a group of memory locations as a single entity while providing direct access to any individual component.
1 2 3 4 5 6 7 8 9 10
#include<iostream> using namespace std; int main(){ int i, a[10]; for(int i=0;i<10;i++){ cout << "a[" << i+1 << "]="; cin>>a[i]; } for(int i=9;i>=0;i--) cout<< a[i] << " " <<endl; return 0; }
Santiago Baldrich
Initializing an array
Santiago Baldrich
Pointers
Variables that contain memory addresses as their values. double *dblPrt, eps; Pointer operators Operator Function & address of * dereference
What? returns the address of the object given as the operand. returns the object located at the address given to the operator.
Santiago Baldrich
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream> using namespace std; int main(){ int i = 50; int *pi; pi = &i; cout << *pi << endl; //Outputs 50 return 0; }
Santiago Baldrich
Santiago Baldrich
Functions
User-dened operations that facilitate the reuse of code within programs. A function basically consists of: Return type Function name List of parameters Body
1 2 3 4 5
Santiago Baldrich
Passing by value Default method, a copy of the arguments is made and passed to the corresponding parameters. The values of the passed arguments is left unchanged.
1 2 3 4 5 6
//Swap function. What will happen to x & y? void swap(int x, int y){ int t; t = x; x = y;y = t; return; }
Santiago Baldrich
Passing by reference The parameters receive the address of the arguments rather than a copy of their value.
1 2 3 4 5 6
//Swap function. What will happen to x & y? void swap(int &x, int &y){ int t; t = x; x = y;y = t; return; }
Santiago Baldrich
Recursive functions
Santiago Baldrich
References
A reference is an alias to an existing object. They must be initialized before their use.
1 2 3 4 5 6 7 8 9 10 11 12
int main(){ int really_long_name = 10; int &r = really_long_name; r = -10; cout << "really_long_name= " << really_long_name << endl; //Outputs -10 really_long_name = 7; cout << "r= " << r << endl; //Outputs 7 return 0; }
Santiago Baldrich Programming 101: Session 0.2 - C++
Templates
C++ templates can be used to parameterize functions or classes
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream> using namespace std; template <class T> void swap(T &x, T &y){ T t = x; x = y; y = t; } int main(){ int a = 10, b = 20; double d = 10.0, e = 20.0; swap(a, b); swap(d, e); return 0; }
Santiago Baldrich Programming 101: Session 0.2 - C++
Overloading
The same function name or operator symbol can be given several dierent denitions. The number and types of arguments tell the compiler which one to use.
1 2 3
int search(const int * data, const int key); int search(const float * data, const float key); int search(const double * data, const double key); When arguments do not exactly match the formal parameter types, the compiler will perform implicit conversions (e.g int into float) in an attempt to nd a match.
Santiago Baldrich
Exception Handling
An exception is a run-time anomaly that a program may detect. A programmer may choose to perform specic actions whenever an exception is raised (thrown).
1 2 3 4 5 6 7
try{ x = new long long int[n]; } catch(...){ cout << "Out of memory " << endl; }
Santiago Baldrich
class DumbExample{ public: //Data and methods accessible to any user of the class protected: //Data and methods accessible to class methods, // derived classes and friends only private: //Data and methods accessible to class //methods and friends }
Santiago Baldrich Programming 101: Session 0.2 - C++
Constructors are methods which are used to initialize an object at its denition time. They have the same name of the class and have no return value. For example, given a Point class, we may want to initialize to coordinates (0,0). They are implicitly called when we dene objects of their classes. Destructors are the mechanism that determine the way an object is destroyed when it becomes invalid.
Santiago Baldrich