SlideShare a Scribd company logo
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

PPT
Lecture#9 Arrays in c++
PPT
C++ Language
PPT
PPTX
POLITEKNIK MALAYSIA
PPTX
Intro to c++
PPTX
Introduction to C++
Lecture#9 Arrays in c++
C++ Language
POLITEKNIK MALAYSIA
Intro to c++
Introduction to C++

What's hot (20)

PPT
C++ functions presentation by DHEERAJ KATARIA
PPTX
Introduction to c++
PDF
PPT
Basics of c++ Programming Language
PPTX
Cs1123 3 c++ overview
PPT
Lecture#8 introduction to array with examples c++
PPT
C++ Overview
PPTX
Learning C++ - Functions in C++ 3
PPT
Lecture#7 Call by value and reference in c++
PPTX
Call by value or call by reference in C++
DOCX
18 dec pointers and scope resolution operator
PDF
Introduction to cpp
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
PPT
Operators in C++
PPTX
Getting started in c++
PPTX
C++ Overview PPT
PPT
C++ functions
C++ functions presentation by DHEERAJ KATARIA
Introduction to c++
Basics of c++ Programming Language
Cs1123 3 c++ overview
Lecture#8 introduction to array with examples c++
C++ Overview
Learning C++ - Functions in C++ 3
Lecture#7 Call by value and reference in c++
Call by value or call by reference in C++
18 dec pointers and scope resolution operator
Introduction to cpp
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
Operators in C++
Getting started in c++
C++ Overview PPT
C++ functions
Ad

Similar to C++ (20)

PPTX
object oriented programming presentation
PPTX
Chapter1.pptx
PPTX
Fundamental of programming Fundamental of programming
PPT
Lecture#2 Computer languages computer system and Programming EC-105
PPT
C++ chapter 2
PPTX
Week7a.pptx
PPT
Practical basics on c++
PPT
Chapter2
PDF
Chap 3 c++
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PPTX
Hello world! Intro to C++
PPTX
Operators1.pptx
PPT
Beginning with C++.ppt thus us beginnunv w
PPTX
#Code2 create c++ for beginners
PPTX
C and C++ programming basics for Beginners.pptx
PPTX
Few Operator used in c++
PPT
C++ Functions.ppt
PPT
C program
PPT
Object Oriented Technologies
PDF
Presentation 2 (1).pdf
object oriented programming presentation
Chapter1.pptx
Fundamental of programming Fundamental of programming
Lecture#2 Computer languages computer system and Programming EC-105
C++ chapter 2
Week7a.pptx
Practical basics on c++
Chapter2
Chap 3 c++
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
Hello world! Intro to C++
Operators1.pptx
Beginning with C++.ppt thus us beginnunv w
#Code2 create c++ for beginners
C and C++ programming basics for Beginners.pptx
Few Operator used in c++
C++ Functions.ppt
C program
Object Oriented Technologies
Presentation 2 (1).pdf
Ad

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Basic Mud Logging Guide for educational purpose
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Renaissance Architecture: A Journey from Faith to Humanism
Week 4 Term 3 Study Techniques revisited.pptx
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Basic Mud Logging Guide for educational purpose
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
Anesthesia in Laparoscopic Surgery in India
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...

C++

  • 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