# C++ Programming Code Examples

## C++ > Data Structures Code Examples

/* Add and subtract two polynomials (Using Linked List) */ #include<iostream.h> #include<conio.h> #include<process.h> // Creating a NODE Structure struct node { int coe,exp; // data struct node *next; // link to next node and previous node }; // Creating a class Polynomial class polynomial { struct node *start,*ptrn,*ptrp; public: void get_poly(); // to get a polynomial void show(); // show void add(polynomial p1,polynomial p2); // Add two polynomials void subtract(polynomial p1,polynomial p2); //Subtract2 polynomials }; void polynomial::get_poly() // Get Polynomial { char c='y'; ptrn=ptrp=start=NULL; while(c=='y' || c=='Y') { ptrn=new node; ptrp->next=ptrn; if(start==NULL) start=ptrn; ptrp=ptrn; cout<<" Enter the coefficient: "; cin>>ptrn->coe; cout<<"Enter the exponent: "; cin>>ptrn->exp; ptrn->next=NULL; cout<<"Enter y to add more nodes: "; cin>>c; } return; } void polynomial::show() // Show Polynomial { struct node *ptr; ptr=start; while(ptr!=NULL) { cout<<ptr->coe<<"X^"<<ptr->exp<<" + "; ptr=ptr->next; } cout<<" "; } void polynomial::add(polynomial p1,polynomial p2) // Add Polynomials { struct node *p1ptr,*p2ptr; int coe,exp; ptrn=ptrp=start=NULL; p1ptr=p1.start; p2ptr=p2.start; while(p1ptr!=NULL && p2ptr!=NULL) { if(p1ptr->exp==p2ptr->exp) // If coefficients are equal { coe=p1ptr->coe+p2ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; p2ptr=p2ptr->next; } else if(p1ptr->exp>p2ptr->exp) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; } else if(p1ptr->exp<p2ptr->exp) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; } ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } // End of While if(p1ptr==NULL) { while(p2ptr!=NULL) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } else if(p2ptr==NULL) { while(p1ptr!=NULL) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } } // End of addition // Subtract two polynomials void polynomial::subtract(polynomial p1,polynomial p2) // Subtract { struct node *p1ptr,*p2ptr; int coe,exp; ptrn=ptrp=start=NULL; p1ptr=p1.start; p2ptr=p2.start; while(p1ptr!=NULL && p2ptr!=NULL) { if(p1ptr->exp==p2ptr->exp) // If coefficients are equal { coe=p1ptr->coe-p2ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; p2ptr=p2ptr->next; } else if(p1ptr->exp>p2ptr->exp) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; } else if(p1ptr->exp<p2ptr->exp) { coe=0-p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; } ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } // End of While if(p1ptr==NULL) { while(p2ptr!=NULL) { coe=0-p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } else if(p2ptr==NULL) { while(p1ptr!=NULL) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } } // End of subtraction int main() { clrscr(); polynomial p1,p2,sum,diff; cout<<"First Polynomial. "; p1.get_poly(); cout<<" Second polynomial. "; p2.get_poly(); clrscr(); cout<<" The First polynomial is: "; p1.show(); cout<<" The second polynomial is: "; p2.show(); cout<<" The sum of two polynomials is: "; sum.add(p1,p2); sum.show(); cout<<" The difference of two polynomials is: "; diff.subtract(p1,p2); diff.show(); getch(); return 0; }

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

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.

Allocate storage space. Default allocation functions (single-object form). A new operator is used to create the object while a delete operator is used to delete the object. When the object is created by using the new operator, then the object will exist until we explicitly use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is not related to the block structure of the program.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

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

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

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.

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.

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.

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.

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.

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

In the C++ Programming, The static keyword allows a variable to maintain its value among different function calls. If the value of a static variable changes when the variable has been

C++ program 'merge two files' and store the content of both file into another file. First file name and second file name (say file1.txt and file2.txt), then Third file name that is used to

To get the IP address of your computer in c++ programming, use the function system() and place the command 'ipconfig' after providing the full path of 'System32' i.e., C:\\Windows\\

All the "variables" must be declared before to use or initial statements of the block or main or function or global. Variables should specify with data type. And it binds a "data type" and

C++ Program to get all the unique partitions of an integer such that addition of a partition results an integer. An integer n, generate all "possible unique" ways to represent n as sum