Lecture 1
Lecture 1
Sandeep Kumar
Introduction:
● C++ is a object oriented programming language (a superset of C).
● C++ = C + objects
● C++ was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill,
New Jersey, USA, in the early of the 1980 .
● Interpreted Languages: Python, Perl, BASIC, Mathematica code
● Compiled Languages : C, C++, Java, FORTRAN ...
Code is first processed in a form which can be understood by the machine. Complied
first translates the source file into object file. The loader (linker) further combines
object files into an executable file which can be run.
●
File Name
● The source file name first.cpp has two parts. Name of the file and its extension.
All C++ source file are saved with the extension .cpp.
● The source file can be compiled with the command g++, which invokes the
C++ compiler and generates the object file.
● By default the object file is given the name a.out.
● To run the object file the command is : ./a.out
● A different name to the object file can be given by the command
Now the object file is saved as first.out, which can be run with ./first.out
Introduction:
● #inlcude<iostream> commands includes the iostream library in the
source file, that includes standard basis C++ inbuilt input/output and
other functions. Compiler knows where to locate this file. Not a C++
statement.
● Namespace is a new concept introduced in the ANSI (American
National Standard Institute ) C++ committee. It provides the scope to
the variables and hold them globally.
● std is the namespace where ANSI standard class libraries are defined.
● Keywords: the words which are reserved in C++. e.g - iostream, std,
using, include, cout, int, return …
Output Operator
Each data is storage in the form of bits (0/1) in computer. Suppose we can represent
a particular data type in 8 number of bits:
Then the largest number it can store is 11111111 (28 -1 ) and smallest number is
00000000 (equals to 0).
If the data type is signed (+ or -), then one bit is reserved for sign and range of
data is (-28-1 to 28-1 -1).
Bit/Byte
1 Bit = 0/1
1 Byte = 8 bits
2 Byte = 16 bits
4 Byte = 32 bits
Data Types and their sizes
Integer Type:
Syntax: int varibale_name
Variable Declaration:
int x;
Variable Initialization:
x = 5;
We can also define as: Or can also declare and initialize together:
int x = 3;
int x, y; int y = 4;
Integer Overflow:
bool type:
● bool type data is used to represent logical TRUE (1) or FALSE (0)
● They are integer type of data.
char type:
● Individual characters can be stored in char type variables.
ASCII Value
Printing ASCII values:
String :
Textual data can be handled by arrays of characters or by the data type string.
Floating point numbers:
● The maximum number of significant digits, i.e., the number of decimal place, in float values is 6 or 7, while in
double type is 15. The maximum number of significant digits is called precision .
● Why not use float or double type always in place of int type?
Arithmetic Operators
If a operator is applied on same type of data, then
the output is also of same type.
● If we want to have output of x/y = 16/5 equals to 3.2, then we have to
convert, at least one of x or y to float or double. Which can be done as
#include<cmath>
Comparison/Relational and Logical Operators:
Complex Numbers:
Library: #include<complex>
A complex number is of type a+ib, where a and b can be both long or double.
E.g.
complex<double> z1; (z1 is a complex numbers with both real and imaginary parts
as double).
complex<long> z2 (z2 is a complex numbers with both real and imaginary parts as
long).
typedef
In C++, typedef ( type definition ) is a keyword allows us to create an alias for existing data types. Once, we have created a
type definition, then we can declare a variable using the alias we created.
E.g.