Search This Blog

Showing posts with label C plus plus. Show all posts
Showing posts with label C plus plus. Show all posts

Wednesday, February 3, 2021

Basic input output in C++

Mr. abhijeet dwivedi

Basic input output in C++





C++ comes with a libraries which is help us in programming input/output. In C++ sequence of bytes corresponding to input and output are commonly known as stream.
1. Input Stream:-
    Direction of flow of bytes take place from input devices. 
    For example: keyboard to the main memory.

2. Output Stream:-
    Direction of flow of bytes take place from main memory to the output device. 
    For example: Display.

Lets take one example 


# include<iostream>
using namespace std;

int main();
{
 int num1, num2;

 cout<<"Enter the value of num1 and num2 <<endl;   -- Output function

 cin>>num1>>num2>>endl;                          -- Input function

 cout<<"The sum of mum1 and num2 is = "<<num1+num2<<endl;

return 0;
}


Reserved keyword in C++

  • This is a list of keyword reserved in C++
  • Since they are uses by the language itself these keywords are not available for variable and overloading.
  • In short you can not create a variable with these names 
There are some reserved keyword in C++ following are:-
  • alignas (since C++11)
  • alignof (since C++11)
  • and
  • and_eq
  • asm
  • atomic_cancel (TM TS)
  • atomic_commit (TM TS)
  • atomic_noexcept (TM TS)
  • auto (1)
  • bitand
  • bitor
  • bool
  • break
  • case
  • catch
  • char
  • char8_t (since C++20)
  • char16_t (since C++11)
  • char32_t (since C++11)
  • class (1)
  • compl
  • concept (since C++20)
  • const
  • consteval (since C++20)
  • constexpr (since C++11)
  • constinit (since C++20)
  • const_cast
  • continue
  • co_await (since C++20)
  • co_return (since C++20)
  • co_yield (since C++20)
  • decltype (since C++11)
  • default (1)
  • delete (1)
  • do
  • double
  • dynamic_cast
  • else
  • enum
  • explicit
  • export (1) (3)
  • extern (1)
  • false
  • float
  • for
  • friend
  • goto
  • if
  • inline (1)
  • int
  • long
  • mutable (1)
  • namespace
  • new
  • noexcept (since C++11)
  • not
  • not_eq
  • nullptr (since C++11)
  • operator
  • or
  • or_eq
  • private
  • protected
  • public
  • reflexpr (reflection TS)
  • register (2)
  • reinterpret_cast
  • requires (since C++20)
  • return
  • short
  • signed
  • sizeof (1)
  • static
  • static_assert (since C++11)
  • static_cast
  • struct (1)
  • switch
  • synchronized (TM TS)
  • template
  • this
  • thread_local (since C++11)
  • throw
  • true
  • try
  • typedef
  • typeid
  • typename
  • union
  • unsigned
  • using (1)
  • virtual
  • void
  • volatile
  • wchar_t
  • while
  • xor

--------------------------------------------------

Tags
c++ min function
c++ coding software
c++ by bjarne stroustrup pdf
c++ objective questions and answers
c++ kya hota hai in hindi
c++ 9th edition
c++ 8th edition
how much c++ do i need to know for unreal engine
c++ 7/2
c++ uses compiler or interpreter
is c++ still useful
c++ apna college
is c++ still widely used
can c++ be used for game development
where is c++ mostly used
for c++ romana
c++ 4.9.2 compiler
c++ ke fayde
when c++ was created
where do we use c++

Wednesday, January 20, 2021

Preprocessor directives in C++ | C++ Tutorial with Codes With Abhi | C++ Programming Language | Codes WIth Abhi

Mr. abhijeet dwivedi

                     The preprocessor directives in C++


The preprocessor is a unique feature of C++. In C++, we have steps like compilation, linking and execution for a typical program. In reality, we have a lot of other features in a C++ program that needs to be processed before passing the program for compilation.





For this purpose, a special step called preprocessing is carried out. Preprocessing is carried out before the compilation process and the special features are preprocessed. As a result, an expanded C++ program is obtained and then it is passed to the compiler.


In this tutorial, we will explore the various preprocessor directives supported by C++.


File Inclusion Directives

#include

File inclusion directive #include allows us to include other files in our source program. We can include any header file that contains definitions of various predefined functions in our program using these functions. We can include header files in our program using the following syntax.


#include <filename> 

Example:#include <iostream>


We have already seen this in our C++ programs. The header iostream contains the functions required for input/output data streaming like cout, cin, etc.


As our programs grow larger or the functionality becomes complex, we might want to divide our program into various files or import functionality from the other files. In this case, we make use of user-defined files. To include user-defined files in our program we can make use of the following syntax of #include directive


include “filename” 

Example: #include “vector_int.h”


This is a user-defined header file that we intend to include in our program in order to use its functionality.


The below code Example shows the usage of the #include directive.


#include <iostream>

using namespace std;

int main()

{

  cout<<"This is an example demonstrating inclusion directive #include";

}

Output:


This is an example demonstrating the inclusion directive #include.


As shown, we have used the #include directive to include the functionality of the <iostream> header in our program.


Macro Definition Directives

#define

The #define directive is used to define the symbolic constants or macros in C++ program.


The general form of a #define directive is:


#define macro_name replacement code

When a preprocessor encounters the macro in the program, the preprocessor replaces this macro with the code that is defined using the #define directive before the code is passed to the compiler.


The below code Example shows a symbolic constant RADIUS that is defined using #define directive and its usage in the program.


#include <iostream>

#define RADIUS 5

using namespace std;


int main()

{

  

cout<<"Area of a circle : "<<3.142 * RADIUS * RADIUS;

}

Output:


Area of a circle : 78.55


As shown in the program, we can make use of symbolic constant RADIUS in our code and it will be replaced by the value defined for it using the #define directive.


We can use the #define directive to define a proper function code. These functions are usually small functions.


An example is shown below.


#include <iostream>

#define REC_AREA(length, breadth) (length * breadth)

using namespace std;


int main()

{

  int length = 20, breadth = 5, area;

 

  area = REC_AREA(length, breadth);

 

  cout << "Area of a rectangle is: " << area;

 

  return 0;

}

Output:


Area of a rectangle is: 100


Here using the #define directive we have defined a function REC_AREA that takes two arguments i.e. length and breadth and calculates the area of a rectangle. In the main function, we just make use of this macro and supply two arguments to it to obtain the area of a rectangle.


#undef

Macros in a program defined with the #define directive last until it is undefined using the #undef directive. Once the program encounters #undef, the subsequent use of macro (undefined by #undef) will give a compilation error.


In the above program, if we just give a statement #undef REC_AREA after the integer declarations, then the program will give a compilation error.


Conditional Compilation Directives

Apart from the directives explained above, C++ also provides the following directives that can be used for conditional compilation of code. These directives can be used on similar lines of the if-else statement of C++.


For Example, we can set DEBUG for a program ON or OFF using these conditional directives.


Some of the conditional compilation directives provided in C++ include:


#if

#elif

#endif

#ifdef

#ifndef

#else

The below program demonstrates the usage of conditional compilation directives in a C++ program.


#include <iostream>

using namespace std;

#define DEBUG


#define MAX(a,b) (((a)>(b)) ? a : b)


int main () {

  int i, j;

 

  i = 100;

  j = 50;

  

#ifdef DEBUG

  cout <<"Trace: Start of main function" << endl;

  #endif

  

cout <<"The maximum is " << MAX(i, j) << endl;

  

#undef MAX

  //cout <<"The maximum is " << MAX(10,20) << endl;

 

  #ifdef DEBUG

  cout <<"Trace: End of main function" << endl;

  #endif

  return 0;

}

Output:


Trace: Start of main function

The maximum is 100

Trace: End of main function


In the above program, we use #ifdef – #endif directive to define a DEBUG for the program. Then we undefined the macro function MAX using the #undef directive. The conditional compilation directive constructs #ifdef – #endif checks if DEBUG is set and if it’s set, it prints few messages in the program.


The # & ## Operators

The # and ## operators are two special operators that are respectively used to convert a text token into a string to be displayed and concatenate two tokens.


Below given is an Example demonstrating both these operators.


#include <iostream>

using namespace std;


#define MKSTR( x ) #x

#define concat(a, b) a ## b


int main () {

  

  cout <<"MKSTR(Hello World) = "<< MKSTR(Hello World) << endl;

  int xy = 100;

 

  cout <<"concat(x,y) = "<<concat(x,y);

  return 0;

}

Output:


MKSTR(Hello World) = Hello World

concat(x,y) = 100


In the above program, we define MKSTR with an argument x. It has body #x. When we print this MKSTR using the argument “Hello World”, we see that because of #x, the argument is converted to a string and is displayed to the output.


Learn Python Programming Language 


Friday, August 14, 2020

Differences between C and C++ | Basic difference between c and c++ | codes with abhi

Mr. abhijeet dwivedi

                  C++ Programming:- 

In this tutorial you will learn:-

 Differences between C and C++:

 

 


       C                     C++

C does no support polymorphism, encapsulation, and inheritance which suggests that C doesn't support object oriented programming

C++ supports polymorphism, encapsulation, and inheritance because it's an object oriented programing language

C may be a subset of C++

C++ may be a superset of C.

C contains 32 keywords

C++ contains 52 keywords.

For the event of code, C supports procedural programming

C++ is understood as hybrid language because C++ supports both procedural and object oriented programming paradigms.

Data and functions are separated in C because it's a procedural programing language

Data and functions are encapsulated together in sort of an object in C++.

C doesn't support information hiding

Data is hidden by the Encapsulation to make sure that data structures and operators are used as intended

C may be a function driven language because C may be a procedural programing language

 

C++ is an object driven language because it's an object oriented programming.

Function and operator overloading isn't supported in C

 

Function and operator overloading  supported in C++

C may be a function-driven language

\

C++ is an object-driven language

Namespace features aren't present inside the C

Namespace is employed by C++, which avoid name collisions.

Header file employed by C is stdio.h

 

Header file employed by C++ is iostream.h.

Reference variables aren't supported by C

 

Reference variables are supported by C++.

Virtual and friend functions aren't supported by C

Virtual and friend functions are supported by C++.

C doesn't support inheritance

C++ supports inheritance.

 C provides malloc() and calloc() functions for dynamic memory allocation, and free() for memory de-allocation

  C++ provides new operator for memory allocation and delete operator for memory de-allocation.

C was developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Labs 

C++ was developed by Bjarne Stroustrup in 1979.

 

For Example


C-Variable declaration

Only at the top:

int i;

for(i=10; i<10;i++)

 

 

C++ variable declaration

Anywhere in the program;

for(i=1;i<10;i++)

 


Wednesday, August 12, 2020

Function Overloading in C++

Mr. abhijeet dwivedi

           C++ Programming:-

In this tutorial you will learn:-

Function Overloading in C++

Function overloading means two or more functions can have an equivalent name, but either the amount of arguments or the info sort of arguments has got to vary . within the first example, we create two functions of an equivalent name, one for adding two integers and another for adding two floats. within the second program, we make two functions with identical names but pass them a special number of arguments. Function overloading is additionally referred to as compile-time polymorphism.




We created two functions "add" for integer and float data types, you'll create any number of them of an equivalent name as needed . Just confirm a compiler are going to be ready to determine which one to call. In the program, you'll create add function for long, double, and other data types.


Function overloading C++ program

#include <iostream>

using namespace std;

/* Function arguments are of different data type */

int add(int, int);
float add(float, float);

int main()
{
  int a, b, x;
  float c, d, y;

  cout << "Enter two integers\n";
  cin >> a >> b;

  x = add(a, b);

  cout << "Sum of integers: " << x << endl;

  cout << "Enter two floating point numbers\n";
  cin >> c >> d;

  y = add(c, d);

  cout << "Sum of floats: " << y << endl;

  return 0;
}

int add(int x, int y)
{
  int sum;

  sum = x + y;

  return sum;
}

float add(float x, float y)
{
  float sum;

  sum = x + y;

  return sum;
}


In the functions, the code is that the same but data types are different, C++ provides an answer to the present problem. We can create one function for various data types which can reduce the dimensions of code by employing a feature of C++ referred to as templates.


C++ program for function overloading

#include <iostream>

using namespace std;

/* Number of arguments are different */

void display(char []);  // print the string passed as argument
void display(char [], char []);

int main()
{
   char first[] = "C programming";
   char second[] = "C++ programming";
   
   display(first);
   display(first, second);
   
   return 0;
}

void display(char s[])
{
   cout << s << endl;
}

void display(char s[], char t[])
{
   cout << s << endl << t << endl;
}

Output of program:

C programming
C programming
C++ programming

A function return type has no role because it returns a value when called and at compile time compiler will not be able to decide which one to call.




Polymorphism and Inheritance in C++

Mr. abhijeet dwivedi

          C++ Programming:-

 In this tutorial you will learn:-

Polymorphism

Polymorphism is that in which we can perform a task in multiple forms or ways. It is applied to the functions or methods. Polymorphism allows the thing to make a decision which sort of the function to implement at compile-time also as run-time.


Types of Polymorphism are:


1. Compile-time polymorphism (Method overloading)

2. Run-time polymorphism (Method Overriding)





Inheritance is the process of by which a replacement class is made that inherits the properties of the exist already class. It supports the concept of code reusability and reduces the length of the code in object-oriented programming.


Types of Inheritance are:


1. Single inheritance

2. Multi-level inheritance

3. Multiple inheritance

4. Hybrid inheritance

5. Hierarchical inheritance



Difference between Inheritance and Polymorphism:

S.NOInheritancePolymorphism
1.Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class).Whereas polymorphism is that which can be defined in multiple forms.


2.It is basically applied to classes.
Whereas it is basically applied to functions or methods.


3.Inheritance supports the concept of reusability and reduces code length in object-oriented programming.Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding).


4.Inheritance can be single, hybrid, multiple, hierarchical and multilevel inheritance.Whereas it can be compiled-time polymorphism (overload) as well as run-time polymorphism (overriding).

5.It is used in pattern designing.While it is also used in pattern designing.





Dynamic and Static Binding in C++

Mr. abhijeet dwivedi

                               C++ Programming:-

In this tutorial you will learn:-


 Dynamic binding

 

Dynamic  memory is the process of linking procedure call to a selected sequence of code (method) at run-time. It means the code to be executed for a selected procedure call isn't known until run-time. Dynamic binding is additionally referred to as late binding or run-time binding.


Dynamic binding is an object oriented programming concept and it's related with polymorphism and inheritance.


Dynamic binding definition

Dynamic binding(dispatch) means a block of code executed with regard to a procedure(method) call is decided at run time.

Dynamic dispatch is usually used when multiple classes contain different implementations of an equivalent method. It provides a mechanism for choosing the function to be executed from various function alternatives at the run-time. In C++, virtual functions are wont to implement dynamic binding

 


Definitions of Static Binding

When compiler acknowledges all the knowledge required to call a function or all the values of the variables during compile time, it's called “static binding“. As all the specified information are known before runtime, it increases the program efficiency, and it also enhances the speed of execution of a program.


Static Binding makes a program very efficient, but it declines the program flexibility, as ‘values of the variable’ and ‘function calling’ are predefined within the program. Static Binding is implemented during a program at the time of coding.


Overloading a function or an operator are the instance of compile-time polymorphism, i.e. static Binding.


Difference between Static and Dynamic Binding


BASIS FOR COMPARISONSTATIC BINDINGDYNAMIC BINDING
Event OccurrenceEvents occur at compile time are "Static Binding".
Events occur at run time are "Dynamic Binding".
InformationAll information needed to call a function is known at compile time.All information need to call a function come to know at run time.
AdvantageEfficiency.Flexibility.
TimeFast execution.Slow execution.
Alternate nameEarly Binding.Late Binding.
ExampleOverloaded function call, overloaded operators.Virtual function in C++, overridden methods in java