0% found this document useful (0 votes)
27 views

02-Introduction To C++, Variables

The document provides an introduction to C++ programming, covering what C++ is, why it should be used, how to get started, and basic syntax and concepts like variables, user input, comments, and a sample program to add two integers.

Uploaded by

Faneeshwar Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

02-Introduction To C++, Variables

The document provides an introduction to C++ programming, covering what C++ is, why it should be used, how to get started, and basic syntax and concepts like variables, user input, comments, and a sample program to add two integers.

Uploaded by

Faneeshwar Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction To C++ Programming

What is C++ ?

✔ C++ is a cross-platform language that can be used to create high-performance


applications.
✔ C++ was developed by Bjarne Stroustrup, as an extension to the C language.
✔ C++ gives programmers a high level of control over system resources and
memory.
✔ The language was updated 3 major times in 2011, 2014, and 2017 to C++11,
C++14, and C++17.
Why use C++ ?
✔ C++ is one of the world's most popular programming languages.
✔ C++ can be found in today's operating systems, Graphical User Interfaces, and
embedded systems.
✔ C++ is an object-oriented programming language which gives a clear structure to
programs and allows code to be reused, lowering development costs.
✔ C++ is portable and can be used to develop applications that can be adapted to
multiple platforms.
✔ As C++ is close to C# and Java, it makes it easy for programmers to switch to C++
or vice versa
Start with C++

✔ To start using C++, you need two things:


✔ A text editor, like Notepad, to write C++ code.
✔ A compiler, like GCC, to translate the C++ code into a language that the
computer will understand.
First Program : Hello World

#include <iostream> 1) Open note pad, or any text editor and write this
using namespace std; program.
2) Save the program with any name like
int main() { program_name.cpp
cout << "Hello World!"; 3) Open command prompt, compile the program using g++
return 0; command.
} 4) Execute .exe file.
C++ Syntax
#include <iostream> is a header file library that lets
#include <iostream> us work with input and output objects, such
using namespace std; as cout (used in line 5). Header files add functionality to
C++ programs.

using namespace std means that we can use names for


objects and variables from the standard library.

int main() { Another thing that always appear in a C++ program,


is int main(). This is called a function. Any code inside
cout << "Hello World!";
its curly brackets {} will be executed.
return 0;
cout (pronounced "see-out") is an object used together
}
with the insertion operator (<<) to output/print text. In
our example it will output "Hello World".

return 0 ends the main function.

Note: Every C++ statement ends with a semicolon ;.


Output

Output:
#include <iostream>
using namespace std;
Hello World!
I am learning C++
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
C++ Comments

#include <iostream> Output:


using namespace std;
Hello World!
int main() { I am learning C++
// This is Comment
cout << "Hello World!"; ✔ Comments can be used to explain C++ code, and
cout << "I am learning C++"; to make it more readable. It can also be used to
/* line 1 comment prevent execution when testing alternative code.
line 2 comment Comments can be singled-lined or multi-lined.
line 3 comment */ ✔ Single-line comments start with two forward
return 0; slashes (//).
} ✔ Multi-line comments start with /* and ends with */.
C++ Variables

Variables are containers for storing data values.


In C++, there are different types of variables (defined with different keywords), for example:
✔ int - stores integers (whole numbers), without decimals, such as 123 or -123
✔ double - stores floating point numbers, with decimals, such as 19.99 or -19.99
✔ char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
✔ string - stores text, such as "Hello World". String values are surrounded by double quotes
✔ bool - stores values with two states: true or false
C++ Variables

Declaring (Creating) Variables:

Where type is one of C++ types (such as int), and variable is the name of the variable (such
as x or myName). The equal sign is used to assign values to the variable.

Create a variable called myNum of type int and assign it the


int myNum = 15; value 15:
cout << myNum;

Print myNum on the screen.


Program 1:

WAP in C++ to add two integer number.

#include<iostream> Output:
using namespace std;
int main(){ SUM = 15
int x=5;
int y=10;
int sum=x+y;
cout<<"SUM = "<<sum;
return 0;
}
C++ User Input
✔ You have already learned that cout is used to output (print) values. Now we will
use cin to get user input.
✔ cin is a predefined variable that reads data from the keyboard with the extraction
operator (>>).

#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter value of x :";
cin>>x;
cout<<"You have entered the value of x = "<<x;
return 0;
}

Output:

Enter value of x : 15 (Enter)


You have entered the value of x = 15
Program 2:

WAP in C++ to add two integer number. Take integer input from console.

#include<iostream> Output:
using namespace std;
int main(){ Enter value of x: 12 (Enter)
int x,y; Enter value of y: 6 (Enter)
cout<<"Enter value of x: “; SUM = 18
cin>>x;
cout<<"Enter value of y: “;
cin>>y;
int sum=x+y;
cout<<"SUM = "<<sum;
return 0;
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy