Sei sulla pagina 1di 16

Programming 101: Session 0.

2 - C++
Santiago Baldrich

August 22, 2013

Santiago Baldrich

Programming 101: Session 0.2 - C++

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

Programming 101: Session 0.2 - C++

Initializing an array

int a[2][3] = { {1,2,3} , {-1,-2,-2}}; Values of a by row: 1, 2, 3, -1, -2, -2.

Santiago Baldrich

Programming 101: Session 0.2 - C++

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

Programming 101: Session 0.2 - C++

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

Programming 101: Session 0.2 - C++

Dynamic Memory Allocation


When the allocation is done by a run-time library invoked during program execution. Allocation of dynamic objects: Using keyword new int *p = new int(256); This line will allocate unnamed memory of type int and will initialize it to 256. Deallocation of dynamic objects: Using keyword delete for single objects and delete[] for an array of objects. delete delete[]

Santiago Baldrich

Programming 101: Session 0.2 - C++

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

return-type function-name(type para1, type para2, . . .){ . . . body-of-the-function; . . . }

Santiago Baldrich

Programming 101: Session 0.2 - C++

Passing arguments to a function

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

Programming 101: Session 0.2 - C++

Passing arguments to a function

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

Programming 101: Session 0.2 - C++

Recursive functions

A recursive function is a function that calls itself, either directly or indirectly.


1 2 3

int gcd(int a, int b){ return b == 0?a:gcd(b,a%b); }

Santiago Baldrich

Programming 101: Session 0.2 - C++

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

Programming 101: Session 0.2 - C++

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

Programming 101: Session 0.2 - C++

Classes and Objects


C++ allows the declaration and denition of classes. Instances of classes are called objects. A class is separated into three access control sections: public, protected and private
1 2 3 4 5 6 7 8 9 10

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 & Destructors

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

Programming 101: Session 0.2 - C++

Potrebbero piacerti anche