vectors inside classes

Solved
Last edited on

Condo(unsigned int fridgenumber); is a function, not a statement. it has no value.
Foodsupply doesn't return anything
buyfood, eatfood are functions with no body, they return nothing.

1
2
3
4
5
Condo::Condo(unsigned int fridgenumber)
:
fridgez(fridgenumber)

{}
What is that?"

inside main, there is nothing to even call an instance...

I would store the number of refrigerators and either the number of filled or empty refrigerators, since we can infer whichever we aren't explicitly storing.

There is no need for a container given your current class definition.
Solved
Last edited on
@cire I'm trying that now but I have trouble accessing the private member functions,

You don't have any private member functions.

On line 45, call the function as you do on line 46.

The member function buyFood promises to return a value, but does not always do so.


Solved
Last edited on
Does not always do so? It can't return anything, it doesn't have a return statement...
@ OP...
@pearly, Those statements are just saying what I will eventually turn the methods into, right now they do nothing. The function below is meant to set the fridgenumber to be equal to my private member fridgez, I'm not sure if I did it correctly. The main doesn't exist because I'm creating a class right now, I'm not trying to test it yet. I need to write the functions first.

If you know you haven't written methods yet, and not trying to test it, why are you trying to compile it? That makes no sense to me.

I don't know how you made it to classes, but honestly you need to re-read functions, and returning values. Be that as it may, Let's go over classes in brief.
A class is really nothing more than a collection of variables, and functions specific to an object. That's, no big secret. In this case you have an object refrigerators in the units of a condo. First off, other than practice, maybe to get it up and running, my personal preference is for each class to have it's own file. It helps keep you organized.

Let's make 3 files. main.cpp condo.h and condo.cpp

I always write my classes first, simply because I find it easier to define 1st and call later. So in condo.h Let's define our class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CONDO_H
#define CONDO_H
#include <iostream>
#include <string>

class Condo
{
public:
    void setmFoodSupply(int) ;
    int getmFoodSupply() ;

private:
    int mFoodSupply;
};

#endif // CONDO_H 


Easy enough - After the header guards and includes, You declare your class name - Condo
Define which access specifier you need. As a rule of thumb, members (variables) are private and methods (functions) are public. Your methods access your members. The method prototypes simply tell the compiler we have this type of functions, be on the lookout for them. Next we define our members. Since we want people to have access to your function, but not directly to your variables, you make them private. In this case... mFoodSupply... That's it, we're done.

Next lits hop over to condo.cpp

Here we'll define the functions we just prototyped.

1
2
3
#include "Condo.h"
void Condo :: setmFoodSupply(int fs){mFoodSupply=fs;}
int Condo :: getmFoodSupply(){return mFoodSupply ;}


setmFoodSupply takes and integer and sets it to your private member mFoodSupply. That's pretty simple right? Sure it is.
getmFoodSupply does the opposite. It takes the value of mFoodSupply, and returns it to the programmer in some manner. You still with me? I know you would be... :) Don't forget to include "Condo.h"

Now for the main.cpp ... This is where you have your fun

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include "Condo.h"
using namespace std;


int main()
{
    Condo unit;

    unit.setmFoodSupply(5);   
    int a=unit.getmFoodSupply(); 
    cout << "I have " << a << " items in my food supply" << endl;  // print a
    // or
    cout << "I have " << unit.getmFoodSupply() << " items in my food supply." << endl; // print get function

    return 0;
}


Nothing hard here...
Write your main like you always do... Do your includes, remember to #include "Condo.h". Namespace isn't such a hot idea, but for this tiny example, it'll be alright. Declare your main function... int main()

Let's make an object... Condo unit; It doesn't get any easier than that. unit.setmFoodSupply(5); Here we send the number 5 to the method setmFoodSupply, and it gets added to the value that it is holding. Simple right? int a=unit.getmFoodSupply(); asks the compiler to look in mFoodSupply and and assign that value to the variable a. Kickin yourself by now huh? Now you can print 'a' OR print the value in a direct call in your print statement.

WOOHOO!! You have a working class!!!

Now... You you have an example that if you try to turn in you will get an "F" on, you have to figure out how to make YOUR class. get/set functions won't cut it. You need to figure out some real function and your constructors and de-constructors. I suck at those so I'll step out and let someone else help you with that.

I hope that shoves you in the right direction...
Last edited on
pearlyman wrote:
Does not always do so? It can't return anything, it doesn't have a return statement...

Perhaps you can explain what return 0; means? Until now, I've always considered it a return statement.

helloworld135 wrote:
I'm mostly confused with how the program will know if the fridges are full or empty, I tried to fix it by intializing the fullFridges as 10 but I'm not sure if that's correct?

I'm not sure what you're asking. The member methods have access to the private data, and clients of the class can query the food supply via the public interface.

Here's something in a similar vein:

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
#include <iostream>

// Say we have a day care that keeps sleeping mats in lockers.
// 3 sleeping mats will fit in a locker.
const unsigned sleeping_mats_per_locker = 3;

struct DayCare
{
    DayCare(unsigned num_lockers) : _lockers(num_lockers), _mats(0) {}

    bool add_mats(unsigned num_mats)
    {
        if (_mats + num_mats <= _lockers * sleeping_mats_per_locker)
        {
            _mats += num_mats;
            return true;
        }
        return false;
    }

    bool widthdraw_mats(unsigned num_mats)
    {
        if (num_mats <= _mats)
        {
            _mats -= num_mats;
            return true;
        }
        return false;
    }

    unsigned mats_stored() const { return _mats; }
    unsigned lockers_in_use() const { return _mats ? (_mats / 3 + 1) : 0u; }
    unsigned lockers() const { return _lockers; }

private:
    const unsigned _lockers;
    unsigned _mats;
};

int main()
{
    const unsigned num_lockers = 5;
    DayCare dc(num_lockers);

    bool done = false;
    do
    {
        std::cout << "Daycare has " << dc.lockers() << " lockers.\n";
        std::cout << dc.lockers_in_use() << " are in use storing ";
        std::cout << dc.mats_stored() << " sleeping mats.\n\n";

        std::cout << "Please enter:\n1 to add more mats\n2 to withdraw mats\n";
        std::cout << "Or anything else to quit.\n> ";

        int selection;
        if (std::cin >> selection && selection == 1 || selection == 2)
        {
            if (selection == 1)
            {
                if (!dc.add_mats(1))
                    std::cout << "Unable to add any more mats!\n";
            }
            else
            {
                if (!dc.widthdraw_mats(1))
                    std::cout << "Unable to withdraw mats!\n";
            }
        }
        else
            done = true;
    } while (!done);
}
Last edited on
Solved
Last edited on
Solved
Last edited on
SECOND TEST MAIN... Not sure if I did it correctly but I think it's broken?


When one writes test code, one must give it as much thought as one does writing the code to be tested. Look at your constructor. How many full refrigerators will you start with for Condo myCondo(10)? So what should a single call to myCondo.buyFood() return?
Solved
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
unsigned int Condo::buyFood(){
    if(Condo::fullFridges< Condo::fridgez){
   Condo::emptyFridges--;
   Condo::fullFridges++;
   return Condo::fullFridges;}
    else
   return Condo::fullFridges;;
}

double Condo::foodSupply() const{
    return fullFridges*2;
}

unsigned int Condo::eatFood(){
     if(Condo::emptyFridges< Condo::fridgez){
   Condo::emptyFridges++;
   Condo::fullFridges--;
   return Condo::fullFridges;}
   else
    return Condo::fullFridges;
}


Why would you do that? That kind of defeats the purpose of an else statement...

1
2
3
4
5
6
7
8
unsigned int Condo::eatFood(){
     if(Condo::emptyFridges< Condo::fridgez){
   Condo::emptyFridges++;
return Condo::emptyFridges;
else
   Condo::fullFridges--;
   return Condo::fullFridges;}
  
Something along that line may work better.
I'm always returning the value of fullFridges, and I also need to change the value of emptyFridges if my fullFridges is changed for my class to work properly as it is now. Otherwise I could potentially have 10 filled and empty fridges at the same time, when I only have 10 fridges. I'm sure I could do it more effectively but I'm really just trying to call that class(which I think is working now) into another class.

I'm basically saying if every fridge isn't empty, eat a fridge and remove the food from it, and also classify the old fridge as empty. If however all my fridges are full, simply tell the user they are all full and do nothing.

Last edited on
Okay, well if you will always be returning FullFridges, then you can nix the else all together...

That's like your dad telling you that you have your choice of punishments... restriction for a week, or restriction for a week....
Solved
Last edited on
Topic archived. No new replies allowed.