Highly used C++ for DATA STRUCTURE Part Ⅱ

Anna Kim
5 min readMar 13, 2019

Operator overloading

In C++ we can put different meanings in operators like ‘+ , - , % ..’ and this is very useful when we want to add different objects. For example, with operator overloading, we can not only add ‘2+3=5’ but also ‘red+blue=purple’ or ‘milk+coffee=latte’ or ‘a(2,3,4) + b(5,6,7) = c{2,3,4,5,6,7} or anything we can think of is possible. This can be done with making the operator into a function type called “operator function”.

Operators allowed to overload
Operators NOT allowed to overload

There are two ways to implement the operator function.1. Implement as an external function and declare as a *friend function in the class. 2. Implement as a member function of the class(more preferred way).

//operator function type
return-type operator+(parameter lists);
two ways to implement operator function

*friend function: An External function which is not included as a member function of the class. (A wandering function which is not included to a particular class)

c = a + b; (+ operator overloading)

Three ways to declare class Love(3rd one is best)

In the Data Structure, ‘Love(int kiss=0; int hug=0) : kiss(kiss), hug(hug) {}’ is universal way to declare function. (remember how to simplify the function into one row with overloading and default parameter)

We can now add two Love objects with Love operator that we defined. Below one is a more detailed example.

Or we can write an operator function to add two Love objects with external function and declare as a friend function.

c = a + b; (++ operator overloading)

class Love {
int kiss;
int hug;
public:
...
Love operator+(Love op2);
};
Love Love::operator(Love op2) {
Love tmp;
tmp.kiss = this->kiss + op2.kiss;
tmp.hug = this->hug + op2.hug
return tmp;
}
int main() {
Love a(1, 10), b(10,6), c;
c = a + b;
c.show() //kiss=11, hug=16
}

c = a += b; (+= operator overloading)

class Love {
...
public:
Love operator+ (Love op2);
}
Love Love::operator+=(Love op2) {
kiss = kiss + op2.kiss;
hug = hug + op2.hug;
return *this; //attention!
}
int main() {
Love a(1,10), b(10,6), c;
c = a += b;
a.show(); //kiss=11, hug=16
c.show(); //kiss=11, hug=16
}

++a (prefix operator overloading)

class Love {
...
public:
Love operator++();
};
Love Love::operator++() {
kiss++;
hug++;
return *this; //return changed object itself
}
int main() {
Love a(1,10), b;
a.show(); //kiss=1, hug=10
b.show(); //kiss=0, hug=0
b = ++a;
a.show(); //kiss=2, hug=11
}

a++ (postfix operator overloading)

class Love {
...
public:
Love operator++(int x); //parameter
};
Love Love::operator++(int x){
Love tmp = *this; //save before the change
kiss++;
hug++;
return tmp; //return before the change
}
int main() {
Love a(1,10), b;
b = a++; //postfix operator
a.show(); //kiss=2, hug=11
b.show(); //kiss=1, hug=10

What if we write with friend function?

//prefix operator ++a
Love operator++(Love& op) {
op.kiss++;
op.hug++;
return op;
}
//postfix operator a++
Love operator++(Love& op, int x) {
Love tmp = op;
op.kiss++;
op.hug++;
return tmp;
}

<< and >> operator overloading

<< operator overloading
ostream& operator<<(const char *);
ostream& operator<<(char);
ostream& operator<<(short);
ostream& operator<<(int);
ostream& operator<<(long);
ostream& operator<<(float);
ostream& operator<<(double);
...

The reason why << operator prints any operand whether it’s int or pointer is because member class ostream inside object cout has many << operator functions overloaded. Likewise, we can define another <<operator function that can print object type as well.

wrap up quiz

Const Object & Member Function

Recall that by putting keyword const, you are not allowed to change the value arbitrarily. (const double PI = 3.14) What if I put the keyword const after a function?

If a member function is a constant function, keyword const after a function declaration means you can read but cannot change the member variables. It prevents member variables from being modified by the function but still calling the value is allowed. That’s why constant member function is called in other words inspector. (non-const member function is called as mutator)

If const is in front of an object

  • none of the member variables are allowed to change
  • only member function with const keyword is allowed to call (Cir.GetArea() o / Cir.SetRadius() x )
Const Object

Template

Overwriting the same code with only different parameter types can be a waste.

void swap(int& a, int& b) {
int tmp;
tmp = a;
a = b;
b = tmp;
}
void swap(double& a, double& b) {
int tmp;
tmp = a;
a = b;
b = tmp;
}
... //waste!

Instead, using template can generalize the function or class which has the same code but only different variables or parameters. This is called generic programming. STL is one of the successful libraries which is oriented towards generic programming. Generic programming focuses on dealing with different types of data with one algorithm and pursues writing one code regardless of the data type.

template <class T> //or
template <typename T> //for declaring template
template function

From generic function, specified functions are created as examples below.

specified version
#include <iostream>
using namespace std;
class Coffee {
int shot;
public:
Coffee(int shot=1) { this->shot=shot; }
int getShots() { return shot; }
};
template <class T>
void myswap(T & a, T & b) {
T tmp;
tmp = a;
a = b;
b = tmp;
}
int main() {
Coffee espresso(1), doubleshot(2);
myswap(espresso, doubleshot);
cout<<espresso.getShots()<<endl; //2
cout<<doubleshot.getShots()<<endl; //1
}
Quiz for template (easy level)
a sum of the array with template
various template

Class Template

Stack is one of the data structures that last data comes out first (Last In First Out; LIFO). Let’s make own stack class using a template member variable.

two principal operations of Stack: Push&Pop
generic stack class

When I want to build a class as a template, make it with a member variable. (T data[100]) MyStack<T>::functionName(); will work. Meanwhile, variable tos doesn’t need to be generic type T since it is an index. (int type is reasonable)

Quiz

These are basic C++ I learned from college class before starting data structure. Next posting will be last part of basic C++: STL. See you soon!

--

--