








































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









































































C is a Structure oriented programming language C++ is a Object Oriented Programming language It is based on function It is based on data Larger programs divided into function Larger programs are divided into different Objects One or more function share the global data Each and every object can have data and function and function can access the data within the object only The data can be move freely from one function to another function External function cannot access the data object within the class We cannot add the data on function easily We can add data and function into class easily Top down approach is used to design a program Bottom up approach is used to design a program It cannot contain inheritance, polymorphism, data abstraction and data encapsulation It contain inheritance, polymorphism, data abstraction and data encapsulation Scanf and Printf function can be used to input and output function in program Cin and Cout can be used to input and output function in program It cannot support overload and virtual function It support overload and virtual function It cannot support inline and friend function It can support inline and friend function The declaration can be used in first statement of program The declaration can be used anywhere in the program
C++ is an Object Oriented Programming Language. In 1980, Bjarne Stroustrab was developed C++. Initially it was named as C with Class. It was developed by AT & T Bell Lab at murray hill, in USA He was strong supporter of simula 67 and Algol 68. In 1983, he was combined the best feature of Simula 67, Algol 68 to C with Classes and that it was named as C++ in 1983. C++ is the extended version of C C++ is an superset of C The C++ can contain the following additional features:
It is a freeform language. It is flexible. It is OOP language. It can support many concepts of OOPs It provides quick performance compare than any other Object Oriented Programming Language. It can contain small size code. It can be understood easier than any other programming language.
The important object oriented features are namely:
Java It was developed by Patrick Naughton, James Gosling, Chris Warth, Mike Sheridan and Ed Frank at Sun Microsystems. It is an object oriented programming language and supports maximum OOP characteristics exist. Its structure is like C and C++, but it easier than C++. It excludes few confusing and unsafe features of C and C++ like pointers. It allows client/server programming. It is used for Internet programming. The Java programs are downloaded by the client machines and executed on different types of hardware. This portability is achieved by translation of java program to machine code using compiler and interpreter. The Java compiler converts the source program to JVM. The compiler Java program is called as byte code. The Java interpreter translates byte code into the object code.
The OOP technology is changing the style of software engineers to think, analyze, plan and implement the software. The software developed using OOP technology is more efficient and easy to update. It have standard class library. The users in their program can reuse the standard class library. It saves lot of time and coding work. The most popular application of object oriented programming is windows. There are several windowing software based on OOP technology. Following are the areas for which OOP is considered.
It is a flexible language. Lengthy programs can be easily controlled by the use of C++. It permits us to create hierarchy associated objects and libraries that can be useful to other programmers. C++ helps the programmer to write bug-free program, which are easy to maintain.
The C++ supports number of I/O operations to perform read and write operations. These functions help the user to work with different types of devices such as keyboard, disk, tape drivers, etc. Stream is an inter-mediator between I/O devices and the user. The stream is a flow of data, measured in bytes, in sequence. If data is received from input devices in sequence, then it is called as source stream. When the data is passed to output devices, then it is called as destination stream. This process of flow of data is also known as encapsulation through streams. The data in source stream can be used as input data by the program. So, the source stream is also called as input stream. The destination stream that collects output data from the program is known as the output stream. The input stream receives data from keyboard or storage devices such as hard disk, floppy disk.
A standard stream is a pre-connected stream provided to a computer program by its environment. C++ comes with four predefined standard stream objects that have already been set up for your use.
A buffer is a block of RAM that is used to hold input or output temporarily. This can speed up I/O considerably because of the various overheads incurred when transferring data. Writing data to the disk is very costly. The read/write operation with disk takes a long time. When these operations are carried out, the execution of the program will be slow for few seconds. If the program involves many read and write operations, the program execution speed will be slower. To avoid this problem, the stream provides buffer. It holds the data temporarily. The input data is passed to the stream buffer. The data is not written to the disk immediately till the buffer fills. When the buffer is completely filled, the data is written to the disk.
C++ streams are based on class and object theory. C++ has number of classes that work with console and file operations. These classes are known as stream classes. It consists of 3 subclass
Ios 1. It is an input and output stream class
abc abc
C++ provides various formatted I/O functions for formatting the output. They are of 3 types:
The ios class contains the setf() member function. The flag indicated the format design.
unsetf is used to clear the flags. Syntax: cout.setf(v1, v2); cout.unsetf(v1); v1, v2 are flags There are 3 types of bit fields.
Syntax: long setf(long v1) where v1 is a variable name of the flag.
The smallest individual unit in a program is known as tokens The program contains various components. The compiler identifies them as tokens. Tokens are classified into:
data_type variable_name; Example: int a; Variable initialization Before using a variable it is essential to initialize it. Assigning a value to the variable is called as initialization. When a value is assigned to the variable, the garbage value is removed and replaced with the given value. Initialization can be done at the place where it is declared or anywhere in the program before their use. Variables declared can be assigned using assignment operator ‘=’. Syntax: data_type variable_name = constant; Example: int a=10; Dynamic initialization The declaration and initialization of variable in a single statement at any place in the program is called as dynamic initialization. It is the process carried out at run-time. Constants The values that do not change during execution of a program. The constant have 2 types
void main() { int x=2, p; cout<<”Address of x=”<<&x; p=&x; cout<<”value of x=”<<p; } OUTPUT Address of x= Value of x=
It is a self-contained block or a sub-program of one or more statements that perform a special task when called. The C++ functions are more civilized than C. void main() { void show(void); show(); } void show() { cout<<”Infunction show()”; } OUTPUT In function show()
It is a collection of elements of similar data type in which each element is located in separate memory location. Eg: int b[4]; void main() { int b[4]={2,4,3,7}; cout<<”b[0]=”<<b[0]<<endl; cout<<”b[1]=”<<b[1]<<endl; cout<<”b[2]=”<<b[2]<<endl; cout<<”b[3]=”<<b[3]<<endl; } OUTPUT b[0]=2 b[1]=4 b[2]=3 b[3]=
The referencing operator (&) is used to define referencing variable. A reference variable prepares an alternate name for previously defined variable. Syntax: data_type & reference variable name = variable name; Example: int qty=10; int &qt = qty; Here, qty is already declared and initialized. The second statement defines an alternative variable name, that is, qt to variable qty. If both variables printed displays the same value. Any change made in one of the variable causes the change in both the variables. qt=qt*2;
User defined data types are structure and classes, union, and enumerator
Struct is a keyword and used to combine variables of different data types into a single record. Syntax: struct
struct my_friend { char fname[80], phone[80]; int age, height; }friend A;
The class is a new keyword introduced in C++. Its use is the same as struct keyword. Syntax:
A union is same as compared to a struct, but the only difference is that it allows the user to declare variables that share the same memory space. The union requires bytes that are equal to the number of bytes required for the largest members. Syntax: union[
The enum keyword is used to create a new data type and also create variable for that data type. Syntax: enum variable_name{value1, value2}; Eg: enum logical(true, false}; Here logical is a new data type and true, false is a variable for logical data type. Here the true take the value 0 and false take the value 1. When the value cannot be specified suppose we can change the value of enumerator by using assignment operator. Eg: enum logical{true=2, false=4};
A program is nothing but the execution of sequence of one or more instructions. Quite often, it is desirable to alter the sequence of the statements in the program Depending upon certain circumstances. (i.e., we have a number of situations where we may have to change the order of execution of statements based on certain conditions) (or) Repeat a group of statements until certain specified conditions are met. This involves a kind of decision making to see whether a particular condition has Occurred or not and direct the computer to execute certain statements accordingly. Based on application, it is necessary / essential (i) To alter the flow of a program (ii) Test the logical conditions (iii) Control the flow of execution as per the selection these conditions can be placed in the program using decision-making statements.
The if statement is a powerful decision making statement and is used to control the flow of execution of statements.
int n; clrscr(); cout<<“Enter a number:”; cin>>n; if( (n%2)==0 ) cout<<”\n The given number is EVEN ”; else cout<<“\n The given number is ODD ”; getch( ); }
Enter a number: 24 The given number is EVEN
Using of one if-else statement in another if-else statement is called as nested if-else control statement. When a series of decisions are involved, we may have to use more than one ifelse statement in nested form.
if ( Test Condition 1) { if ( Test Condition 2) { Statement -1; } else { Statement -2; } } else { if ( Test Condition 3) { Statement -3; } else { Statement -4; } } /* end of outer if-else */ If Test Condition-1 is true then enter into outer if block, and it checks Test Condition- 2 if it is true then Statement-1 executed if it is false then else block executed i.e Statement-2. If Test Condition -1 is false then it skips the outer if block and it goes to else block and Test Condition-3 checks if it is true then Statement-3 executed, else Statement-4 executed
void main() { float a,b,c; cout<<“Enter Three Values:”; cin>>a>>b>>c; cout<<“\n Largest Value is:” ; if(a>b) { if(a>c) cout<< a; else cout<< c; } else { if (b>c) cout<<b; else cout<<c; } }
Enter three values: 9.12 5.34 3. Largest Value is: 9.
This is another way of putting if „s together when multiple decisions are involved. A multipath decision is a chain of if ‟s in which the statement associated with each else is an if. Hence it forms a ladder called else–if ladder.
if (Test Condition -1) Statement -1; else if ( Test Condition -2) Statement -2; else if ( Test Condition -3) Statement -3; : else if ( Test Condition –n) Statement –n; else default statement; Rest of the Program Statements-X; The above construction is known as else if ladders. The conditions are evaluated from top to bottom. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the Rest of the Program Statement–X (skipping rest of the ladder). When all the “n‟ conditions become false, then the final else containing the default statement will be executed.
void main() { int a,b,c; clrscr(); cout<<“Enter the numbers:”; cin>>a>>b>>c); if ((a>b) && (a>c)) cout<<“Highest Number is:”<< a; else if ((b>a) && (b>c)) cout<<“Highest Number is:”<< b; else cout“Highest Numbers is:”<< c; getch(); }
Enter the numbers: 12 34 10 Highest number is : 34
The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression which is included within the switch statement. The switch statement is a multi-way branch statement. In a program if there is a possibility to make a choice from a number of options, this structured selected is useful. The switch statement requires only one argument of int or char data type, which is checked with number of case options. The switch statement evaluates expression and then looks for its value among the case constants. If the value matches with case constant, then that particular case statement is executed. If no one case constant not matched then default is executed. Here switch, case and default are reserved words or keywords. Every case statement terminates with colon “:”. In switch each case block should end with break statement, i.e.
switch(variable or expression)
for(initialize expression; test condition; updation ) { Statement 1; Statement 2; }
void main() { int i; clrscr(); cout<<“The numbers of 1 to 25 are:”; for(i=1; i < =15; i=i+1) cout<< i; }
The Numbers of 1 to 15 are: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
We can also use loop within loops. i.e. one for statement within another for statement is allowed in C. (or „C‟ allows multiple for loops in the nested forms). In nested for loops one or more for statements are included in the body of the loop.
for( initialize ; test condition ; updation) / outer loop / { for(initialize ; test condition ; updation) / inner loop / { Body of loop; } } The outer loop controls the rows while the inner loop controls the columns.
void main() {
int a,b,sum; clrscr(); for (a=3; a > =1; a - - ) { for(b=1;b<=2;b++) { sub = a – b; cout<<”a=”<<a<<”b=”<<b”a-b=”<<sub); } } getch(); }
a=3 b =1 a-b = a=3 b =2 a-b = a=2 b =1 a-b = a=2 b =2 a-b = a=1 b =1 a-b = a=1 b =2 a-b =-
The simplest of all the looping structures in C is.
Initialization Expression; while( Test Condition) { Body of the loop } The while is an entry – controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed. The execution process is repeated until the test condition becomes false and the control is transferred out of the loop On exit, the program continues with the statement immediately after the body of the loop. The body of the loop may have one or more statements. The braces are needed only if the body contains two or more statements. It‟s a good practice to use braces even if the body has only one statement.
void main() { int a=1,sum=0; clrscr(); while(a<=10) { sum = sum + a; a++; } Cout<<“Sum of 1 to 10 numbers is:”<<sum); getch(); }
Sum of 10 numbers is: 55
In do-while, the condition is checked at the end of the loop. The do-while loop will execute at least one time even if the condition is false initially. The do-while loop executes until the condition becomes false.
Initialization Expression; do { Body of the loop; } while ( Test Condition);