Skip to main content

Learn C++ Programming with examples

Programming Languages

Welcome to coding world 

Go to page bottom for C++ examples with solutions

 C++ Tutorial – Learn C++ Programming with examples

In this article we will discus aboutπŸ‘‡

1: C++ Tutorial
2: Features of C++
3: Types of Operators in C++
4: Examples of C++

C++ language is a direct descendant of C programming language with additional features such as type checking, object oriented programming, exception handling etc. You can call it a “better C”.

C++ is a general purpose language, when I say general purpose it simply means that it is designed to be used for developing applications in a wide variety of domains.

C++ Tutorial

To learn C++ programming, refer these tutorials in the given order. These tutorials are written for beginners so even if you have no prior knowledge in C++, you won’t face any difficulty understanding these tutorials.

Features of C++

1) Better memory management – you can dynamically allocate memory during runtime using new and delete operator in C++ to have better memory management.

 

2) Object oriented – C++ supports object oriented programming features, which means we can use the popular OOPs concepts such as Abstraction, Inheritance, Encapsulation and Inheritance in C++ programs, these features make writing code in C++ a lot easier. We will cover them in detail in this tutorial series.

 

3) Portable – Most of C++ compilers supports ANSI standards that makes C++ portable because the code you write on one operating system can be run on other Operating system without making any change. We cannot say C++ a fully platform independent language as certain things in C++ are not portable, such as drawing graphics on a screen, since standard C++ has no graphics or GUI API.

 

4) Structured programming language – We have functions in C++, which makes easier to break a problem into small blocks of code and structure the program in such a way so that it improves readability and reusability.

 

5) Exception handling: Just like Java we can do exception handling in C++ which makes it easier to identify and handle the exceptions.

 

6) Simple – Last but not least, just like C, it is easier to write a program in C++. Once you get familiar with the syntax of C++ programming language, it becomes a lot easier to code in C++.

Operators in C++

Operator represents an action. For example + is an operator that represents addition. An operator works on two or more operands and produce an output. For example 3+4+5 here + operator works on three operands and produce 12 as output.

 Types of Operators in C++


1) Basic Arithmetic Operators

2) Assignment Operators

3) Auto-increment and Auto-decrement Operators

4) Logical Operators

5) Comparison (relational) operators

6) Bitwise Operators

7) Ternary Operator

 

1) Basic Arithmetic Operators

Basic arithmetic operators are: +, -, *, /, %

+ is for addition.

 

– is for subtraction.

 

* is for multiplication.

 

/ is for division.

 

% is for modulo.

Note: Modulo operator returns remainder, for example 20 % 5 would return 0

 

Example of Arithmetic Operators

#include <iostream>

using namespace std;

int main(){

  int num1 = 240;

  int num2 = 40;

  cout<<"num1 + num2: "<<(num1 + num2)<<endl;

  cout<<"num1 - num2: "<<(num1 - num2)<<endl;

  cout<<"num1 * num2: "<<(num1 * num2)<<endl;

  cout<<"num1 / num2: "<<(num1 / num2)<<endl;

  cout<<"num1 % num2: "<<(num1 % num2)<<endl;

  return 0;

}

Output:

 num1 + num2: 280

num1 - num2: 200

num1 * num2: 9600

num1 / num2: 6

num1 % num2: 0


2) Assignment Operators

Assignments operators in C++ are: =, +=, -=, *=, /=, %=

 

num2 = num1 would assign value of variable num1 to the variable.

 

num2+=num1 is equal to num2 = num2+num1

 

num2-=num1 is equal to num2 = num2-num1

 

num2*=num1 is equal to num2 = num2*num1

 

num2/=num1 is equal to num2 = num2/num1

 

num2%=num1 is equal to num2 = num2%num1

 

Example of Assignment Operators

#include <iostream>

using namespace std;

int main(){

 int num1 = 240;

 int num2 = 40;

 num2 = num1;

 cout<<"= Output: "<<num2<<endl;

 num2 += num1;

 cout<<"+= Output: "<<num2<<endl;

 num2 -= num1;

 cout<<"-= Output: "<<num2<<endl;

 num2 *= num1;     

 cout<<"*= Output: "<<num2<<endl;

 num2 /= num1;     

 cout<<"/= Output: "<<num2<<endl;

 num2 %= num1;     

 cout<<"%= Output: "<<num2<<endl;

 return 0;

}

Output:

 = Output: 240

+= Output: 480

-= Output: 240

*= Output: 57600

/= Output: 240

%= Output: 0


3) Auto-increment and Auto-decrement Operators
++ and —

num++ is equivalent to num=num+1;

 

num–- is equivalent to num=num-1;

 

Example of Auto-increment and Auto-decrement Operators

#include <iostream>

using namespace std;

int main(){

  int num1 = 240;

  int num2 = 40;

  num1++; num2--;

  cout<<"num1++ is: "<<num1<<endl;

  cout<<"num2-- is: "<<num2;

  return 0;

}

Output:

 num1++ is: 241

num2-- is: 39


4) Logical Operators

Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.

 

Logical operators in C++ are: &&, ||, !

 

Let’s say we have two boolean variables b1 and b2.

 

b1&&b2 will return true if both b1 and b2 are true else it would return false.

 

b1||b2 will return false if both b1 and b2 are false else it would return true.

 !b1 would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.

 

Example of Logical Operators

#include <iostream>

using namespace std;

int main(){

   bool b1 = true;

   bool b2 = false;

   cout<<"b1 && b2: "<<(b1&&b2)<<endl;

   cout<<"b1 || b2: "<<(b1||b2)<<endl;

   cout<<"!(b1 && b2): "<<!(b1&&b2);

   return 0;

}

Output:

 b1 && b2: 0

b1 || b2: 1

!(b1 && b2): 1


5) Relational operators

We have six relational operators in C++: ==, !=, >, <, >=, <=

 

== returns true if both the left side and right side are equal

 

!= returns true if left side is not equal to the right side of operator.

 

> returns true if left side is greater than right.

 

< returns true if left side is less than right side.

 

>= returns true if left side is greater than or equal to right side.

  

<= returns true if left side is less than or equal to right side.

 

Example of Relational operators

#include <iostream>

using namespace std;

int main(){

   int num1 = 240;

   int num2 =40;

   if (num1==num2) {

      cout<<"num1 and num2 are equal"<<endl;

   }

   else{

      cout<<"num1 and num2 are not equal"<<endl;

   }

   if( num1 != num2 ){

      cout<<"num1 and num2 are not equal"<<endl;

   }

   else{

      cout<<"num1 and num2 are equal"<<endl;

   }

   if( num1 > num2 ){

      cout<<"num1 is greater than num2"<<endl;

   }

   else{

      cout<<"num1 is not greater than num2"<<endl;

   }

   if( num1 >= num2 ){

      cout<<"num1 is greater than or equal to num2"<<endl;

   }

   else{

      cout<<"num1 is less than num2"<<endl;

   }

   if( num1 < num2 ){

      cout<<"num1 is less than num2"<<endl;

   }

   else{

      cout<<"num1 is not less than num2"<<endl;

   }

   if( num1 <= num2){

      cout<<"num1 is less than or equal to num2"<<endl;

   }

   else{

      cout<<"num1 is greater than num2"<<endl;

   }

   return 0;

}

Output:

 num1 and num2 are not equal

num1 and num2 are not equal

num1 is greater than num2

num1 is greater than or equal to num2

num1 is not less than num2

num1 is greater than num2


6) Bitwise Operators

There are six bitwise Operators: &, |, ^, ~, <<, >>

 

num1 = 11; /* equal to 00001011*/

num2 = 22; /* equal to 00010110 */

Bitwise operator performs bit by bit processing.

num1 & num2 compares corresponding bits of num1 and num2 and generates 1 if both bits are equal, else it returns 0. In our case it would return: 2 which is 00000010 because in the binary form of num1 and num2 only second last bits are matching.

 

num1 | num2 compares corresponding bits of num1 and num2 and generates 1 if either bit is 1, else it returns 0. In our case it would return 31 which is 00011111

  

num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1 if they are not equal, else it returns 0. In our example it would return 29 which is equivalent to 00011101

 

~num1 is a complement operator that just changes the bit from 0 to 1 and 1 to 0. In our example it would return -12 which is signed 8 bit equivalent to 11110100

 

num1 << 2 is left shift operator that moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. In our case output is 44 which is equivalent to 00101100

 

Note: In the example below we are providing 2 at the right side of this shift operator that is the reason bits are moving two places to the left side. We can change this number and bits would be moved by the number of bits specified on the right side of the operator. Same applies to the right side operator.

 

num1 >> 2 is right shift operator that moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. In our case output is 2 which is equivalent to 00000010

 

Example of Bitwise Operators

#include <iostream>

using namespace std;

int main(){

   int num1 = 11;  /* 11 = 00001011 */

   int num2 = 22;  /* 22 = 00010110 */

   int result = 0;

   result = num1 & num2;

   cout<<"num1 & num2: "<<result<<endl;

   result = num1 | num2;

   cout<<"num1 | num2: "<<result<<endl;

   result = num1 ^ num2;

   cout<<"num1 ^ num2: "<<result<<endl;

   result = ~num1;

   cout<<"~num1: "<<result<<endl;

   result = num1 << 2;

   cout<<"num1 << 2: "<<result<<endl;

   result = num1 >> 2;

   cout<<"num1 >> 2: "<<result;

   return 0;

}

Output:

 num1 & num2: 2

num1 | num2: 31

num1 ^ num2: 29

~num1: -12

num1 << 2: 44 num1 >> 2: 2

7) Ternary Operator

This operator evaluates a boolean expression and assign the value based on the result.

Syntax:

 variable num1 = (expression) ? value if true : value if false

If the expression results true then the first value before the colon (:) is assigned to the variable num1 else the second value is assigned to the num1.

 

Example of Ternary Operator

#include <iostream>

using namespace std;

int main(){

  int num1, num2; num1 = 99;

  /* num1 is not equal to 10 that's why

   * the second value after colon is assigned

   * to the variable num2

   */

  num2 = (num1 == 10) ? 100: 200;

  cout<<"num2: "<<num2<<endl;

  /* num1 is equal to 99 that's why

   * the first value is assigned

   * to the variable num2

   */

  num2 = (num1 == 99) ? 100: 200;

  cout<<"num2: "<<num2;

  return 0;

}

Output:

 num2: 200

num2: 100


Click on given linkπŸ‘‡ 

C++ examples with solutions







Popular posts from this blog

Key Ecommerce Leader (KEL) Pakistan Awards Event 2024 | Pakistan KEL Awards Winner Tayyub Hussnain (Norwich Street Wear) | 2024 Alibaba.com Pakistan Key E-commerce Leader Winner

 Key Ecommerce Leader (KEL) Pakistan Awards Event 2024 On August 20, 2024, Alibaba.com organized an exceptional event at the Pearl Continental Hotel in Lahore. Participants gained insights into global business strategies and discovered the vast opportunities available through Alibaba.com. In an exciting development, Alibaba.com announced the launch of Trade Assurance in Pakistan by the end of August 2024, representing a significant advancement for the Pakistani market. Featured speakers included: 🌟 Andrew Zheng - Vice President, Alibaba.com 🌟 Vianne Wang - Head of Global Supply Chain, Alibaba.com 🌟 Winni Chen - Merchant Development Manager The event was a resounding success, enhancing confidence in the future of eCommerce in Pakistan! πŸ’ͺπŸŽ‰ During the event, six successful business owners were invited to share their stories and insights. Mr.Tayyub Hussnain stood out with his inspiring success story and impressive leadership skills, captivating the audience and judges alike. Ho...

Pakistan celebrities real ages 2021

  Pakistani Celebrities Ages 2021 A few celebrities hide their age since they have to be shape us get it that they are still energetic. So let’s take a see at the Veritable Age of Pakistani Celebrities and see which celebrity ceaselessly lie around his/her age. 1: Ayeza Khan 15 January 1987 2: Mehwish Hayat 6 January 1988 3: Saba Qamar 5 April 1987 4: Sajal Alay 17 January 1994       5: Sara Khan 14 July 1992 6: Iqra Aziz 24 November 1997     7: Alizeh Shah 9 June 2000 8: Zara Noor Abbas 13 March 1991 9: Saboor Aly 3 March 1995 10: Hina Altaf 2 January 1992 11: Sana Javed   25 March 1993 12: Momal Sheikh   6 April 1986 13: Hiba Bukhari 27 July 1993 14: Hira Mani 27 February 1989 15: Aiman Khan 20 November 1998 16: Minal Khan 20 November 1998 17: Kinza Hashmi 7 March 1997 18: Kubra Khan  16 June 1993 19: Mawra Hoccane 28 September 1992 20: Urwa Hoccane 2 July 1991 21: Ha...

Good Morning Wishes