Beruflich Dokumente
Kultur Dokumente
#include <iostream>
using namespace std;
//transmitere prin valoare (passsing by value)
void sum(int x, int y)
{
x = x + y;
}
//transmitere prin adresa/pointer (passsing by address/pointer)
void sum_a(int* x, int y)
{
*x = *x + y;
}
//transmitere prin referinta (passsing by reference)
void sum_r(int&x, int y)
{
x = x + y;
}
int main()
{
int x = 5;
int* px;
px = &x;
cout << *px << "\n";
int& y = x;
x++;
cout << y << endl;
int* py = px + 2;
py--;
cout << py - px << endl;
int a = 3, b = 7;
sum(a, b);
cout << a << endl;
sum_a(&a, b);
cout << a << endl;
sum_r(a, b);
cout << a << endl;
//alocarea de spatiu pt un double in heap cu malloc (allocating space for one
double in heap with malloc)
double* pd = (double*)malloc(sizeof(double));
*pd = 1.2;
cout << *pd << endl;
free(pd);
pd = NULL;
Seminar 3
#include <iostream>
#include <string>
using namespace std;
//static matrix
int m[2][3] = { {1, 2, 3}, {4, 5, 6} };
add_element(z, 3, 5);
for (int i = 0; i < 4; i++)
{
cout << z[i] << " ";
}
delete[] z; z = NULL;
}
Seminar 4
#include <iostream>
#include <string>
using namespace std;
struct person
{
char firstName[40];
string surname;
int age;
float height;
};
class Person
{
//accessible just for this class
private:
char firstName[40];
//accessible for this class and all the inherited classes
protected:
float height;
//accessible for everyone
public:
string surname;
int age;
Seminar 5 + 6+7
#include <iostream>
#include <string>
using namespace std;
class FootballTeam
{
private:
//constant fields should be initialized at declaration or in the constructors
const int id;
string name;
unsigned matches_played;
unsigned* goals_scored;
public:
//static field | it is common for all the objects | it is saved at class
level, not at instance level
static int nb_of_players;
//default constructor | constant field initializing is mandatory
FootballTeam() : id(0)
{
name = "";
matches_played = 0;
goals_scored = NULL;
}
//constructor with 2 parameters
FootballTeam(int id, string name) : id(id)
{
//the usage of this is mandatory, otherwise the argument will hide the
attribute
this->name = name;
matches_played = 0;
goals_scored = NULL;
}
//constructor with 3 parameters
FootballTeam(string name, unsigned matches_played, unsigned*
goals_scored) : id(2)
{
this->name = name;
this->matches_played = matches_played;
//goals_scored cannot be copied with =, it would be a shallow copy
if (goals_scored != NULL)
{
this->goals_scored = new unsigned[matches_played];
for (unsigned i = 0; i < matches_played; i++)
{
this->goals_scored[i] = goals_scored[i];
}
}
else
{
this->matches_played = 0;
this->goals_scored = NULL;
}
}
//copy constructor
FootballTeam(const FootballTeam& t) : id(t.id)
{ name = t.name;
matches_played = t.matches_played;
if (t.goals_scored)
{ goals_scored = new unsigned[matches_played];
for (unsigned i = 0; i < matches_played; i++)
{
goals_scored[i] = t.goals_scored[i];
}
}
else
{
matches_played = 0;
goals_scored = NULL;
}
}
//destructor
~FootballTeam()
{
if (goals_scored != NULL)
{
delete[] goals_scored;
}
}
//equals operator
FootballTeam& operator=(const FootballTeam& t)
{
if (goals_scored)
{
delete[] goals_scored;
}
name = t.name;
matches_played = t.matches_played;
if (t.goals_scored)
{
goals_scored = new unsigned[matches_played];
for (unsigned i = 0; i < matches_played; i++)
{
goals_scored[i] = t.goals_scored[i];
}
}
else
{
matches_played = 0;
goals_scored = NULL;
}
return *this;
}
//pre-incrementation operator
FootballTeam& operator++()
{
this->goals_scored[matches_played - 1]++;
return *this;
}
//post-incrementation operator
FootballTeam operator++(int i)
{
FootballTeam copy = *this;
this->goals_scored[matches_played - 1]++;
return copy;
}
//addition operator (works just for adding FootballTeam and unsigned values,
not vice versa)
FootballTeam operator+(unsigned value)
{
FootballTeam copy = *this;
copy.goals_scored[copy.matches_played - 1] += value;
return copy;
}
return in;
}
//the output operator
ostream& operator<<(ostream& out, FootballTeam t)
{
out << "Name: " << t.name << endl;
out << "Matches played: " << t.matches_played << endl;
out << "Goals scored per match: ";
if(t.goals_scored)
{
for (unsigned i = 0; i < t.matches_played; i++)
{
out << t.goals_scored[i] << " ";
}
}
else
{
out << "none";
}
out << endl;
return out;
}
//just an empty function which shows that the copy constructor is called when
passing an object by value
void DoNothing(FootballTeam t)
{
int main()
{
//anonymous block to force the call of the destructor at the end
{
//default constructor is called
FootballTeam t1;
}
//contructor with 2 parameters is called, 99 is the id that cannot be
modified anymore
FootballTeam t2(99, "Real Madrid");
unsigned v[] = { 3, 3, 3, 1, 2 };
unsigned w[] = { 0, 0, 1 };
t3.set_goals(3, w);
DoNothing(t3);
FootballTeam t5;
//equals operator call
t5 = t4;
t5.set_goals(3, w);
//FootballTeam* q = p;
//deep copy of the object that p points to into another object that q
will point to
FootballTeam* q = new FootballTeam(*p);
//displays 11
cout << t3.nb_of_players << endl;
t4.nb_of_players++;
t3.nb_of_players--;
//operators testing
FootballTeam t6 = ++t3;
FootballTeam t7 = t3++;
FootballTeam t8 = t3 + 4;
FootballTeam t9 = 4 + t3;
FootballTeam t10;
cin >> t10 >> t3;
cout << t3 << t4;