Explain this code to me

Hello this is my first post here so pardon me if I'm doing it wrong. My college gave me a piece of code for me to work through but I'm having a really hard time breaking it down line by line. Can anyone describe what is happening in this code? I don't understand most of the code written in the classes but mostly the operators.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

class Runner
{
private:
    char* name;
    bool sex;
    int age;
public:
    Runner(const char* _name="", bool _sex=false, int _age=0)
    {
        this->name=new char[strlen(_name)+1];
        strcpy(this->name, _name);
        this->sex=_sex;
        this->age=_age;
    }
    Runner(Runner& ob)
    {
        delete[] this->name;
        this->name=new char[strlen(ob.name)+1];
        strcpy(this->name, ob.name);
        this->sex=ob.sex;
        this->age=ob.age;
    }
    ~Runner()
    {
        delete[] this->name;
    }
    Runner& operator=(Runner& ob)
    {
        if (this!=&ob)
        {
            delete[] this->name;
            this->name=new char[strlen(ob.name)+1];
            strcpy(this->name, ob.name);
            this->sex=ob.sex;
            this->age=ob.age;
        }
        return *this;
    }
    int getAge()
    {
        return this->age;
    }
    bool operator>(Runner& ob)
    {
        return this->age>ob.age;
    }
    friend ostream& operator<<(ostream& out, Runner& ob)
    {
        out << ob.name << endl;
        if (ob.age)
            out << "Male" << endl;
        else
            out << "Female" << endl;
        out << ob.age << endl;
        return out;
    }
};

class Marathon
{
private:
    char location[100];
    Runner* array;
    int numRunners;
public:
    Marathon(char* _location="")
    {
        strcpy(this->location, _location);
        this->array=NULL;
        this->numRunners=0;
    }
    ~Marathon()
    {
        delete[] this->array;
    }
    Marathon& operator+=(Runner& ob)
    {
        Runner *temp=new Runner[this->numRunners+1];
        copy(this->array, this->array+this->numRunners, temp);
        temp[this->numRunners++]=ob;
        delete[] this->array;
        this->array=temp;
        return *this;
    }
    double procentAge()
    {
        int sum=0;
        for (int i=0; i<this->numRunners; ++i)
            sum+=this->array[i].getAge();
        return sum/((double)(this->numRunners));
    }
    void printYounger(Runner &u)
    {
        for (int i=0; i<this->numRunners; ++i)
            if (u>this->array[i])
                cout << this->array[i];
    }
};

int main() {
    char ime[100];
    bool Male;
    int age, n;
    cin >> n;
    char location[100];
    cin >> location;
    Marathon m(location);
    Runner **u = new Runner*[n];
    for(int i = 0; i < n; ++i) {
        cin >> name >> Male >> age;
    	u[i] = new Runner(name, male, age);

        m += *u[i];
    }
	m.printYounger(*u[n - 1]);
    cout << m.procentAge() << endl;
    for(int i = 0; i < n; ++i) {
        delete u[i];
    }
    delete [] u;
	return 0;
}
Ill do a little of it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  Runner& operator=(Runner& ob) //an assignment operator.  
    {
        if (this!=&ob) //if its not itself, in which case there is nothing to do.
        {
            delete[] this->name; //delete the memory rather than reuse it.  Seems sloppy, how did it get allocated in the first place?
            this->name=new char[strlen(ob.name)+1];//allocate more memory, rather than reuse it, still sloppy.  consider allocate once to a max size allowed. 
            strcpy(this->name, ob.name); //copy the name field from the input to the output, or from the right side of assignment to left side, x = y, this is putting y into x.
            this->sex=ob.sex; //straight copy, more y into x, just a different field.
            this->age=ob.age;  //as above
        }
        return *this;  //return the modified result item.
    }
    
    bool operator>(Runner& ob) //fairly straightforward... this is a comparison operator 
    {
        return this->age>ob.age;  //returns the Boolean result of this comparison which defines the operation.
    }

    friend ostream& operator<<(ostream& out, Runner& ob) //defines a cout operation to print the object with cout << myobject syntax, making use of the existing operator (via the friend). 
    {
        out << ob.name << endl; //what to do when writing this object? write this!
        if (ob.age)
            out << "Male" << endl; //and write one of these.  
        else
            out << "Female" << endl;
        out << ob.age << endl;  //and then this...
        return out;  //returns the prepared stream for cout to process. 
    }



Last edited on
Topic archived. No new replies allowed.