document 1
document 1
Introduction to C++
Introduction to C++
C++ is a middle-level programming language developed by Bjarne Stroustrup in 1979 at Bell Labs
C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. C++ is closer
to the compiler and faster than C language
C++ is both a procedural and an object-oriented programming language. It supports OOP features such as
polymorphism, encapsulation, and inheritance. As C++ is an object-oriented programming language so it
gives a clear structure to programs and allows code to be reused, lowering development costs.
Variables
A variable is the title of a reserved region allocated in memory. In other words, it may be referred
<data_type> <variable_name>
Example:
int x = 10; // here int is the data type and x is variable
int y = 50;
Changing values of variables: A variable's value can also be changed in the program.
Example:
int x = 10;
int z = 30;
NOTE:
Assigning value to variable while declaring is optional
Variables Naming Rules
Variables can start from an alphabet or underscore _ or $
Special characters except _ and $ are not allowed
Some particular keywords are not allowed
Commas or blanks are not allowed.
Data types specify the different sizes and values that can be stored in the variable.
Hence, by assigning different data types, we can store integers, decimals, or characters in these variables.
Java
C++ &+ DSA
DSA
The different types are :
C++ float : float is used to store floating-point numbers (decimals and exponentials)
C++ double: double is used to store floating-point numbers (decimals and exponentials)
Identifiers in C++
It is a name given to a package, class, interface, method, or variable. All identifiers must have
different names.
All identifiers should begin with a letter (A to Z or a to z), $ and _ and must be unique
After the first character/letter, identifiers can have any combination of characters
abc, _abc, __xyz Output in C++ In C++, all lines that start with a pound or hashtag # sign are called directives
and are processed by a preprocessor which is a program invoked by the compiler. #include<iostream> tells the
preprocessor to include the iostream header file(file that has some already pre-written code which we are
importing to avoid reinventing the wheel) in the program and iostream is the header file which contains all the
basic functions of the program like input/output etc. Using namespace std is used to define which input/output
form is going to be used. (Explained in the forthcoming lectures). int main(): This line is used to declare a
function having the name “main” whose return type is integer. Every C++ program’s execution begins with the
Example:
#include <iostream>
int main()
Java
C++ &+ DSA
DSA
NOTE :
This can be done using cin . cin is a predefined object that reads data from the user with the extraction
Example:
#include <iostream>
int main() {
int x, y;
sum = x + y;
// prints sum
return 0;
Q1. Take two integers input, x and y : x>y, and find the remainder when x is divided by y.
Solution:
#include <iostream>
int main() {
int x, y;
// prints remainder
return 0;
#include <iostream>
int main() {
float sub1,sub2,sub3,sub4,sub5;
cin >> sub1 >> sub2 >> sub3 >> sub4 >> sub5;
// prints percent
return 0;
Java
C++ &+ DSA
DSA
Q3. Calculating Area of a Circle
#include <iostream>
int main() {
int radius;
cin>>radius;
// prints area
return 0;
C++ Operators
Operators are the symbols that are used to perform pre-defined operations on variables and values
(commonly referred to as operands).
Arithmetic Operators: They are used in mathematical expressions in a program. They function in
Example:
#include<iostream>
int main() {
int x = 5;
int y = 10;
Java
C++ &+ DSA
DSA
C++ Relational Operators:
C++ Logical Operators : Logical operators are used for decision making. This class of operators is used to check
whether an expression is true or false. Some of the commonly used logical operators are mentioned in the table
below.
Operator Example Meaning && (Logical AND) expression1 && expression2 true only if both expression1 and
expression2 are true | (Logical OR) expression1 | expression2 true if either expression1 or expression2 is true !
(Logical NOT) !expression true if expression is false and vice versa
left-hand operand.
= p = q; p = q;
+= p += q; p = p + q;
-= p -= q; p = p - q;
*= p *= q; p = p * q;
/= p /= q; p = p / q;
%= p %= q; p = p % q;
Java
C++ &+ DSA
DSA
Bitwise Operators :
Operator Description
~ Bitwise Complemen
<< Left Shif
>> Right Shif
>>> Unsigned Right Shif
& Bitwise AN
^ Bitwise exclusive OR
Miscellaneous Operators:
1 sizeof
This operator is used to compute the size of a variable. For example, sizeof(p) ,
Conditional operator Condition is true then it returns the value of Expression1 otherwise
and unions.
The dot operator is used for the actual object. The arrow operator is used with a
pointer to an object.
5&
Address-of operator & returns the memory address of a variable. For example &p; will
Associativity specifies the order in which operators are evaluated by the compiler, which can be
Java
C++ &+ DSA
DSA
Category Operators Associativity
int main() {
float p,r,t,si;
p = 100;
r = 10;
t = 2;
si = (p*r*t)/100;
cout<<si;
Java
C++ &+ DSA
DSA
Q5. Which of the following statements is false
(1) Each new C++ instruction has to be written on a separate line
(2) Usually all C++ statements are entered in small case letters
(3) Blank spaces may be inserted between two words in a C++ statement
Solution:
Fals
Fals
Tru
true
(2) 3
(3) 2
(4) 0
Solution: Option 3) 2
Q7. What will be the value of d if d is a float after the operation d = 2 / 7.0?
(1) 0
(2) 0.2857
Java
C++ &+ DSA
DSA
THANK
YOU !