I am developing my own project for the practice. I am having a great deal of difficulty conceptualizing the classes I want. The scenario is that of 2 countries, each capable of producing 2 goods, using 2 factors of production.

Specifically, Lower Mongolia and Upper Mongolia have certain amounts of workers (labor) and machines (capital) with which each can produce some combination of guns and butter. Each good has a selling price, a labor use requirement, and a capital use requirement.

I made classes of the goods with their 3 members. I instantiated with the countries. I decided to use only 1 factor for simplicity. This didn't work because of duplicate variables for each good. By that I mean, the term Lower_Mongolia.price was the same for both guns and butter.

I am considered making a base class called country, having labor and capital as members, with derived classes for the goods, having price, labor use, and capital use, as members. However, I can't find good examples of how to instantiate the country names of Upper Mongolia and Lower Mongolia in the main() section. I still wind up with duplicate variables like Lower_Mongolia.price for both guns and butter.

Is there a way to achieve the organization I want using classes?

I worked on my problem and, with some help from another forum, came up with this code.

#include "../../std_lib_facilities.h"

class Goods {
protected:
    double unit_labor;
    double price;
    double labor;
public:
    double getUnit_labor(string country, string goods) {
    if (country == "home" && goods == "apples") {unit_labor = 6;}
    if (country == "home" && goods == "bananas") {unit_labor = 3;}
    if (country == "foreign" && goods == "apples") {unit_labor = 8;}
    if (country == "foreign" && goods == "bananas") {unit_labor = 12;}
    return unit_labor;
    }

    double getLabor(string country, string goods) {
    if (country == "home" && goods == "apples") {labor = 500;}
    if (country == "home" && goods == "bananas") {labor = 800;}
    if (country == "foreign" && goods == "apples") {labor = 300;}
    if (country == "foreign" && goods == "bananas") {labor = 1000;}
    return labor;
    }
};

class Apples : public Goods {
public:
};

class Bananas : public Goods {
public:
};

class Country {
protected:
    double labor;
    double capital;
public:
    double getLabor(string country, string goods) {
    if (country == "home" && goods == "apples") {labor = 500;}
    if (country == "home" && goods == "bananas") {labor = 800;}
    if (country == "foreign" && goods == "apples") {labor = 300;}
    if (country == "foreign" && goods == "bananas") {labor = 1000;}
    return labor;
    }
};

class Home : public Country {
public:
};

class Foreign : public Country {
public:
};

class World {
protected:
    double labor;
    double capital;

public:
    Goods apples;
    Goods bananas;
    Country home;
    Country foreign;
};

int main() {
    World home;

    cout << "The labor requirement for apples in the home country is " << home.apples.getUnit_labor("home", "apples") << ".\n";
    cout << endl;
	
    cout << "The labor requirement for bananas in the home country is " << home.bananas.getUnit_labor("home", "bananas") << ".\n";
    cout << endl;

    cout << "The labor endowment for apples in the home country is " << home.apples.getLabor("home", "apples") << ".\n";
    cout << endl;
	
    cout << "The labor endowment for bananas in the home country is " << home.bananas.getLabor("home", "bananas") << ".\n";
    cout << endl;


    World foreign;

    cout << "The labor requirement for apples in the foreign country is " << foreign.apples.getUnit_labor("foreign", "apples") << ".\n";
    cout << endl;
	
    cout << "The labor requirement for bananas in the foreign country is " << foreign.bananas.getUnit_labor("foreign", "bananas") << ".\n";
    cout << endl;

    cout << "The labor endowment for apples in the foreign country is " << foreign.apples.getLabor("foreign", "apples") << ".\n";
    cout << endl;
	
    cout << "The labor endowmen for bananas in the foreign country is " << foreign.bananas.getLabor("foreign", "bananas") << ".\n";
    cout << endl;

    keep_window_open();
    return 0;
}

It complies and runs yielding the correct output. But it is not written I feel it should be. Originally, I had the getLabor() function only in the Country class. That version did not compile. The error was that the getLabor() function could not be found in the Goods class.

I don't understand why the compiler can't find, or is not looking for, the getLabor() function in the Country class. Can anyone help me with an explanation?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.