NameSpaces “using namespace std “

Leave a comment

actually most of us when beginning to write a C++ code for example we write

using namespace std ;

but most of us doesn’t recognize what does that mean and what’s the Namespace  so this topic basically will talk about namespaces

what’s the NAMESPACE :

it’s and entity that can group inside it a number of classes , object and functions . it’s name is divided into two section ” name ” , ” space ” so actually it’s a space designed for the name of the variable , to prevent the collision of names , suppose you have a function called connect ()  and you want to make a newer version of it inside the same File , and you don’t want to go through alot of naming Connect_new , Connect_newer  …etc so we assign a namespace for  each one

to access elements inside the name space we need to use the operator  ” :: ”  ;

for example

namespace  mynamespace
{
int x , y ;
} 
void main () {
mynamespace::x ;
mynamespace::y ;
}

multiple name spaces in the same file :


more things that make naming collision :

if you are including two header files, those header files have a common function naming inside it which function will get  executed when called ?  acutally nothing will get executed and and error appears calling

 error C2084: function 'connect()' already has a body

the using operator :

using operator is a keyword that introduces a certain namespace to a specific region so when you write

using namespace mynamespace  ;

you can call connect function without using the operator ::

mynamespace::connect () ;
using namespace mynamespace ;
connect() ;    // it should work now  =)

Namespace STD :

all standard libraries inside C++ standard library lies in the name space std , so we  always write the name space std to call the attributes at once  instead of writing

std::cout<<"hellow world " ;
std::cin>> x ;
using namespace std ;
cout <<"now no need to use std:: " ;

that’s it , this is acutally every thing about the explanation of ” using namespace std ; ” for any further questions  don’t hesitate to ask here or DM me  i always check my DMs  @hadyelsahar

Linked Lists in C++

Leave a comment

First of all :

working with linked lists requires a good knowledge about pointers so if you still finding pointers is a hard topic you can practice out this topic
linked listsLinked lists are type of data structures :

In computer science, a linked list (or more clearly, “singly-linked list”) is a data structure that consists of a sequence of nodes each of which contains a reference (i.e., a link) to the next node in the sequence.

source :wikipedia

so the linked list is a type of datastructure that can stand alone or used to create other data structures it consists of Nodes and each node has the reference to the next node ie : “linked to it ”  until the final node where it doesn’t have anything to point to

you can Create a linked list using Classes or Structures but we are using structure and may it be a good thing just to remember how to deal with structures

here’s a little example about how to initialize a Linked list Item and fill any of it’s data

#include <iostream>
#include <string>
using namespace std ;

 struct  listItem {
 string  name  ;  // name of the item 
listItem * next ;  // pointer points to the next node 
 } ;

void main (){
listItem * head ; // pointer will always points on the first node
listItem * myListItem ;   // creating a new instant from ListItem  
myListItem.name = " Greetings Egyptian Geeks "  ; //filling it's data 
cout << myListItem.name ;
head = &myListItem ;  // making the pointer to the adress of the First Item
}

download from here

simple enough !! , it’s a normal Structure  ,  So let’s Move to the NEW part !

Adding New Item to the beginning of the LinkedList

listItem   myListItem2 ;   // creating a new instant from ListItem 
 myListItem2.name = " add me to the beginnning  "  ; //filling it's data 

 myListItem2.next = head ; // making it point to the Next item -that was first- 

 head = & myListItem2 ;  // making the Head pointer points to the new head one

Adding new element to the End

mylastListItem.Next = &newLastItem ;

Addin new element in the Middle

we have two list items listitem1 and listitem2 we want to put middleListItem in the middle we create a new temp pointer to save the adress of listitem2 in it  – as we can’t access it unless by  listitem1.next

temp = listitem1.next ;
listitem1.next = & middleListItem ;
middlelistItem.next = temp ;

Delete elements after a specific number of nodes (n):

temp = head ;
For( i =0 ; i< n ; i++ )
{
temp = temp->next ;  //  hint : there's no something called *temp.next
}
temp = Null ;  // we usually check Temp.next = null then this is the final node

of course this topic doesn’t cover all the operations done on the linked lists , it’s all about the concept but other operations don’t contain any new concepts it’s a Mix between conditions and Pointers

for any suggestions or adds , feel free to post below  or DM me on twitter @hadyelsahar i do always check my DMs

Vectors ,the Dynamic Arrays “D & A episode #2 ”

Leave a comment

what’s a vector ?

simply not precisely we can say that the vector is a Dynamic array of variables, struct or objects in which you Insert data at the end ; and this shows a Great difference with arrays in which arrays was created with a fixed size , this is simply why would we use a vector

declaring vectors :

  • vector <int> myvector ;   >> declaring an empty vector of integers
  • vector <float > myvector (5)  >> declaring a vector of  floats having 5 spaces
  • vector <int> myvector (10,1) >> declaring a vector of  integers having 10 spaces all initialized with value 1

Accessing elements in a vectors :

we will focus on two ways to access elements inside the vector

the normal method ” indexing ” []  :

each element in the vector has an index beginning with index 0 for the first element  1 for the second and …etc till the final element and we use  square brackets to write the index inside it

myvector[1] ; // to select the second element
myvector[n-1] ; // to select the final element among n elements

the function   at ()   :

the vector class has a member method called at()  it takes the index of the element and returns the element having this element

myvector.at(1) ;  // to index the second element in the vector 

Are both  the same ? :

actually no  because in the first method  no index range checking happens in the standard library’s vector so an access out of  the array  index bounds is not caught by the compiler for example

vector<int> myvector(5,1) ; cout<< myvector[6] ;

this might be caught by compiler as an error and might not , maybe rubbish is printed

(Range check- ing can be done by using at method ;

ie : myvector.at ( i  )  is the same as a  [  i  ] ,  except that an exception is thrown  if  i  is out-of-bounds

How to fill  a vector :

dealing with vectors is the same as dealing with arrays so to fill a certain element in a vector you say
myvector[5] = 13 ;

Inserting and Deleting Elements inside the vector :

  • push_back() method is used to insert a new row in the vector  // it takes the value of the row as an argument  ex : myvector.push_back(15) ;

  • pop_back() method is used to remove the last row in the vector

click the image for larger view

 

Getting the size of  a vector  & changing it :

  • size() method is used to return the size of the vector = the number of elements in the vector
  • resize () takes two arguments  the new size as and the value of the new added elements  , if you didn’t add the second argument the new elements will have a value of zero by default

Hello world program in C++ & VS2010 “D&A episode #1”

1 Comment

before going into code we need first to settle some important informations

C++ is originally called C with classes so it’s really a subset of C  , however C++ have a lot more Features than C ; including some basic things like inputs and outputs

do you mean that Scanf () and printf() and getche()  works in C++ ? yeah they do  =)  actually all C functions do work on C++

Great  so that means we don’t need to read about functions , arrays and all the C stuff ,,, yeah however we will elaborate some important things and offcourse things related to data structure

so let’s  code :

in the begining this is a let’s describe how to configure visual studio for a hello world program ,and a sample code to make a simple hello world program

the source code project  from here : Download

  • open new project  cntrl + shift + N
  • click new win 32 console application
  • click on console application  and empty project
  • then on the right click on the solution explorer
  • if you cant find the solution explorer click show it from View next to file in the menu  or click  cntrl +alt + L
  • and write the following in the  main file

  • #include <iostream>
    using namespace std ; 
    void main () { 
     cout << "hello world " ; 
    }
  • click cntrl +f5 to run

NOTES :

  • #include <iostream>    // this to include the libraries for console output  .. has alot of similarities with stdio.h “standard input and output” library in C
  • using  .. this keyword in C++ is like we say assume that we are using , the following so we write using namespace std  , in order not to say  std::cout  and write cout  alone , (we will explain what’s the name space later on )
  • cout is like printf  it takes a string and prints it into the console

How to get a free Microsoft Visual studio 2010

2 Comments

microsoft visual studio 2010

Hello to my new  series of  lessons  “data structure and algorithms using C++ and Ms VisualStudio2010 “

before going through algorithms and Data Structure or even C++ we need First to install an IDE : “integrated development environment ”

is a software application that provides comprehensive facilities to
 computer programmers for software development    src : wikipedia 

the most of  IDEs  mainly consists of  :

  • text editor
  • compiler
  • debuggerr
  • design tools

the IDE main benefit is it’s integrates all the processes of writing code in one program which is the “IDE” as well as configuration to the files and writing a preparation code to let developers start developing at once

some examples of an IDE ,

in the early ages  people used  the command line tools

lately :

Microsoft visual studio  on windows platform  , Eclipse , Netbeans , ActiveState Komodo .

how to get a free microsoft visual studio :

for any user :

you can download visual studio 2010  express version it’s for free but it has  less feature than the other version but for a beginner developer , it doesn’t matter  : http://www.microsoft.com/express/Downloads/#2010-Visual-CPP

and here’s a little comparison about different visual studio version

for students  :

if you have a  live@edu email , -if you don’t have one contact your campus –  you can logi nto Microsoft Dream Spark then you could download a punch of developers stuff including microsoft visual C++ express which is already free as well as Microsoft visual studio 2010  professional edition which is not .

for students in computer science and computer engineering   :

contact your campus MSP –microsoft student partnersfor an MSDNAA account this MSDNAA account provides alot of Free Microsoft stuff only for students that helps them in their study like : windows 7 professional , Ms visual studio professional , expression studio version 3 & version 4  ….etc . if you already have one you could login through here : bit.ly/MSDNAA_account   (only for ASU , engineering Campus  )

….. to be contiued