OK, FIRST off, you need to put that code into the code tags. They are there for a reason, it makes your code much easier to read.
A constructor is a member function of a given class. It does not merely assign values to the class's member variables but initializes them. (YES, there is a difference between creating and assigning variables vs initializing them!) To create a constructor, you need to create it like a normal member function except the name of the function must be that of your class. For example, if you had a card class, your constructors would be called card(). Constructors have NO return type (as i recall).
Each constructor must have a different set of arguments. This is to make sure that the constructors can be differentiated. The default constructor has no arguments. It creates any object of its class that is given no arguments. So take this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
struct card
{
int rank;
char suit;
card()
{
rank = 0;
suit = '\n';
}
card(int givesuit, char giverank)
{
rank = giverank;
suit = givesuit;
}
}
int main()
{
card emptycard;
card notemptycard(9,'C');
return 0;
}
|
Because the first card was created without any constructor arguments, it remained empty and was initialized with a call to the default constructor. Usually the default constructor is used to provide "empty" values to a class object to make it clear that that object has not yet been used.
The second card was created with an argument list. Because that argument list matches the arguments of the second constructor in the class, it calls that constructor to create the card, executing the actions within the constructor.
Keep in mind that although a constructor is designed to construct a class object, it is a function and does not have to consist solely of assignments.
I presume you know what it means to overload a function. It is to create multiple functions of the same name, but without the same argument lists. That way, you can refer to the function by one name but give it different sets of args in each call, and based on the arg lists the compiler then differentiates which one you want to call. Overloading an operator is a slightly different story but I would say it's one of the coolest things in C++ that you should totally know.
Basically, overloading an operator enables it to take on a different meaning when your class objects are used in context with the operator in question. For example let's say I wanted to compare which cards were higher in the above example. I could write a comparison member function to take two cards and compare them or I could just redefine the > and < operators. Another example of an overloaded operator is the cout and cin objects. Those << and >> operators are actually bitwise shift operators, not stream manipulation operators. But by overloading the operators within the cout and cin class objects, they take on a specific meaning that allows them to be used in a different way.
Here's an example.
1 2 3 4
|
bool operator<(card first, card second)
{
return first.rank < second.rank;
}
|
An overloaded operator must have at least one user-defined class as an operand, and must use the same number of operands as the original operator. Overloaded operators do NOT have to be members of a class. However, if they are, you can reduce the number of arguments by one since you have the implicit this operand (the class it is called from). You can overload almost all the operators, but I cannot remember which ones you can't overload. The :: and . operators are among them however. (Yes you can overload operators such as new, delete, () and []! Overloading the subscript operator is how the standard containers are accessed.) In addition, the assignment, subscript and function-call operators must be defined as member functions.
To overload the operator, you first issue a return type, then you type operator, and, without a space, the symbol for the one you want to overload. The above example demonstrates how. You can call an overloaded operator function as you would a normal operator:
1 2 3 4
|
card first(3,'S'), second(12,'H');
if (first < second)
{ // Some code here...
}
|
Or you can explicitly call it:
1 2 3 4
|
card first(3,'S'), second(12,'H');
if (operator<(first,second))
{ // Some code here...
}
|
It is not advisable to overload some operators, simply because they have important system-level meanings, such as the & operator. You risk screwing up existing code in your program if you did such a thing, so some operators should be more cautiously overloaded. However, some operators, such as the arithmetic, << and >>, and = operators can benefit significantly from overloading. In general you should define at least the < and == operators in your class because these operators are used by the standard containers and algos.
To overload the << and >> operators as your teacher expects, you would create an operator that takes an ostream (such as cout) and your class object, and returns a reference to the ostream argument. Then you would simply output the various members of your class like so:
1 2 3 4 5
|
ostream& operator<< (ostream& os, const card &thecard)
{
os << thecard.suit << " " << thecard.rank;
return os;
}
|
By redefining those operators a call like this one would then become legal:
1 2
|
card sixofdiamonds (6,'D');
cout << sixofdiamonds;
|
Anything I haven't cleared up for you?
EDIT: By the way, this website has tons of good lessons on programming in C++ and I'm sure you could find these topics in there somewhere if you are still confused.