Category Archives: C++

How to count numbers from string ?



C++ Overloading (Operator and Function)

Function Overloading in C++

Operator Overloading in c++

we would have to pass two arguments for each operand as follows:

Box operator+(const Box&, const Box&);



2016-01-07 01_08_59-C__Users_ZakiHP_Documents_GitHub_c++_operator-overload.exe


There are some examples for various operators and list.You can go to reference link for this.Here I am pausing this as I am going to switvh to java as java is good for OOP. But I will be back to this C++ series if I get my free time again. 🙂

And If I get time I have a plan to start blogging about C++ vector, STL and Template library that is very helpful for contest programming.

C++: Inheritance

One of the most important conecpts in object oriented programming is that of inheritance.Inheritance allows us to define a class int terms of another class,which makes it easier to create  and maintain an applicatrion.This also provides an opportunity to resue code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class.This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the is a relationship.For example, mammal Is-A animal,monkey Is-A mammal hence monkey Is-A animal .And as well as and so on.

Base & Derived classes:

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. T o define a derived class we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:


where access specifier is one public,protected or private, and base-class is the name of a previously defined class.If the access-specifier is not used, then it is private by default.

consider a base class Shape and it s derived class Rectangle as follows:


2016-01-04 23_46_23-inheritance-derivedclass.cpp - Code__Blocks 13.12

Access Control and Inheritance:

A derived class can access all the non-private members of its base class.Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to who can accesss them in the following way:

Access                    Public    Protected  Private
Same Class              yes          yes             yes
Derived classes      yes         yes             no
Outside classes      yes         no             no

A derived class inherits all base class method with the following exceptions:
– Constructors, destructors and copy constructors of the base class.
– Overloaded operators of the  base class.
– The friend function of the base class.

Type of Inheritance:

When deriving a class from a base class the base class may be inherited through public,protected or private inheritance.The type of inheritance is specified by the access-specifier as explained above.

We hardly use protected or private inheritance but public inheritance is commonly used.While using different type of inheritance following rules are applied:

Public Inheritance: When deriving a class freom a public base class,public members of the base class become public members of the derived class and protected members of the base class become the protected members of the derived class .A base class private members are never accessible directly from a derived class, but can be acccessible through calls to the public and protected members of the base class.

Protected Inheritanc:When deriving from a protected base class,public and protected members of the base class become protectecd members of the derived class.

Private Inheritance:When deriving from a private base class,public and protected members of the base class become private members of the derived class.

Muktiple Inheritances:

A C++ class can inherit members from more than one class and here is the exrended syntax:

class derived-class:access basA,access baseB,access baseC……

Here the example goes:

2016-01-05 04_10_00-C__Users_ZakiHP_Documents_GitHub_c++_inheritance-derivedclass-multiple.exe

C++ OOP: Constructor Destructor

The class constructor:

A class constructor is a special member function of a class that is executed whenever we create neww objects of that class

A costructor will have exact same name as the class and it does not have any return type at all, not even void.Constructors can be very useful for setting initial values for certain member variables.

Following example wxpalins the concept of constructor:

Parameterized Constructor:

A default constructor does not have any parameter, but if you need, a constructor can have parameters.This helps yout to assign initial value to an object at the time of its creation as shown in the example:

2015-09-30 14_02_06-_D__desktop_desktop september_oop_param.exe_

Using Initialization Lists to Initialize Fields:

In case of parameterized constructor, you can use following syntax to initialize the fields:

Above syntax is equal to the following syntax:

If for a class C, you have multiple fields X,Y,Z etc. to be initialized, then can be use same syntax and separate the fields by comma as follows:


The class Destructor:

A destructor is a special member fucntion that is executed whenever an object of it’s class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

A destructor will have exact same name as the class prefixed with a tilde(~) and it can neither return a value nor can it take any paramaters.Destructor can be very useful for releasing resources before coming out of the program liek

2015-09-27 00_53_49-_D__desktop_desktop september_oop_destructor.exe_

Sorting Algorithm : Insertion Sort

From this two video I tried to re learn and implement the algorithm with pseudo code.


Implemented Code in C:

2015-08-07 13_30_30-C__Users_User_Desktop_insertionsort.exe

Here ary[] means array 🙂


Harvard CS50 course:

GeeksForGeeks Algo Implement(Not Worked Yet):
Practice Problem:

CLRS Theory:

Simple Implementation of Insertion Sorting(Idea Sanfoundry):

It is helpful:

Time complexity of Insertion Sort:

  • Worst case complexity : O(n^2)
  • Best case complexity :    O(n)
  • Average case complextiy:  O(n^2)Reference:

How to calculate complexity there is a example available at the upper video “Lecture 7”

and there are some links

Here a good presentation about this sorting


For descending order I took help from:
Here they change the comparison value for descending at while loop condition

Another good website:

Time complexity of insertion sort when there are O(n) inversions?

For some theory revise this video is good:

c++ precision

C++ precision example:

In this link everything explained clearly….You can check

And basically if we want to make just like C in C++ fixed value after decimal point then it can be like this

In C:

In C++:

Both C and C++ will give the same result output:3.55

C++ OOP:

C++ is a OOP Version of C…
A class is a blueprint of an object…..Basically a object is created from class

c++ class define:



c++ object define:

Here we are declaring two objects of a class Box

Box box1; // declare box1 of type Box
Box box2; //declare box2 of type Box

Basic example of OOP here :

C++ Class Member Functions:

2015-09-26 01_01_14-_D__desktop_desktop september_oop_access.exe_

We use this process to avoid direct access to the members of the class

Another technique, we have used scope resolution operator :: to define the same function outside the class that means if we want to define class in outside from the class then we have to use scope resolution operator.


inside the class we define this like that

and the member function is called just like this in a object from a class

C++ Access Modifiers:

Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to access directly the internal representation of a class type .The access restriction to the class members is specified by the labeled public,private and protected sections with class body.The keywords public ,private and protected are called access specifiers.


A class can have multiple public,protected or private labeled sections.Each section remains in effect until either another section label or the closing right brace of the class body is seen.The default access for members and classes is private.

The public members:

A public meber is accessible from anywhere outside the class but within  a program.You can set and get the value of public variables without any member function as shown in the following example:

2015-09-26 02_34_55-access.cpp - Code__Blocks 13.12


The private members:

A private member variable or function cannot be accessed or even viewed from outside the class.Only the class and friend functions can access private members.

By default all the members of a class would be private, for example in the following class width is a private member, which means until you label a member, it will be assumed a private member.

Practically we define data in private section and related functions in public section so that they can be called from outside of the class as shown in the following program.

The protected members:

A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.

We will discuss derived classes and inheritance in next .But here I have derived one child class names SmallBox from a parent class Box

Following example is similar to above example and here width member will be accessible by any member function of its derived class SmallBox


Structure in C++

struct Books{

char title[50];
char author[50];
char subject[100];
int book_id;


Structure as Function arguments


Pointers to structures:

struct Books *struct_pointer;


To access the members of the structure  I have to use -> operator as follows


With typedef:

There are some arrors in teh code  I will fix thsi later..


c++ input/output stream

I/O stream:




C++ References

 References as parameters
References as Return Value:



C++ Strings

Actually C++ strings are two types

1. Traditional C style character string
2. String class type Introduces in Standard C++

Example of C-style string:

Some builtin strings function came from language C

strcpy(s1,s2); //copies s2 into s1

strcat(s1,s2);  //concatenates string s2 at the end of the string s1

strlen(s1); //returns the length of the string s1..length means actually size that means how many letters is in the string with white spaces and spaces

strcmp(s1,s2); //returns 0 if s1==s2 s1 and s2 are same if less than 0 s1<s2 and greater than 0 s1>s2

strchr(s1,ch);returns a pointer at the first occurence f character ch in string s1

strstr(s1,s2); //retruns a pointer to the first occurence of string s2 in string s1

Example here:


In C++ Standard String Class:

Here we are using the library header #include<string> that is c++ built in header for strings manipulation…

C++ Pointer

I tried to clear here my logic for pointer basic


Actually in c++ pointer can be declare like this

type  *var_name;


int *ip;
double *dp;
float *fp;
char *ch;


C++ NULL Pointers

It actually represent no where

int *zaki=0;
int *dhaka=NULL;

Both the examples meaning the same attitude of the c++

It actually default by the operating systems

Here is one example:

We can also define by this

if(ptr) //it will proceed if p is not null
if(!ptr) //it will proceed if p is null

Pointer arithmetic

Incrementing a pointer:

Decrementing a pointer:

Comparison with pointer:


C++ pointer vs arrays:

C++ Array of pointers

and also can store data like this with pointer and array mutually

C++ Pointer to Pointer:


C++ Array

declare arrays:

arraytype arrayName[arraySize]; //basic syntax


int zakiLive[10];

Initialize arrays:

int zakiLive[5]={4,5,6,8};

It means value 8 is in 3rd index in array zakiLive as index in array count from 0 and it goes on..
Here is an example of array

C++ Arrays have some types:

Multi-Dimensional Arrays:


syntax can be like this

array_data_type  array_name[size1][size2][size3]……..[sizeN];

Two Dimensional Arrays(It is one kind of multidimensional arrays):

type arrayName[x][y];

Initialize Two Dimensional Arrays:

int a[3][4]={

We can also write equivalent like this

int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};

We can accesss Two-Dimensional Array elemets by

int valueArray=a[2][3];

Here is a code

C++ Pointer to an Array

Here it is happeing like this shown above.

There are another 2 types of C++ array functioning with pointer

Passing Arrays to Function and Returning Array From Functions

Now let discuss in details

Passing Arrays to Function:

What we know from the function  synatx is

But it’s not possible to pass a full array to the argument by using an index number..But we can pass an array by using without an index..This process can be done by three steps

Formal Parameters as a pointer

Formal Parameters as a size array as follows

Formal Paramaeters as an unsized array as follows

The example given here with array without size


Return Array from Functions:

We will discss this later more In Sha Allah 🙂

C++ Numbers

defining numbers in c++


Mathematical Functions:

C++ has some predefined library functions for mathematical calculation.We have to use the header #include<cmath> for using this




C++ Nested Switch , Nested If

Nested If,Else If,Else:


Just another example here:

Nested Switch Statement: