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.
// these are private, they cannot be accessed outside of the class
// these are public, they can be accessed from anywhere in the program
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
// function calls
Try to work through it. If you have more questions, I will try to answer them.