SlideShare a Scribd company logo
1 of 25
C++
Programming Language
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.
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++.
“HELLO WORLD”
FIRST C++ PROGRAM
#include<iostream>
//Single line comment
using namespace std;
//This is where the execution of program
begins
int main(){
// displays Hello World! on screen
cout<<"Hello World!";
return 0;}
Output:
Hello World!
COMMENTS
• You can see two types of comments in the above program
• Comment doesn’t affect your program logic in any way, you can write whatever you want
in comments but it should be related to the code and have some meaning so that when
someone else look into your code, the person should understand what you did in the code
by just reading your comment.
For example:
/* This function adds two integer numbers
and returns the result as an integer value
*/
int sum(int num1, int num2)
{
return num1+num2;
}
// This is a single line comment
/* This is a multiple line comment
suitable for long comments
*/
#INCLUDE<IOSTREAM>
This statements tells the compiler to include
iostream file. This file contains pre defined
input/output functions that we can use in our
program.
USING NAMESPACE STD;
A namespace is like a region, where we have
functions, variables etc and their scope is limited to
that particular region. Here std is a namespace name,
this tells the compiler to look into that particular
region for all the variables, functions, etc
INT MAIN()
As the name suggests this is the main function of our
program and the execution of program begins with this
function, the int here is the return type which indicates to
the compiler that this function will return a integer value.
That is the main reason we have a return 0 statement at the
end of main function.
COUT << “”;
The cout object belongs to the iostream
file and the purpose of this object is to
display the content between double
quotes as it is on the screen.
CIN>>“”;
This statement is used when we have to
take input from the user.
RETURN 0;
This statement returns value 0 from the main()
function which indicates that the execution of main
function is successful. The value 1 represents failed
execution.
VARIABLES IN C++
A variable is a name which is associated with a value
that can be changed. For example when I write int
num=20; here variable name is num which is associated
with value 20, int is a data type that represents that this
variable can hold integer values.
Syntax of declaring a variable in C++
data_type variable1_name = value1, variable2_name = value2;
For example:
int num1=20, num2=100;
We can also write it like this:
int num1,num2;num1=20;num2=100;
TYPES OF VARIABLES
• int: These type of of variables holds integer value.
• char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.
• bool: holds boolean value true or false.
• double: double-precision floating point value.
• float: Single-precision floating point value.
DATA TYPES IN C++
Data types define the type of data a variable can hold, for example an
integer variable can hold integer data, a character type variable can
hold character data etc.
BUILT IN DATA TYPES
• char: For characters. Size 1 byte.
char ch = 'A';
• int: For integers. Size 2 bytes.
int num = 100;
• float: For single precision floating point. Size 8 bytes.
float num = 123.78987;
• double: For double precision floating point. Size 8 bytes.
double num = 10098.98899;
• bool: For booleans, true or false.
bool b = true;
USER-DEFINED DATA TYPES
• We have three types of user-defined data types in C++
1. struct
2. union
3. enum
DERIVED DATA TYPES IN C++
• We have three types of derived-defined data types in C++
1. Array
2. Function
3. Pointer
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++
 Basic Arithmetic Operators
 Assignment Operators
 Auto-increment and Auto-decrement Operators
 Logical Operators
 Comparison (relational) operators
 Bitwise Operators
 Ternary Operator
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
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
AUTO-INCREMENT AND AUTO-DECREMENT
OPERATORS
• ++ and –
• num++ is equivalent to num=num+1;
• num-- is equivalent to num=num-1;
Example
#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
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()
{ b
ool 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
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.
Exam ple 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 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
THANKYOU


More Related Content

What's hot

What's hot (20)

C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
C++ Ch3
C++ Ch3C++ Ch3
C++ Ch3
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
C++ functions
C++ functionsC++ functions
C++ functions
 

Similar to C++ Programming Language Features in 40 Characters

Similar to C++ Programming Language Features in 40 Characters (20)

Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105
 
C++ chapter 2
C++ chapter 2C++ chapter 2
C++ chapter 2
 
Week7a.pptx
Week7a.pptxWeek7a.pptx
Week7a.pptx
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
Chapter2
Chapter2Chapter2
Chapter2
 
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
 
Hello world! Intro to C++
Hello world! Intro to C++Hello world! Intro to C++
Hello world! Intro to C++
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
C program
C programC program
C program
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
 
Inline function
Inline functionInline function
Inline function
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 

Recently uploaded

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 

Recently uploaded (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 

C++ Programming Language Features in 40 Characters

  • 2. 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. 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++.
  • 3. “HELLO WORLD” FIRST C++ PROGRAM #include<iostream> //Single line comment using namespace std; //This is where the execution of program begins int main(){ // displays Hello World! on screen cout<<"Hello World!"; return 0;} Output: Hello World!
  • 4. COMMENTS • You can see two types of comments in the above program • Comment doesn’t affect your program logic in any way, you can write whatever you want in comments but it should be related to the code and have some meaning so that when someone else look into your code, the person should understand what you did in the code by just reading your comment. For example: /* This function adds two integer numbers and returns the result as an integer value */ int sum(int num1, int num2) { return num1+num2; } // This is a single line comment /* This is a multiple line comment suitable for long comments */
  • 5. #INCLUDE<IOSTREAM> This statements tells the compiler to include iostream file. This file contains pre defined input/output functions that we can use in our program.
  • 6. USING NAMESPACE STD; A namespace is like a region, where we have functions, variables etc and their scope is limited to that particular region. Here std is a namespace name, this tells the compiler to look into that particular region for all the variables, functions, etc
  • 7. INT MAIN() As the name suggests this is the main function of our program and the execution of program begins with this function, the int here is the return type which indicates to the compiler that this function will return a integer value. That is the main reason we have a return 0 statement at the end of main function.
  • 8. COUT << “”; The cout object belongs to the iostream file and the purpose of this object is to display the content between double quotes as it is on the screen.
  • 9. CIN>>“”; This statement is used when we have to take input from the user.
  • 10. RETURN 0; This statement returns value 0 from the main() function which indicates that the execution of main function is successful. The value 1 represents failed execution.
  • 11. VARIABLES IN C++ A variable is a name which is associated with a value that can be changed. For example when I write int num=20; here variable name is num which is associated with value 20, int is a data type that represents that this variable can hold integer values. Syntax of declaring a variable in C++ data_type variable1_name = value1, variable2_name = value2; For example: int num1=20, num2=100; We can also write it like this: int num1,num2;num1=20;num2=100;
  • 12. TYPES OF VARIABLES • int: These type of of variables holds integer value. • char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc. • bool: holds boolean value true or false. • double: double-precision floating point value. • float: Single-precision floating point value.
  • 13. DATA TYPES IN C++ Data types define the type of data a variable can hold, for example an integer variable can hold integer data, a character type variable can hold character data etc.
  • 14. BUILT IN DATA TYPES • char: For characters. Size 1 byte. char ch = 'A'; • int: For integers. Size 2 bytes. int num = 100; • float: For single precision floating point. Size 8 bytes. float num = 123.78987; • double: For double precision floating point. Size 8 bytes. double num = 10098.98899; • bool: For booleans, true or false. bool b = true;
  • 15. USER-DEFINED DATA TYPES • We have three types of user-defined data types in C++ 1. struct 2. union 3. enum
  • 16. DERIVED DATA TYPES IN C++ • We have three types of derived-defined data types in C++ 1. Array 2. Function 3. Pointer
  • 17. 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.
  • 18. TYPES OF OPERATORS IN C++  Basic Arithmetic Operators  Assignment Operators  Auto-increment and Auto-decrement Operators  Logical Operators  Comparison (relational) operators  Bitwise Operators  Ternary Operator
  • 19. 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
  • 20. 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
  • 21. AUTO-INCREMENT AND AUTO-DECREMENT OPERATORS • ++ and – • num++ is equivalent to num=num+1; • num-- is equivalent to num=num-1; Example #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
  • 22. 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() { b ool 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
  • 23. 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.
  • 24. Exam ple 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 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