LECTURE 1.1 - C++ INTRO
LECTURE 1.1 - C++ INTRO
INTRO TO C++
C++ tutorial provides basic and advanced concepts of C++. Our C++ tutorial is designed
for beginners and professionals.
Our C++ tutorial includes all topics of C++ such as first example, control statements,
objects and classes, inheritance, constructor, destructor, this, static, polymorphism,
abstraction, abstract class, interface, namespace, encapsulation, arrays, strings,
exception handling, File IO, etc.
What is C++
C++ is a general purpose, case-sensitive, free-form programming language that supports
object-oriented, procedural and generic programming.
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
C++ Standard Libraries
Standard C++ programming is divided into three important parts:
The core library includes the data types, variables and literals, etc.
The standard library includes the set of functions manipulating strings, files, etc.
The Standard Template Library (STL) includes the set of methods manipulating a
data structure.
Usage of C++
By the help of C++ programming language, we can develop different types of secured
and robust applications:
Window application
Client-Server application
Device drivers
Embedded firmware etc
C++ Program
In this tutorial, all C++ programs are given with C++ compiler so that you can easily
change the C++ program code.
File: main.cpp
1. #include <iostream>
3. int main() {
5. return 0;
6. }
7) In C, scanf() and printf() are C++ mainly uses stream cin and
mainly used for input/output. cout to perform input and output
operations.
10) C does not provide the feature of C++ supports the feature of
namespace. namespace.
C++ history
History of C++ language is interesting to know. Here we are going to discuss brief history of C++ lang
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T
& Telegraph), located in U.S.A.
It was develop for adding a feature of OOP (Object Oriented Programming) in C without significantly
component.
C++ programming is "relative" (called a superset) of C, it means any valid C program is also a valid C++
Let's see the programming languages that were developed before C++ language.
C++ Program
Before starting the abcd of C++ language, you need to learn how to write, compile and run
the first C++ program.
To write the first C++ program, open the C++ console and write the following code:
1. #include <iostream.h>
2. #include<conio.h>
3. void main() {
4. clrscr();
6. getch();
7. }
void main() The main() function is the entry point of every program in C++
language. The void keyword specifies that it returns no value.
cout << "Welcome to C++ Programming." is used to print the data "Welcome to
C++ Programming." on the console.
getch() The getch() function asks for a single character. Until you press any key, it
blocks the screen.
By menu
Now click on the compile menu then compile sub menu to compile the c++ program.
Then click on the run menu then run sub menu to run the c++ program.
By shortcut
Or, press ctrl+f9 keys compile and run the program directly.
You can view the user screen any time by pressing the alt+f5 keys.
If bytes flow from main memory to device like printer, display screen, or a network
connection, etc, this is called as output operation.
If bytes flow from device like printer, display screen, or a network connection, etc to main
memory, this is called as input operation.
<iostream It is used to define the cout, cin and cerr objects, which
> correspond to standard output stream, standard input stream
and standard error stream, respectively.
1. #include <iostream>
3. int main( ) {
5. cout << "Value of ary is: " << ary << endl;
6. }
Output:
1. #include <iostream>
3. int main( ) {
4. int age;
5. cout << "Enter your age: ";
7. cout << "Your age is: " << age << endl;
8. }
Output:
1. #include <iostream>
3. int main( ) {
7. }
Output:
C++ Variable
A variable is a name of memory location. It is used to store data. Its value can be changed
and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
1. type variable_list;
1. int x;
2. float y;
3. char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
2. float f=30.8;
3. char c='A';
1. int a;
2. int _ab;
3. int a30;
1. int 4;
2. int x y;
3. int double;
The memory size of basic data types may change according to 32-Z or 64-bit operating
system.
Let's see the basic data types. It size is given according to 32 bit OS.
float 4 byte
double 8 byte
C++ Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name etc. A
list of 32 Keywords in C++ Language which are also available in C language are
given below.
A list of 30 Keywords in C++ Language which are not available in C language are
given below.
asm dynamic_cast namespac reinterpret_cast bool
e
C++ Operators
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise etc.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operator
Unary operator
Ternary or Conditional Operator
Misc Operator
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operators direction to be evaluated, it may be left to right or right
to left.
1. int data=5+10*10;
The "data" variable will contain 105 because * (multiplicative operator) is evaluated before
+ (additive operator).
2) Assignment Operators
Assignments operators in C++ are: =, +=, -=, *=, /=, %=
4) Logical Operators
Logical Operators are used with binary variables. They are mainly used in
conditional statements and loops for evaluating a condition.
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.
b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1
5) 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 or equal to right side.
<= returns true if left side is less than or equal to right side.
6) Bitwise Operators
There are six bitwise Operators: &, |, ^, ~, <<, >>
num1 | num2 compares corresponding bits of num1 and num2 and generates 1
if either bit is 1, else it returns 0. In our case it would return 31 which is 00011111
num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1
if they are not equal, else it returns 0. In our example it would return 29 which is
equivalent to 00011101
~num1 is a complement operator that just changes the bit from 0 to 1 and 1 to 0.
In our example it would return -12 which is signed 8 bit equivalent to 11110100
num1 << 2 is left shift operator that moves the bits to the left, discards the far left
bit, and assigns the rightmost bit a value of 0. In our case output is 44 which is
equivalent to 00101100
Note: In the example below we are providing 2 at the right side of this shift
operator that is the reason bits are moving two places to the left side. We can
change this number and bits would be moved by the number of bits specified on
the right side of the operator. Same applies to the right side operator.
num1 >> 2 is right shift operator that moves the bits to the right, discards the far
right bit, and assigns the leftmost bit a value of 0. In our case output is 2 which is
equivalent to 00000010
7) Ternary Operator
This operator evaluates a boolean expression and assign the value based on the
result.
Syntax:
num2: 200
num2: 100