 # C++ Programming Code Examples

## C++ > Beginners Lab Assignments Code Examples

### C++ Program to Calculate Difference Between Two Time Period

/* C++ Program to Calculate Difference Between Two Time Period In this program, user is asked to enter two time periods and these two periods are stored in structure variables t1 and t2 respectively. Then, the computeTimeDifference() function calculates the difference between the time periods and the result is displayed on the screen from the main() function without returning it (call by reference). */ // Computes time difference of two time period // Time periods are entered by the user #include <iostream> using namespace std; struct TIME { int seconds; int minutes; int hours; }; void computeTimeDifference(struct TIME, struct TIME, struct TIME *); int main() { struct TIME t1, t2, difference; cout << "Enter start time." << endl; cout << "Enter hours, minutes and seconds respectively: "; cin >> t1.hours >> t1.minutes >> t1.seconds; cout << "Enter stop time." << endl; cout << "Enter hours, minutes and seconds respectively: "; cin >> t2.hours >> t2.minutes >> t2.seconds; computeTimeDifference(t1, t2, &difference); cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":" << t1.seconds; cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds; cout << " = " << difference.hours << ":" << difference.minutes << ":" << difference.seconds; return 0; } void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *difference){ if(t2.seconds > t1.seconds) { --t1.minutes; t1.seconds += 60; } difference->seconds = t1.seconds - t2.seconds; if(t2.minutes > t1.minutes) { --t1.hours; t1.minutes += 60; } difference->minutes = t1.minutes-t2.minutes; difference->hours = t1.hours-t2.hours; }

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

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.

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

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

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.

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

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.

In discrete mathematics, tree rotation is an operation on a binary tree that changes the structure without interfering with the order of the elements. A tree rotation moves one

To convert decimal number to octal number in C++ programming, you have to ask to the user to enter the decimal number to convert it into octal number to display the equivalent

Add Edge to connect "v" and "w". A recursive function to print BFS starting from s. Returns reverse ('or transpose') of this graph. Check if graph is connected. Print graph is connected.