C++ Part 1: Funcion overloading. Note: you may have already covered this and operator overloading, but I wanted to make sure it got covered. In C and Pascal, the following will produce an error: void CoolFuncion( int number ) { codecodecode } void CoolFunion( char letter ) { differentcodedcodedcode } Your compiler will tell you that you are not allowed to have two funcions with the same name. However, the above code WILL work in c++. The compiler will look at the arguments being passed to the function and decide on the appropriate one. Note: This means, that although you can have two funcions with the same NAME, if they have the same heading ie: void OneFunc( short num ) void OneFunc( short a ) (yes they are the same, a and num are both shorts) they will still produce and error. Now: why the heck would I want to use something like this? Well it's vary useful for constructors. For example: Let's assume you're making a program to graph points. A user can either give you coordinates, OR he can give you an angle and a distance. So, when creating your object how do you deal with these in your constructor? Simple: funcion overloading. Take a look at this bit of code: class Point { private: short x; short y; float angle; float distance; //we'll assume the user can give you decimals for these, and not for points. public: Point( short x, short y); Point( float angle, float dis ); ... }; Bingo! Two constructors! Now you can just pass the info you got from the user to the Point constructor and the correct one will be called automatically! This is probably the best and most common use of funcion overloading, since the constructor has a fixed name. Part 2: Operator Overloading DANGER DANGER DANGER in c++, in addition to overloading functions, you can overload operators. To do so, simply declare a function of the following form: operator() for example: int operator+( int ) { specialized code for adding two ints } operator functions can be used as part of classes ie, member functions, or as a function for the whole program. However, I discourage their use due to the fact that it's hard to remember whether you're using the original verion of the operator or not. If you forget.... well.... bad things may happen. Note: you may NOT overload the . .* :: ?: or sizeof() operators for ANY reason. Your compiler will tell you so if you try. Also, you may only overide existing operators YOU MAY NOT CREATE YOUR OWN For example: int operator a( int m ) is not allowed because a is not normally an operator. If you want more info on operator overloading look at chapter 10. It goes into much more detail than I do. However, I don't feel that oper. overloading is a safe habit so I won't spend any more time on it. Part 3: Friends Remember what the 4 access levels are? Here they are again if you don't: private: public: friend: protected: In this section we will deal with friend: I'll use last lectures demo program with a few additions to illustrate my point. Instead of just having employees with salaries, let's say we had supervisors who could give raises. Now we only want supervisors to be able to give a pay raise, so the code would have to be part of the Supervisor class. But, the data members of Employee are private:. Only employee functions can edit them! So how do we do it? We use friend: If something (i'll explain what in a sec) is declared as a friend of a class it can edit all members, even those marked as private. There are a few ways to declare friends. You can make an entire class a friend. For example: class OneClass { friend class TwoClass; ... }; This makes all of one class's variables accessible to all of TwoClasses functions, but it doesn't work the other way around!!! TwoClass's funcions are still private. You can also make a member function of another class a friend. class OneClass { friend void TwoClass::DoThing( void ); }; You can also make a regular function a friend: class OneClass { friend void main( void ); }; Note: you can group friend declarations together like this: class OneClass { friend: void TwoClass::DoThing( void ); void main( void ); private: ... }; Now look at the demo. //Supervisor.cpp Copyright 1996 GeekSoft Inc. #include #include class Employee; class Supervisor { // Data members... private: char supervisorName[ 30 ]; long supervisorID; float supervisorSalary; // Member functions... public: Supervisor( char *name, long id, float salary ); ~Supervisor( void ); void PayRaise( Employee *luckyPerson ); }; class Employee { friend class Supervisor; // Data members... private: char employeeName[ 30 ]; long employeeID; float employeeSalary; // Member functions... public: Employee( char *name, long id, float salary ); ~Employee( void ); void PrintEmployee( void ); }; Employee::Employee( char *name, long id, float salary ) { strcpy( this->employeeName, name ); this->employeeID = id; this->employeeSalary = salary; cout << "Creating employee #" << employeeID << "\n"; } Employee::~Employee( void ) { cout << "Deleting employee #" << employeeID << "\n"; } void Employee::PrintEmployee( void ) { cout << "-----\n"; cout << "Name: " << employeeName << "\n"; cout << "ID: " << employeeID << "\n"; cout << "Salary: " << employeeSalary << "\n"; cout << "-----\n"; } Supervisor::Supervisor( char *name, long id, float salary ) { strcpy( this->supervisorName, name ); this->supervisorID = id; this->supervisorSalary = salary; cout << "Creating supervisor #" << supervisorID << "\n"; } Supervisor::~Supervisor( void ) { cout << "Deleting supervisor #" << supervisorID << "\n"; } void Supervisor::PayRaise( Employee *luckyPerson ) { luckyPerson->employeeSalary *= 1.10; } void main( void ) { Employee *employeePtr; Supervisor *supervisorPtr; employeePtr = new Employee( "Michael Carns", 1000, 200.0 ); supervisorPtr = new Supervisor( "Roseann Krane", 1002, 600.0 ); employeePtr->PrintEmployee(); supervisorPtr->PayRaise( employeePtr ); employeePtr->PrintEmployee(); delete employeePtr; delete supervisorPtr; } Now, we COULD have made only PayRaise() a friend of Employee. However, we chose to make the entire class a friend because supervisors may want to do other things to poor hapless employees :)