prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

C++ Programming Part 4

Rajjit Mar 11th, 2023 10 mins read
post-11

TOPIC: C++ Header Files and Operators

C++ Header Files

    There are two types of header files
  • System header files: It comes with the compiler. Example: iostream, string, math, etc.
  • User defined header fils: It is written by the Programmer. Example: newfile.h, hellocode.h, etc.

Let us see how the header files are used:

copy
This is the code about System header files

C++ Program

Code written in Visual Studio Code

// Program to illustrate the use of System header files
#include<iostream>  // here, we are using the iostream header files
#include<conio.h> // here, we are using conio.h header files
// we will learn more about the header files, so stay connnected with our blog
using namespace std;
int main()
{
          cout<<"We are learning about System header files";
          cout<<"Here, iostream and conio.h are used";

          getch();
          return 0;
}
Output:
We are learning about System header files
Here, iostream and conio.h are used

copy
This is the code about User defined header files

C++ Program

Code written in Visual Studio Code

.cpp file

// Program to illustrate the use of User defined header files
// Make a file with the extension .cpp         
#include<iostream>

#include "hellocode.h" // here, a user defined header file is used

using namespace std;
int main()
{
          int b = 45;
          int c;
          cout<<"We are learning about User defined header files";
          cout<<"Here, hellocode.h is used";
              
          c = a + b;
          cout << c;

          return 0;
}

We have no integer 'a' defined in the cpp file. So we give the integer 'a' to the hellocode.h file. By doing so we are able to access the contents of the hellocode.h file. As we have included the file, there will be no errors in the program. And we will get the output.


.h file

// Make a file hellocode.h in the same directory
int a = 45;
Output:
We are learning about User defined header files
Here, hellocode.h is used
90


Operators
🤔 What do you mean by Operators?
➡️ Operators are the symbols which are used to perform operations (like Addition, Subtraction, Multiplication, Division, Relational, Logical, etc.) on Operands (like Variables a and b, x and y, newVar, sum, etc.). The symbols include +, -, *, /, %, ++, --, ==, etc.

    👉🏼Types of Operators and Examples:
  • Arithmetic Operators: The operators that perform basic arithmetic and mathematical operations.

    These Operators include +, -, *, /, %, ++, --

    Example: a + b, a - b

  • Assignment Operator: The operator that is used to assign a specific value to a variable.

    This Operator include =

    Example: newVar = 45

  • Comparison Operators or Relational Operators: These operators are used to compare the values of the variables.

    These Operators include ==, !=, >=, <=,>, <

    Example: a >= b, a < b

  • Logical Operators: These operators are used to perform logical operations based on the given inputs.

    These Operators include logical AND (&&), logical OR (||), logical NOT (!)

    Example: a &&b, a || b

copy
This is the code for understanding operators

C++ Program

Code written in Visual Studio Code

// Program to understand the different ytpes of operators
#include<iostream>
using namespace std;
int main()
{
          int a = 5, b = 9;
          cout << "Operators in C++:" << endl;
          cout << "Following are the types of operators in C++:" << endl;

          // Arithmetic Operators
          cout << "Following are the types of arithmetic operators in C++:" << endl;
          cout << "The value of a + b is " << a + b << endl;
          cout << "The value of a - b is " << a - b << endl;
          cout << "The value of a * b is " << a * b << endl;
          cout << "The value of a / b is " << a / b << endl;
          cout << "The value of a % b is " << a % b << endl;
          cout << "The value of a++ is " << a++ << endl;
          cout << "The value of a-- is " << a-- << endl;
          cout << "The value of ++a is " << ++a << endl;
          cout << "The value of --a is " << --a << endl;

          // Assignment Operators
          //cout<<"Following are the types of assignment operators in C++:"<< endl;
          //int a = 4, b = 5;
          //char d ='r';

          // Comparison Operators
          cout << "Following are the types of comparison operators in C++:" << endl;
          cout << "The values of a == b is " << (a == b) << endl;
          cout << "The values of a != b is " << (a != b) << endl;
          cout << "The values of a >= b is " << (a >= b) << endl;
          cout << "The values of a <= b is " << (a <= b) << endl;
          cout << "The values of a > b is " << (a > b) << endl;
          cout << "The values of a < b is " << (a < b) << endl;

          // Logical Operators
          cout << "Following are the types logical of operators in C++:" << endl;
          cout << "The value of logical AND operator ((a == b) && (a < b)) is " << ((a == b) && (a < b)) << endl;
          cout << "The value of logical OR operator ((a == b) || (a < b)) is " << ((a == b) || (a < b)) << endl;
          cout << "The value of logical NOT operator (!(a == b)) is " << (!(a == b)) << endl;          

          return 0;
}
Output:
Operators in C++:
Following are the types of operators in C++:
Following are the types of arithmetic operators in C++:
The value of a + b is 14
The value of a - b is -4
The value of a * b is 45
The value of a / b is 0
The value of a % b is 5
The value of a++ is 5
The value of a-- is 6
The value of ++a is 6
The value of --a is 5
Following are the types of comparison operators in C++:
The values of a == b is 0
The values of a != b is 1
The values of a >= b is 0
The values of a <= b is 1
The values of a > b is 0
The values of a < b is 1
Following are the types logical of operators in C++:
The value of logical AND operator ((a == b) && (a < b)) is 0
The value of logical OR operator ((a == b) || (a < b)) is 1
The value of logical NOT operator (!(a == b)) is 1

"First solve the problem. Then, write the code."
- John Johnson

So, we have discussed about the C++ Header Files and Operators. More blogs coming soon.