Monday 31 July 2017

C++ LANGUAGE


C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T.

History:
LanguageYearDeveloped By
Algol1960International Group
BCPL1967Martin Richard
B1970Ken Thompson
Traditional C1972Dennis Ritchie
K & R C1978Kernighan & Dennis Ritchie
C++1980Bjarne Stroustrup
Downloading C++:
Turbo C++ is used to write both C and C++ programs.
While writing C++ program we can see with .cpp extension for C++ programs.












Installing TurboC++:














Hello C++ program:
iostream ---- standard input output functions.
Provides cin and cout methods for reading from input and writing to output respectively.
#include ---- includes console input output library functions.
void main() ---- Entry point for every program. Specifies that it returns no value.
cout ---- Used to print the data on the console.
getch() ---- Asks for a single character. Until press any key blocks the screen.

Compile and run the program using shortcut key:
Press ctrl+F9 keys to compile and run the program directly.
Alt+F5 to view user screen any time.
Press Esc to return to turbo C++ console.
Output operation:
Bytes flow from main memory to device like printer, display screen, or a network connection is called output operations.
Input operation:
Bytes flow from device like printer, display screen, or a network connection to main memory is called input operations.
Standard output stream (cout):
Standard output device, which is usually a display screen.
Displays the output on the console. 
Eg:
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. char ary[] = "Welcome to C++ ";
  5. cout << "Value of ary is: " << ary << endl;
  6. }
Output:
Value of ary is: Welcome to C++
Standard input stream (cin):
Standard input device, which is usually a keyboard.
Read the input from the console.
Eg:
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int age;
  5. cout << "Enter your age: ";
  6. cin >> age;  
  7. cout << "Your age is: " << age << endl;  
  8. }  
Output:
Enter your age: 22
Your age is: 22
Standard endline (endl):
Used to insert a new line characters and flushes the stream.
Eg: 
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. cout << "hi";    
  5. cout << "moulica<<endl;   
  6. cout << "how are you"<<endl;
  7. }   
Output:
hi moulica
how are you
Variable:
Name of memory location.
Used to store data.
Syntax:
type variable_list;
Eg:
int x;
Syntax for variable initialization:
data_type variable_name = constant
Eg:
int a = 10;
Data Types:
Data TypesMemory SizeRange
char1 byte-128 to 127
signed char1 byte-128 to 127
unsigned char1 byte0 to 127
short2 byte-32,768 to 32,767
signed short2 byte-32,768 to 32,767
unsigned short2 byte0 to 32,767
int2 byte-32,768 to 32,767
signed int2 byte-32,768 to 32,767
unsigned int2 byte0 to 32,767
short int2 byte-32,768 to 32,767
signed short int2 byte-32,768 to 32,767
unsigned short int2 byte0 to 32,767
long int4 byte
signed long int4 byte
unsigned long int4 byte
float4 byte
double8 byte
long double10 byte
Loops:
Iterate a part of program several times.
for loop:
Number of iterations is fixed can use for loop.
Syntax:
for(initialization; condition; increment / decrement) {
//code to be executed
}
Flow chart:
While loop:
Iterate a part of program several times.
Number of iterations is not fixed.
Syntax:
while(condition) {
//code to be executed
}
Flow chart:
Do-while loop:
Iterate a part of program several times.
Number of iterations is not fixed must have to execute the loop atleast once.
Condition is checked after loop body.
Syntax:
do {
//code to be executed 
}while(condition);
Flow chart:
Control statements:
if statement:
Tests the condition. Executes if the condition is true.
Syntax:
if(condition) {
//code to be executed
}
Flow chart:
                 
if-else statement:
Executes if block if condition is true otherwise else block will execute.
Syntax:
if(condition) {
//code to be execute if condition is true
} else {
//code if condition is false
}
Flow chart:
if-else-if ladder:
Executes one condition from multiple statements.
Syntax:
if(condition1) {
//code to be execute if condition1 is true
} elseif(condition2) {
//code to be execute if condition2 is true
elseif(condition3) {
//code to be execute if condition3 is true
....
else {
//code to be executed if all conditions are false
}
Flow chart:
Switch:
Executes one statement from multiple conditions.
Like if-else-if ladder.
Syntax:
switch(expression)
case value1: 
//code to be execute
break;
case value2: 
//code to be execute
break;
....
default
//code to be execute if all conditions are not matched;
break;
}
Flowchart:


Break:
Breaks the current flow of the program.
Syntax:
jump-statement
break;
Flow chart:
Continue:
Continues the current flow of the program and skips the remaining code at the specified locations.
Syntax:
jump-statement;
continue;
Flow chart:
Goto:
Known as jump statement.
Transfers control to other part of the program.
Add two strings:

Find starting point for a string:


Read from user input and write to user output:


Read from a file and write to a file:
Read from a file:
Create an .txt file with some data in it.
Copy the .txt file in turboc3--Bin folder.


We can see the filename.txt folder in our bin folder.
After writing a program we can see the output what we have written in our .txt file.


Write to a file:

Here in console screen we are creating a file to our folder like writing datatofile.txt
Then entering few lines of data to file. 

Now, it will create a .txt file and data will be saved in it.

Opening the .txt file we can see the data which we entered in our console screen.
Graphic output syntax:
Line:
Circle:

Rectangle:

Star:

Downloading codeblocks:













Partial string:
Count the number of occurances of substring in a  string:
Second and fourth line in a file:

Non repeated character in a string:
Permutation of a string:
Digit identification is string have any numbers:
Replacing first highest repeated character with user given character:
Pattern:
Database: