Overloading Operator

I need to compare objects in a program. I need to provide the operator overloading for the operator ">" so that Country can be compared based on its instance variable population and the non-decreasing order of Country objects is defined as the non-decreasing order of integer values of its instance varialbe population. I hope I described that correctly and it makes sense. I have never done this before and I am confused on how I do the overloading operator. Any help would be greatly appreciated! Here is the Country.h, and Country.cpp programs...

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
#ifndef COUNTRY_H
#define	COUNTRY_H

#include <iostream>
#include <string>

using namespace std;

class Country
{
  public:
    Country();
    Country(string n, long p, double a);
    string get_name() const;
    long get_population() const;
    double get_area() const;
    friend bool operator>(Country C1, Country C2);
    friend ostream &operator<<(ostream &out, const Country &C); 

  private:
    string name;
    long population;
    double area;    // square kilometers
};

#endif	/* COUNTRY_H */ 


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


#include "Country.h"

Country::Country()
{
    population = 0;
    area = 0;
}

Country::Country(string n, long p, double a)
{
    name = n;
    if (p < 0 || a < 0)
    {
       cout << "Invalid number! Reset to 0." << endl;
       population = 0;
       area = 0;
    }
    else 
    {
        population = p;
        area = a;
    }
}

string Country::get_name() const
{
    return name;
}

long Country::get_population() const
{
    return population;
}

double Country::get_area() const
{
    return area;
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/202818/
Topic archived. No new replies allowed.