Look, I'm not going to do your homework for you. I am willing to help you–up to a point, but I'm not going to just give you the answers. (Ok, I know I did for the first one, but I was feeling nice).
Because if I do that, then you're not going to actually learn how to use these things, you'll just copy and paste the answers, and use them without really trying to figure out how they work so you can use them for other things too.
Now, your compiler should be giving you a number of errors (at least 9) for the first program, so you can get started on correcting those. What compiler are you using, by the way?
A word of advice: In a class,
all members (functions, variables, etc) are
private, which means that they can only be accessed through member functions of that class (and friend functions, but we won't go into that). They can't be accessed by main unless they have been declared public.
Here's an example of a class with two private member variables and two public member functions.
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
|
class Example
{
private:
// these are private, they cannot be accessed outside of the class
int var1;
int var2;
public:
// these are public, they can be accessed from anywhere in the program
Example ()
{
var1 = 15;
var2 = 120;
}
void function1 ()
{
std::cout << "Var1: " << var1 << std::endl;
}
void function2 ()
{
std::cout << "Var2: " << var2 << std::endl;
}
}; // note: you need to put a semicolon after the class declaration.
// sample driver program to run the class
int main (int argc, char const *argv[])
{
// initialize object of the Example class
Example object;
// function calls
object.function1 ();
object.function2 ();
return 0;
}
|
Try to work through it. If you have more questions, I will try to answer them.
Best,
max