C / C++

Complex class


Operator overloading is one type of polymorphism
we can use different operators like =, >, + to act differently according to their operands (arguments)

suppose we will start with the complex class
which has two data members real represents the real part and img for the imaginary part
in addition to setters and getters for the data


Selection Sort


Selection sort is a sorting algorithm

we compare the value in the first position with the value in the second position if the value in the second position is smaller then we swap the two values, otherwise we do nothing, assuring that the first position holds the smaller of both.
then we compare the value in the first position again with the value in the third position and make the same with the fourth and fifth until the last position in the array
after finishing these steps we’d have the smallest value in the first position
we will repeat these steps with the second position comparing it with the third and fourth until the end which would result that the second smallest value in the second position
we will repeat this again and again with every position until the end.

Binary Search


The binary Search is a very efficient search algorithm applies only on ordered arrays

the main concept of binary search is to break the whole array into two halves and then check the value in the middle, if we’re searching for a value bigger than the value in the middle so it’d be at the right part otherwise it’d be in the left part
we will repeat this step in the resulted part by dividing it into two parts and vice versa.
we should make a stop point and it’d be in two cases

  1. if we ended with no division available in our array then it’ll we will notify that we didn’t find the value
  2. if we found the value we will return with the current index


Linear Search


The linear Search is the simplest search algorithm
it’s simply checks for every element in an array one by one in order

we can implement this algorithm in a function that takes 3 arguments

  1. A pointer to the array
  2. The size of the array
  3. The value we want to search for

the return value of the function ‘d be the index (order) of the element in that array or -1 if it’s not exist

Diamond problem and Virtual Inheritance


We ‘d speak about a very common problem happened while using multi-level inheritance
it’s called Diamond problem
we will discuss it using a simple example

Suppose we are in a system that requires to model two entities

  1. Student that has an age and a grade
  2. Worker that has an age and a salary

it’s better to make our design using inheritance ending with three classes

  1. Person class has age attribute
  2. Student class that inherits Person class and has grade attribute
  3. Worker class that inherits Person class and has salary attribute


Building a simple line editor with C


we are trying to build a simple line editor using C
in the first we can define a set of constants represents the ASCII equivalent to the keys we’d use


building a simple menu with C


a sipmple way to build a menu using C

// 1. define constannts
// 2. construct menu items
//        v---------------
// 3. draw menu          |
// 4. check for input    |
//        ^---------------


Go to Top