C ++ Programming basics

Views:
 
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

C++ Programming basics : 

C++ Programming basics By: Rashmi Tatte

C++ Features : 

C++ Features Supports data security Prevents accidents with data Helps code reuse. Lets you use operators the way you like. 5. Allows multiple functions/operators with the same name.

Slide 3: 

C++ Program is collection of functions. Execution begins at main() function. C++ Statements end with ;. C++ introduces new comment symbol ie double slash //.

C++ output operator : : 

C++ output operator : cout is a predefined object that represents the standard output stream in C++. The operator << is called insertion or put to or bit-wise left shift operator. It inserts the contents of the variable on its right to the object on its left.

C++ Input operator : 

C++ Input operator The identifier cin is a predefined object that corresponds to the standard input stream. Along with cin, extraction operator >> is used. It extracts the value from keyboard and assigns it to the variable on its right.

Example of cout : 

Example of cout cout<<“Welcome”; In the above example string in quotes will be displayed on the screen.

Example of cin : 

Example of cin cin>>number1; Above is an input statement and causes the program to wait for the user input. The number keyed from user is placed in the variable number1.

Directives : : 

Directives : The directive causes the preprocessor to add the contents of the file to the programs. Eg : #include<iostream.h> In above example the directive #include, includes the contents of iostream header file to the program.

using Directive : : 

using Directive : For using the identifiers defined in the namespace scope using directive is included. Eg. using namespace std; Here std is namespace , this will bring all identifiers defined in std to the global scope. Namespace defines the scope for the identifiers that r used in a program.

Tokens : : 

Tokens : Keywords Identifiers Constants Strings Operator

Basic Data Types : : 

Basic Data Types :

Data types with their size & range : 

Data types with their size & range

Other features of C++ : : 

Other features of C++ : Declaration of variables Dynamic Initialization of variables Reference variables.

Operators : 

Operators C++ has a rich set of operators. All C operators are valid in C++. Arithmetic operators: + , - , / , * , % Increment/decrement operator: ++ , -- Relational Operators : < , > , <= , >= , == , != Logical operators: && , || , ! Bitwise Operators: <<, >>, &, | Conditional Operator : ? : Scope Resolution operators: :: Linefeed Operator: endl Field width Operator: setw Memory Management Operators: new, delete

Operators in C++ : 

Operators in C++ Scope Resolution operator Memory Management operator Manipulators

Scope Resolution Operator : 

Scope Resolution Operator C++ is a block-structured language. Blocks and scopes can be used in constructing programs. The scope of the variable extends from the point of its declaration till the end of the block containing declaration. Syntax: :: variable-name ; Eg. … … { int count=20; … { … int count=50; … } … } Block 2 Block 1

Slide 17: 

#include<iostream> #include<iomanip> int m=10; int main() { int m=20; { int m=30; cout<<“We are in inner block” cout<<“m=”<<m<<endl; cout<<“::m=”<<::m<<endl; } cout<<“We are in outer block” cout<<“m=”<<m<<endl; cout<<“::m=”<<::m<<endl; return 0; }

Memory Management operator : 

Memory Management operator new delete

Manipulator Operators : 

Manipulator Operators endl setw

endl : 

endl endl will instruct output stream to display next output on new line. Its working is almost same as \n The difference between \n and endl is endl causes the output buffer to be flush.

setw : 

setw setw is used to set field width of output For setw we have to include iomanip file in our source code. The default field is just wide enough to hold the value. Eg. Interger 567 will occupy a field 3 character wide String Rajasthan will occupy a field 9 character wide

Example for setw : 

Example for setw #include<iostream> #include<iomanip> using namespace std; int main() { long pop1=2425785, pop2=47, pop3=9761; cout<<setw(8)<<“LOCATION”<<setw(12)<<“POPULATION”<<endl <<setw(8)<<“MUMBAI”<<setw(12)<<pop1<<endl <<setw(8)<<“NASIK”<<setw(12)<<pop2<<endl <<setw(8)<<“PUNE”<<setw(12)<<pop3<<endl; return 0; }

Output : 

Output

Control Structures : 

Control Structures if Statement switch Statement do-while Statement while Statement for Statement

Our First C++ Program!!!! : 

Our First C++ Program!!!!