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++ > Beginners Lab Assignments Code Examples

Hello World Program in C++

/* Hello World Program in C++ */ /* Multiple line comment */ #include<iostream> //Single line comment using namespace std; //This is where the execution of program begins int main() { // displays Hello World! on screen cout<<"Hello World!"; return 0; } /* 1. Comments - You can see two types of comments in the above program Comments as the names suggests are just a text written by programmer during code development. Comment doesn't affect your program logic in any way, you can write whatever you want in comments but it should be related to the code and have some meaning so that when someone else look into your code, the person should understand what you did in the code by just reading your comment. Now if someone reads my comment he or she can understand what I did there just by reading my comment. This improves readability of your code and when you are working on a project with your team mates, this becomes essential aspect. 2. #include<iostream> - This statements tells the compiler to include iostream file. This file contains pre defined input/output functions that we can use in our program. 3. using namespace std; - A namespace is like a region, where we have functions, variables etc and their scope is limited to that particular region. Here std is a namespace name, this tells the compiler to look into that particular region for all the variables, functions, etc. I will not discuss this in detail here as it may confuse you. I have covered this topic in a separate tutorial with examples. Just follow the tutorial in the given sequence and you would be fine. 4. int main() - As the name suggests this is the main function of our program and the execution of program begins with this function, the int here is the return type which indicates to the compiler that this function will return a integer value. That is the main reason we have a return 0 statement at the end of main function. 5. cout << "Hello World!"; - The cout object belongs to the iostream file and the purpose of this object is to display the content between double quotes as it is on the screen. This object can also display the value of variables on screen(don't worry, we will see that in the coming tutorials). 6. return 0; - This statement returns value 0 from the main() function which indicates that the execution of main function is successful. The value 1 represents failed execution. */

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

#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.

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.

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.

The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment

Program sample takes the values of two large numbers as input and displays the computed value node with node in the resultant Linked List. Result of "subtraction" for two numbers


To print "Pascal Triangle" in C++, you have to enter the Number of Line. So to Print "Pascal Triangle", you have to use three For Loops as shown here in the C++ Programming samples


To convert binary to octal in C++, you have to ask to the user to enter any number in binary to "convert it into octal" to display equivalent value in octal on the screen as shown in code







This is a C++ Program to find "shortest path". Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like "Prim's MST", we generate a SPT with given