Welcome guest! Click here to register or login.
logo

<< Tutorial 12
Tutorial 14 >>

Classes & Objects II

***Please register FREE to rate this tutorial***


Topics
Member functions
Operator overloads
This keyword
Examples
Example 1

Member functions

Member functions were discussed in some detail in the functions tutorial. By definition, a member function is a function of a class or object that is accessed by the dot operator. They are declared in the public section of the class and are written just like regular functions.

Just like before, the Rect class has two member functions called Area() and Perimeter(). These functions are accessed with the dot operator outside the class (here, in then main() function) and preform appropriate calculations.

Thinking about a rectangle, let's add another member function called triple() which will simply triple both sides of the rectangle. This is just for you to see how member functions work.

void triple(){
 s1 *= 3;
 s2 *= 3;
}

Operator overloads

In C++, there are hundreds of definitions of operators relating to addition, subtraction, division, logic, pointers etc... Now, let's say that we have two objects of type Rect and we wanted to copy the values from one rectangle to another. WHOOPS! ERROR! C++ does not know what a Rect is since it is not a primitive data type. No worries, we can do what is called operator overloading where we can define our own operator action. Below is what I was referring to:

Rect r(3,5), s(2,4);

r = s; //ERROR HERE!

Here is the definition of an operator overload:

type operator sign (parameters) {
  /*...*/
}

where in the above, type is the return type of the operator, sign is the appropriate sign related to the operator ( +, -, * etc...) and parameters are the right hand side of the operator.

Now that we know how to write the overload of the operator, let's actually overload the assignment operator ( = ). Add this code after all the member functions in the class:

void operator=(const Rect r){
	s1 = r.s1;
	s2 = r.s2;
}

The reason the parameter is constant is that we are not actually changing the value on the right hand side. We are just getting them so we can place them in the left hand side (which relating to an above snippet would be the Rect r object).

Now, add this to the main function at the end:

Rect s(2,4);

//check the overloaded operator:
r = s;

//make sure it worked:
cout << r.getS1() << " " << r.getS2() << endl;

The above snippets will compile and work when used. This now shows how to assign values. But what if we wanted to add two rectangles together? Simple, overload the addition operator. Again, place this snippet after the member functions:

int operator+(Rect r){

	int sum = 0;

	sum = sum + Perimeter(s1,s2);
	sum = sum + Perimeter(r.getS1(), r.getS2());

	return sum;
}

This makes use of the member function Perimeter which is called twice, making use of the current sides of the class and the other rectangles sides. The return type is int because you are returning the sum of the sides.

Finally, here is the complete class with all everything from both tutorials:

Download source code here (Right click - Save Tagret As...)

#include <iostream>
using namespace std;

class Rect{

private:
	//representing the 2 sides of the rectangle.
	int s1, s2;

public:

	//constructors:
	//default constructor:
	Rect(){
		s1 = 1;
		s2 = 1;
	}

	//non default constructor:
	Rect(int a, int b){
		s1 = a;
		s2 = b;
	}

	//accessors:
	int getS1(){ return s1; }
	int getS2(){ return s2; }

	//mutators:
	void setS1(int s){
		if(s <= 0)
			return;

		s1 = s;
	}
	void setS2(int s){
		if(s <= 0)
			return;

		s2 = s;
	}

	//member functions:
	int Area(int s1, int s2){
		return(s1*s2);
	}

	int Perimeter(int s1, int s2){
		return(s1*s1 + s2*s2);
	}

	void triple(){
		s1 *= 3;
		s2 *= 3;
	}

	void operator=(const Rect r){
		s1 = r.s1;
		s2 = r.s2;
	}

	int operator+(Rect r){

		int sum = 0;

		sum = sum + Perimeter(s1,s2);
		sum = sum + Perimeter(r.getS1(), r.getS2());

		return sum;
	}

};

int main(){

	Rect r(3,5), s(2,4);

	int s1 = r.getS1();
	int s2 = r.getS2();

	cout << "Area of sides 3, 5 is: "
		<< r.Area(s1,s2) << endl;

	cout << "Perimeter of sides 3, 5 is: "
		 << r.Perimeter(s1,s2) << endl;

	r.triple();

	cout << "Tripled sides: s1= " << r.getS1()
		 << " s2 =" << r.getS2() << endl;

	int sum = r + s;

	cout << "The sum is: " << sum << endl;

	r = s;

	cout << r.getS1() << "  " << r.getS2() << endl;
	return 0;
}

This keyword

The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself.

As good use of the this keyword is to check if a parameter is a pointer to the object itself. Below is a possible function for the Rect class above:

bool myself(Rect& r){
     if(&r == this) return true;
     else return false;
}

The above function will check if the address of the parameter r is the same as the address of the current object (represented by this).

Another use of the this keyword is for the assignment operator overload. We could have rewritten the Rect operator= like this:

Rect& operator=(const Rect& r){
      s1 = r.getS1();
      s2 = r.getS2();
      return *this;
}

Notice that the return type is a reference to the Rect class and not void as before. The parameter is also constant since again we are nopt changing the right hand side.


<< Tutorial 12
Tutorial 14 >>