Documente Academic
Documente Profesional
Documente Cultură
int x,y;
GetMax <int> (x,y);
2. Template clase
Exemplu:
3. Specializarea template-urilor
Trebuie definiti din nou toti membri (nu sint mosteniti de la template-ul generic)
mysequence<> myseq;
mysequence<char,10> myseq;
Namespace - concept care grupeaza entitati (clase, obiecte, functii) sub acelasi
nume
namespace identifier
{
entities
}
Exemplu:
namespace myNamespace
{
int a, b;
}
myNamespace::a
myNamespace::b
sau
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
using first::x;
using second::y;
cout << x << endl;
cout << y << endl;
sau
float myfunction (char param) throw (tip); // n este de tipul tip (de exemplu
tip=int) -> float myfunction (char param) throw (int);
sau
#include <exception>
using namespace std;
class myexception: public exception
{
virtual const char* what() const throw() // membru virtual what - poate fi
supradefinit
{
return "My exception happened";
}
} myex; // myex este o clasa derivta din clasa exception
in main:
try
{
throw myex;
}
catch (exception& e) // parametru pentru catch este un pointer la o clasa myex
{
cout << e.what() << endl;
}
#include <exception>
using namespace std;
int main () {
try
{
int* myarray= new int[1000];
}
catch (exception& e)
{
cout << "Standard exception: " << e.what()
<< endl;
}
return 0;
}
Conversii implicite
Conversii explicite: (tip)nume_var
- dynamic_cast
reinterpret_cast
folosit pentru clas acre nu sint intr-o relatie de tip clasa de baza , clasa
derivata
functioneaza pentru orice clase
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B*>(a);
Vector:
https://en.cppreference.com/w/cpp/container/vector
(vector_template.pdf)
template<
class T,
class Allocator = std::allocator<T>
> class vector;
trebuie incluse:
<vector> - clasa vector
<cstdlib> - alocari dinamice
at
std::vector<int> data = { 1, 2, 4, 5, 5, 6 };
// Set element 1
data.at(1) = 88;
empty
std::vector<int> numbers;
numbers.empty() - intoarce tru sau false
erase:
int main( )
{
std::vector<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
print_container(c);
c.erase(c.begin());
print_container(c);
c.erase(c.begin()+2, c.begin()+5);
print_container(c);
// Erase all even numbers (C++11 and later)
for (std::vector<int>::iterator it = c.begin(); it != c.end(); ) {
if (*it % 2 == 0) {
it = c.erase(it);
} else {
++it;
}
}
print_container(c);
}
iesire:
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 6 7 8 9
1 7 9
Liste
https://en.cppreference.com/w/cpp/container/list
(list_template.pdf)
Exemple:
template<
class T,
class Allocator = std::allocator<T>
> class list;
Membri:
Tablouri (array)
https://en.cppreference.com/w/cpp/container/array
(array_template.pdf)
template
template<
class T,
std::size_t N
> struct array;
Metode