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...