0% found this document useful (0 votes)
7 views22 pages

LECTURE 1.1 - C++ INTRO

The document is a comprehensive tutorial on C++, covering both basic and advanced concepts, including object-oriented programming principles, standard libraries, and data types. It provides practical examples of C++ programs, input/output operations, and the differences between C and C++. Additionally, it outlines the history of C++, its keywords, operators, and rules for defining variables.

Uploaded by

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

LECTURE 1.1 - C++ INTRO

The document is a comprehensive tutorial on C++, covering both basic and advanced concepts, including object-oriented programming principles, standard libraries, and data types. It provides practical examples of C++ programs, input/output operations, and the differences between C and C++. Additionally, it outlines the history of C++, its keywords, operators, and rules for defining variables.

Uploaded by

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

PROGRAMMING 2

INTRO TO C++
C++ tutorial provides basic and advanced concepts of C++. Our C++ tutorial is designed
for beginners and professionals.

C++ is an object-oriented programming language. It is an extension to C programming.

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.

C++ is a middle-level language, as it encapsulates both high- and low-level language


features.

Object-Oriented Programming (OOPs)


C++ supports the object-oriented programming, the four major pillar of object-oriented
programming used in C++ are:

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>

2. using namespace std;

3. int main() {

4. cout << "Hello C++ Programming";

5. return 0;

6. }

A detailed explanation of first C++ program is given in next chapters.


No. C C++ C vs C++
1) C follows the procedural style C++ is multi-paradigm. It supports
programming. both procedural and object
oriented.

2) Data is less secured in C. In C++, you can use modifiers for


class members to make it
inaccessible for outside users.

3) C follows the top-down C++ follows the bottom-up


approach. approach.

4) C does not support function C++ supports function


overloading. overloading.

5) In C, you can't use functions in In C++, you can use functions in


structure. structure.

6) C does not support reference C++ supports reference variables.


variables.

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.

8) Operator overloading is not Operator overloading is possible in


possible in C. C++.

9) C programs are divided C++ programs are divided


into procedures and modules into functions and classes.

10) C does not provide the feature of C++ supports the feature of
namespace. namespace.

11) Exception handling is not easy in C++ provides exception handling


C. It has to perform using other using Try and Catch block.
functions.
C++ history
History of C++ language is interesting to know. Here we are going to discuss brief history
of C++ language.

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell


laboratories of AT&T (American Telephone & Telegraph), located in U.S.A.

Bjarne Stroustrup is known as the founder of C++ language.

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.

Bjarne Stroustrup is known as the founder of C++ language.

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.

Language Year Developed By


Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis Ritchie

C++ 1980 Bjarne Stroustrup

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();

5. cout << "Welcome to C++ Programming.";

6. getch();

7. }

#include<iostream.h> includes the standard input output library functions. It


provides cin and cout methods for reading from input and writing to output respectively.
#include <conio.h> includes the console input output library functions. The getch()
function is defined in conio.h file.

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.

How to compile and run the C++ program


There are 2 ways to compile and run the C++ program, by menu and by shortcut.

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 will see the following output on user screen.

You can view the user screen any time by pressing the alt+f5 keys.

Now press Esc to return to the turbo c++ console.

C++ Basic Input/Output


C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of
data. It makes the performance fast.

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.

I/O Library Header Files


Let us see the common header files used in C++ programming are:

Header File Function and Description

<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.

<iomanip> It is used to declare services useful for performing formatted I/O,


such as setprecision and setw.

<fstream> It is used to declare services for user-controlled file processing.

Standard output stream (cout)


The cout is a predefined object of ostream class. It is connected with the standard output
device, which is usually a display screen. The cout is used in conjunction with stream
insertion operator (<<) to display the output on a console

Let's see the simple example of standard output stream (cout):

1. #include <iostream>

2. using namespace std;

3. int main( ) {

4. char ary[] = "Welcome to C++ tutorial";

5. cout << "Value of ary is: " << ary << endl;

6. }

Output:

Value of ary is: Welcome to C++ tutorial

Standard input stream (cin)


The cin is a predefined object of istream class. It is connected with the standard input
device, which is usually a keyboard. The cin is used in conjunction with stream extraction
operator (>>) to read the input from a console.

Let's see the simple example of standard input stream (cin):

1. #include <iostream>

2. using namespace std;

3. int main( ) {

4. int age;
5. cout << "Enter your age: ";

6. cin >> age;

7. cout << "Your age is: " << age << endl;

8. }

Output:

Enter your age: 22


Your age is: 22

Standard end line (endl)


The endl is a predefined object of ostream class. It is used to insert a new line characters
and flushes the stream.

Let's see the simple example of standard end line (endl):

1. #include <iostream>

2. using namespace std;

3. int main( ) {

4. cout << "C++ Tutorial";

5. cout << " Javatpoint"<<endl;

6. cout << "End of line"<<endl;

7. }

Output:

C++ Tutorial Javatpoint


End of line

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.

Let's see the syntax to declare a variable:

1. type variable_list;

The example of declaring variable is given below:

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:

1. int x=5,b=10; //declaring 2 variable of integer type

2. float f=30.8;

3. char c='A';

Rules for defining variables


 A variable can have alphabets, digits and underscore.
 A variable name can start with alphabet and underscore only. It can't start with digit.
 No white space is allowed within variable name.
 A variable name must not be any reserved word or keyword e.g. char, float etc.

Valid variable names:

1. int a;

2. int _ab;

3. int a30;

Invalid variable names:

1. int 4;
2. int x y;

3. int double;

C++ Data Types


A data type specifies the type of data that a variable can store such as integer, floating,
character etc.

There are 4 types of data types in C++ language.

Types Data Types

Basic Data Type int, char, float, double, etc

Derived Data Type array, pointer, etc

Enumeration Data Type enum

User Defined Data Type structure

Basic Data Types


The basic data types are integer-based and floating-point based. C++ language supports
both signed and unsigned literals.

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.

Data Types Memory Size Range

char 1 byte -128 to 127

signed char 1 byte -128 to 127

unsigned char 1 byte 0 to 127

short 2 byte -32,768 to 32,767

signed short 2 byte -32,768 to 32,767

unsigned short 2 byte 0 to 32,767

int 2 byte -32,768 to 32,767

signed int 2 byte -32,768 to 32,767

unsigned int 2 byte 0 to 32,767

short int 2 byte -32,768 to 32,767

signed short int 2 byte -32,768 to 32,767

unsigned short 2 byte 0 to 32,767


int
long int 4 byte

signed long int 4 byte

unsigned long int 4 byte

float 4 byte

double 8 byte

long double 10 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.

auto break case char const continu default do


e

doubl else enum exter float for goto if


e n

int long registe return short signed sizeof stati


r c

struct switc typedef union unsigne void volatil while


h d e

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

explicit new static_cast false catch

operator template friend private class

this inline public throw const_cast

delete mutable protected true try

typeid typename using virtual wchar_t

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.

There are following types of operators to perform different types of operations in C


language.

 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.

Let's understand the precedence by the example given below:

1. int data=5+10*10;

The "data" variable will contain 105 because * (multiplicative operator) is evaluated before
+ (additive operator).

The precedence and associativity of C++ operators is given below:

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left


Multiplicative */% Left to right

Additive +- Right to left

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == !=/td> Right to left

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Right to left

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= | Right to left


=

Comma , Left to right

Operators in C++ - Expanded


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

2) 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

3) Auto-increment and Auto-decrement


Operators
++ and —
num++ is equivalent to num=num+1;

num–- is equivalent to num=num-1;

Example of Auto-increment and Auto-decrement


Operators
#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

4) 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(){
bool 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

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 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.

Example 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 not 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

6) Bitwise Operators
There are six bitwise Operators: &, |, ^, ~, <<, >>

num1 = 11; /* equal to 00001011*/


num2 = 22; /* equal to 00010110 */
Bitwise operator performs bit by bit processing.
num1 & num2 compares corresponding bits of num1 and num2 and generates 1
if both bits are equal, else it returns 0. In our case it would return: 2 which is
00000010 because in the binary form of num1 and num2 only second last bits
are matching.

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

Example of Bitwise Operators


#include <iostream>
using namespace std;
int main(){
int num1 = 11; /* 11 = 00001011 */
int num2 = 22; /* 22 = 00010110 */
int result = 0;
result = num1 & num2;
cout<<"num1 & num2: "<<result<<endl;
result = num1 | num2;
cout<<"num1 | num2: "<<result<<endl;
result = num1 ^ num2;
cout<<"num1 ^ num2: "<<result<<endl;
result = ~num1;
cout<<"~num1: "<<result<<endl;
result = num1 << 2;
cout<<"num1 << 2: "<<result<<endl;
result = num1 >> 2;
cout<<"num1 >> 2: "<<result;
return 0;
}
Output:

num1 & num2: 2


num1 | num2: 31
num1 ^ num2: 29
~num1: -12
num1 << 2: 44 num1 >> 2: 2

7) Ternary Operator
This operator evaluates a boolean expression and assign the value based on the
result.
Syntax:

variable num1 = (expression) ? value if true : value if false


If the expression results true then the first value before the colon (:) is assigned
to the variable num1 else the second value is assigned to the num1.

Example of Ternary Operator


#include <iostream>
using namespace std;
int main(){
int num1, num2; num1 = 99;
/* num1 is not equal to 10 that's why
* the second value after colon is assigned
* to the variable num2
*/
num2 = (num1 == 10) ? 100: 200;
cout<<"num2: "<<num2<<endl;
/* num1 is equal to 99 that's why
* the first value is assigned
* to the variable num2
*/
num2 = (num1 == 99) ? 100: 200;
cout<<"num2: "<<num2;
return 0;
}
Output:

num2: 200
num2: 100

You might also like