Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Data Structures and Algorithm Analysis in C++ Code Examples

Queue implemented with STL list

/* Queue implemented with STL list */ #include <iostream> #include <list> #include "dsexceptions.h" using namespace std; template <class Object> class Queue { public: bool isEmpty( ) const; const Object & getFront( ) const; void makeEmpty( ); Object dequeue( ); void enqueue( const Object & x ); private: list<Object> theList; }; template <class Object> bool Queue<Object>::isEmpty( ) const { return theList.empty( ); } template <class Object> const Object & Queue<Object>::getFront( ) const { if( isEmpty( ) ) throw Underflow( ); return theList.front( ); } template <class Object> void Queue<Object>::makeEmpty( ) { while( !isEmpty( ) ) dequeue( ); } template <class Object> Object Queue<Object>::dequeue( ) { Object frontItem = getFront( ); theList.pop_front( ); return frontItem; } template <class Object> void Queue<Object>::enqueue( const Object & x ) { theList.push_back( x ); } int main( ) { Queue<int> q; for( int j = 0; j < 5; j++ ) { for( int i = 0; i < 5; i++ ) q.enqueue( i ); while( !q.isEmpty( ) ) cout << q.dequeue( ) << endl; } return 0; }

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

Test whether container is empty. Returns whether the list container is empty (i.e. whether its size is 0). The C++ list::empty function is used to check whether the list is empty or not. It returns true if the size of the list is zero, else returns false. This function does not modify the container in any way. To clear the content of a list container, see list::clear. No parameter is passed to the function. Function returns true if the container size is 0, false otherwise.

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Delete first element. Removes the first element in the list container, effectively reducing its size by one. pop_front() is an inbuilt function in C++ STL which is declared in header file. pop_front() is used to pop (delete) the element from the beginning of the list container. The function deletes the first element of the list container, means the second element of the container becomes the first element and the first element from the container is removed from the container. This function decreases the size of the container by 1. This destroys the removed element. pop_front() is an inbuilt function in C++ STL which is declared in header file. pop_front() is used to pop (delete) the element from the beginning of the list container. The function deletes the first element of the list container, means the second element of the container becomes the first element and the first element from the container is removed from the container. This function decreases the size of the container by 1.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

Add element at the end. Adds a new element at the end of the list container, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container size by one. The list:push_back() function in C++ STL is used to add a new element to an existing list container. It takes the element to be added as a parameter and adds it to the list container. This function accepts a single parameter which is mandatory value. This refers to the element needed to be added to the list, list_name. This function does not return any value.

A C++ template is a powerful feature added to C++. It allows you to define the generic classes and generic functions and thus provides support for generic programming. Generic programming is a technique where generic types are used as parameters in algorithms so that they can work for a variety of data types. We can define a template for a function. For example, if we have an add() function, we can create versions of the add function for adding the int, float or double type values. Where Ttype: It is a placeholder name for a data type used by the function. It is used within the function definition. It is only a placeholder that the compiler will automatically replace this placeholder with the actual data type. class: A class keyword is used to specify a generic type in a template declaration.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Access first element. Returns a reference to the first element in the list container. The C++ list::front function returns a reference to the first element of the list. Please note that, Unlike the list::begin function, which returns the iterator pointing to the first element, it returns the a direct reference to the same element of the list. Unlike member list::begin, which returns an iterator to this same element, this function returns a direct reference. Calling this function on an empty container causes undefined behavior.

List is a popularly used sequence container. Container is an object that holds data of same type. List container is implemented as doubly linked-list, hence it provides bidirectional sequential access to it's data. List doesn't provide fast random access, it only supports sequential access in both directions. List allows insertion and deletion operation anywhere within a sequence in constant time. Elements of list can be scattered in different chunks of memory. Container stores necessary information to allow sequential access to it's data. Lists can shrink or expand as needed from both ends at run time. The storage requirement is fulfilled automatically by internal allocator. Zero sized lists are also valid. In that case list.begin() and list.end() points to same location. But behavior of calling front() or back() is undefined. To define the std::list, we have to import the <list> header file.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.