Login    New User    Forgot Password    Help      

Overloading the Arithmetic Operators


In order to allow operations like Complex c = a+b, in above code we overload the "+" operator. The overloading syntax is quite simple, similar to function overloading, the keyword operator must be followed by the operator we want to overload:


class Complex
        Complex(double re,double im)
        Complex operator+(const Complex& other);
        Complex operator=(const Complex& other);
        double real;
        double imag;
Complex Complex::operator+(const Complex&  other)
    double result_real = real + other.real;
    double result_imaginary = imag + other.imag;
    return Complex( result_real, result_imaginary );
The tutorials in this section are aimed primarily at beginning level programmers, including those who have little to no prior programming experience. Intermediate level programmers will probably also find plenty of tips and tricks that may be of use in improving their programming skills.
This tutorial is broken down into sections, each section containing a number of related subtopics. There is no suggested amount of time that you should spend with each section; progress through the material at a pace that is comfortable for you. Since the concepts from each section tend to build on those from previous sections, it is a good idea to ensure you generally understand each section before you move on.
Before we get started, lets hit on a couple of important goals of these tutorials:
  1. Cover programming topics as well as C++. Traditional textbooks do a pretty good job of teaching the basics of a given programming language, but they often do not cover relevant programming topics that are incidental to the language. For example, books will omit sections on programming style, common pitfalls, debugging, good/bad programming practices, and testing. Consequently, by the time you finish the book, you understand how to program in a language, but you have a ton of bad habits that will come back to bite you later! One of the goals of these tutorials is to make sure that all of these incidental topics are covered along the way, in the sections where it naturally makes sense to discuss them. When you finish, you will not only know how to program in C++, you will know how NOT to program in C++, which is arguably as important.
  2. Provide a lot of examples. Most people learn as much or more from following the examples as they do from reading the text. It is often the case that the text will describe the theory of something, but the examples show how to apply it. This tutorial will endeavor to provide plenty of clear, concise examples to ensure that you are able to apply the concepts youre learning. We will also avoid the twin evils: the magic hand wave (also known as ), where in the interest of space part of an example is omitted, and the unexplained new concept, where a new concept that is integral to the example is introduced without any mention of what it is or how it works. Both of these tend to lead to getting stuck, which makes moving on to the next concept rather difficult.
  3. Provide practice programs. Unlike riding a bicycle, programming topics will fade from memory if not practiced. We strongly encourage you to not only read the examples, but compile and run them to see the result. If you are unsure how they work, use the debugger to walk through them line by line (we will cover how to do that in the appendix). Also, you can learn a lot by modifying the examples and recompiling them to see what happens. However, the best way to learn is to write your own program utilizing the concepts that youve learned. To encourage you to practice, the end of each section will contain some exercises that you can attempt to answer on your own, along with solutions. You can compare your solution against ours to see what we did differently, or, if you get stuck, how we solved the problem. Then you can go back and refocus on the areas you need more work on.
  4. Most importantly: have fun. Programming can be a lot of fun, and if youre not generally having fun, youre not in the right mindset to be programming. Tired or unhappy programmers make mistakes, and debugging code tends to take much longer than writing it correctly in the first place! Often you can save yourself some time by going to bed, getting a good nights sleep, and coming back to a problem in the morning. Tired or unhappy tutorial writers have many of the same issues. :)