Tuesday 29 August 2017

Topics in C++

Arithmetic Operations in C++(Addition,subtraction,multiplication,division and modulus) With The Help Of Global,Local And Default Variables.

Let understand arithmetic operations with the help of global and local variables.
The arithmetic operation is used for some computation or calculation.
Addition of two variables:-
#include<iostream>
using namespace std;
int x=6; //global variable

int main()
{
   int y=60,z; /* y is declared and initialized,y and z                 are local variable */
 
   z=x+y;
   cout<<"Addition:"<<z;
   return 0;
}


output:-
Addition:66
here x is the global variable(we can use the global variable outside the main function because it is available globally) y and z are local variables(we can use these variables inside the function in which they are declared). y is declared and initialized in the main function but you can give value to y  at the runtime by using cin.

Subtraction of two variables:-

#include<iostream>
using namespace std;
int main()
{
 int x,y,z;
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=x-y;
 cout<<"Subtraction:"<<z;
 return 0;
}
output:-

Enter the value of x:53
Enter the value of y:30
subtraction:23
Multiplication of variables:-

#include<iostream>
using namespace std;
int main()
{
 float2 w=0,x,y,z; //default value for w
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=w*x*y;
 cout<<"multiplication:"<<z;
 return 0;
}


output:

Enter the value of x:2
Enter the value of y:3
multiplication:0


here we have a default value to w that means if we forget to give a value to w it will automatically provide a value to w.
Take an example: if we assign a value to w on runtime say w=2, then answer will be 2*2*3=12 not zero.
do it yourself give the default value of w is zero then assign a value to w by using cin then see what will be the answer and also you can assign a value for w after default value through cin then latest value of w will be include in multiplication.

Division of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x/y;
 cout<<"Quotient:"<<z;
 return 0;
}

output:-
Enter the value of x:67
Enter the value of y:9
Quotient:7.44444
Modulus of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x%y;
 cout<<"Remainder:"<<z;
 return 0;
}

output:-
Enter the value of x:45
Enter the value of y:6
Remainder:3
when you divide two variables it gives you quotient where modulus give you the remainder.
if you use int variables is only provide either quotient or remainder, if you use float it will give you perfect value.
Example: divide 7 by 2 if you take 7 and 2 as the integer the quotient is 3 and modulus is 1 where if you take 7 and 2 as float it will give you 3.5 as answer.

                             HAPPY CODING

Wednesday 23 August 2017

Data Types in C++

while doing programming in any programming language, you need some specific words to tell the compiler. Here I will explain these words.

TOKENS:-it is the smallest unit in a program, these are of five types.


  1. Keywords:-These are the words that convey a special meaning to the language compiler like default, case, break etc.
  2. Identifiers:-It is the name given by the user for a unit of the program.Identifiers can contain letters and digits.example are MYFILE,_DS,z2t0z9.
  3. Literals:-These are data items that never change their value during a program run (often referred to as constants).examples integer-constant,character-constant,floating-constant,string-literal.
  4. Punctuators:-It enhance a program's readability and give proper meaning to statements, expressions etc as per syntax. examples are {},[ ].
  5. Operators:-These are tokens that trigger some computation or action when applied to the variable and other objects in an expression.examples are arithmetic operators, logical operators, relational operators, conditional operators.i will explain it further.
DATA TYPES:-Data types are means to identify the type of data and associated operations for handling it.C++ data types are of two types:-

  1. Fundamental data types: These are those that are not composed of other data types.These are six fundamental data types in C++.
  • Int Data type: Int stores integer i.e. 34,-678 etc.They have no fractional parts.
  • Char Data type: Char stores character.character can store character variable.examples are &,b,% and 3.
  • Float Data type: Float stores floating-point numbers.it stores number having integer plus fractional part.example are  8.98888,7.89.
  • Double Data type: Double stores twice as int and float.
  • Void Data type: The void type specifies an empty set of values.It is used as the return type for functions that do not return a value.
  • Bool Data type: Bool stores Boolean algebra.It stores either true or false.

DATA TYPES MODIFIERS:-Expect type void, the basic data types can be modified by using modifiers such as


  • signed
  • unsigned 
  • long 
  • short
     2.Derived Data Type: These are derived from the fundamental data type.


  • Array: Arrays refer to a named list of a finite number n of similar data elements.Examples are ary[1],ary[2].......ary[n].
  • Function: A function is a named part of a program that can invoke from other parts of the program as often needed.
  • Pointer: A pointer is a variable that holds a memory address(type *ptr).
  • Reference: A reference is an alternative name of an object.The general declaring of reference variable is :type &ref-var=var-name;
  • Constant: The keyword const can be added to the declaration of an object to make that object a constant rather than a variable.The general declaration of constant is -const type name=value;




  • User-Defined Derived Data Types:-There are some derived data types that are defined by the user.
  1. Class:- A class represent a group of similar objects.
  2. Structure: A structure is a collection of variables referenced under one name, providing convenient means of keeping related information together.
  3. Union: A union is a memory location that is shared by two or more different variables, generally of different types at different times.
  4. Enumeration:- An alternative method for naming integer constants is often more convenient than const.
more information will be provided on these topics as we go deeper in C++.



Tuesday 22 August 2017

What is namespace in C++?

when you use the namespace, your program looks like:-

#include<iostream>
using namespace std;
int main()
{
 cout<<"Hello World";
 return 0;
}
when you don't use the namespace, your program looks like:-

#include<iostream>
int main()
{
 std::cout<<"Hello World";
 return 0;
}
without "using namespace std;" when you write for example "cout<<;", you'd have to put "std::cout<<;",
Another role of the namespace is that it solves name conflicts among files and allow them to use with the same name.Let take an example,
you have a file let say main, and you want to include two files file_one and file_two in main but file_one and file_two both have the same function display when you include these file in the main file and call these file having the same function name it produces a conflict and shows an error.
The error will be the redefinition of function this is because we are not declaring display inside any declarative region such as namespace.C++ think you are redefining the function.To solve this conflicts we use the namespace.we use namespace then file name then we define the function in both file then we use this function in the main file in INT MAIN() { file_one::function}. so namespace avoids collision among same name file and makes them executable.




Thursday 17 August 2017

Basic Syntax Of C++(what is main,#include,iostream,namespace and comments in c++)

program to print a string on the screen(DevC++)

The above program is one of the simplest programs that can be written in C++, but it does include basic elements that every C++ programs have. Let us have a  look at these elements one by one:

1. COMMENTS IN C++ PROGRAM 
Comments are pieces of code that the compiler simply does not execute.Comments are written for the explanation of the steps or program.There are two ways to insert comments in C++ programs.

(i) Single line comments with / /:-The comments that begin with //are single line comments.The compiler simply ignores everything following in that same line.In above picture line 1 represents the single line comment, whatever written after // will be discarded.
(ii)multi-line comments with /*.......*/:-The comments begin with /* and end with */.That means, everything that falls between /* and */will be discarded.In above picture line, 8 and 9 represent multi-line comments.

2. #INCLUDE
The statements that begin with the #(hash) sign are directives for the preprocessor(a computer program that modifies data to conform to the input requirements of another program).That means these statements are processed before compilation takes place.INCLUDE statement tells compiler's preprocessor to include the header file in the program.

3. INT MAIN (){.....}
The line indicates the beginning of the main function.Whatever written between the curly brackets {}
will be executed.It is essential to have the main function.INT is data type will be explained further.

4. IOSTREAM(header file)
The header file iostream is included in every C++ program to implement input/output facilities, without this file we cannot input or take the output from the program.

5. COUT(pronounced "see-out")
Cout stands for console output.It helps in printing the values on the screen.

6. CIN(pronounced "see-in")
Cin stands console input.It takes input from input devices.

7. INSERTION OPERATOR("<<")
Insertion is performed by insertion operator"<<".

8.EXTRACTION OPERATOR(">>")
Extraction is performed by the extraction operator">>".

9. SEMICOLON(;)
The semicolon (;) character used to finish every executable statement of a C++ program and it must be included after every executable instruction.

10. RETURN 0
The return instruction makes the main() to finish and it returns a value, in this case, it is returning 0(zero).Returning 0 is the most usual way of telling that program has terminated normally.

                      click here to know what is namespace