Untitled

 avatar
unknown
plain_text
a month ago
1.9 kB
4
Indexable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cctype>
using namespace std;

//dinamicko alociranna memorija
class Team {
private:
    char *ime;
    char *stadion;
    char *grad;
    //copy metod za pomalce linii kod
    void copy(const Team &other) {
        this->ime = new char [strlen(other.ime) + 1];
        strcpy(this->ime, other.ime);
        this->stadion = new char [strlen(other.stadion) + 1];
        strcpy(this->stadion, other.stadion);
        this->grad = new char [strlen(other.grad) + 1];
        strcpy(this->grad, other.grad);
    }

    void freeMemory() {
        delete [] ime;
        delete []stadion;
        delete [] grad;
    }

public:
    //konstruktor so argumenti i default 2in1
    Team(char *ime = "", char *stadion = "", char *grad = "") {
        this->ime = new char [strlen(ime) + 1];
        strcpy(this->ime, ime);
        this->stadion = new char [strlen(stadion) + 1];
        strcpy(this->stadion, stadion);
        this->grad = new char [strlen(grad) + 1];
        strcpy(this->grad, grad);
    }

    //copy constructor
    Team(const Team &other) {
        copy(other);
    }

    //operator =
    Team &operator =(const Team &other) {
        if (this != &other) {
            freeMemory();
            copy(other);
        }
        return *this;
    }

    //destruktor
    ~Team() {
        freeMemory();
    }

    void print() {
        cout<<ime<<" "<<stadion<<" "<<grad<<endl;
    }
};

int main() {
    int n;
    cin>>n;
    Team *teams = new Team [n]; // bez default ke padne
    char ime[100], stadion[100], grad[100];
    for (int i=0;i<n;i++) {
        cin>>ime>>stadion>>grad;
        teams[i] = Team(ime, stadion,grad); // bez operator = ke padne
    }
    for (int i=0;i<n;i++) {
        teams[i].print();
    }
    return 0;
}
Editor is loading...
Leave a Comment