Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // ELEMENTE ALOCATE DINAMIC IN CLASA => COPY CONSTRUCTOR
- class Vector
- {
- private:
- int *v;
- int n;
- public:
- Vector(int n)
- {
- this->n = n;
- v = new int[n];
- }
- Vector(const Vector& other)
- {
- cout << "Copy constructor\n";
- n = other.n;
- v = new int[n];
- for(int i = 0; i < n; i++)
- v[i] = other.v[i];
- }
- ~Vector()
- {
- delete[] v;
- };
- void setElement(int ind, int val)
- {
- v[ind] = val;
- }
- int getElement(int ind)
- {
- return v[ind];
- }
- int catePare()
- {
- int cnt = 0;
- for(int i = 0; i < n; i++)
- if(v[i] % 2 == 0)
- cnt++;
- return cnt;
- }
- void operator*=(int x)
- {
- for(int i = 0; i < n; i++)
- v[i] += x;
- }
- int& operator[](int x)
- {
- return v[x];
- }
- void operator=(const Vector& other)
- {
- cout << "operator=\n";
- delete[] v;
- n = other.n;
- v = new int[n];
- for(int i = 0; i < n; i++)
- v[i] = other.v[i];
- }
- };
- int main()
- {
- Vector * a = new Vector(5);
- a->setElement(3, 12);
- cout << a->getElement(3);
- delete a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement