Introduction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

Introduction,Token,Expression,

Control Statement
Prachi Dhannawat
Asst. Prof.
UMIT, SNDT Women’s University, Mumbai
Introduction
● Object Oriented Programming is a paradigm that provides many concepts such as
inheritance, data binding, polymorphism etc.
● The programming paradigm where everything is represented as an object is known as
truly object-oriented programming language.
● Smalltalk is considered as the first truly object-oriented programming language.
● Object means a real word entity such as pen, chair, table etc.
Object Oriented Paradigm, Class and Object

Fig. 1 Fig. 2
Multiple Views of Object Oriented Tree
Object-Oriented Programming

Object-Oriented Programming is a methodology or paradigm to design a program using


classes and objects. It simplifies the software development and maintenance by providing
some concepts:

● Object
● Class
● Inheritance
● Polymorphism
● Abstraction
● Encapsulation
Advantage of OOPs over Procedure-oriented programming language
1. OOPs makes development and maintenance easier where as in Procedure-oriented
programming language it is not easy to manage if code grows as project size grows.
2. OOPs provide data hiding whereas in Procedure-oriented programming language a
global data can be accessed from anywhere.
3. OOPs provide ability to simulate real-world event much more effectively. We can
provide the solution of real word problem if we are using the Object-Oriented
Programming language.
4. C++ is an object-oriented programming language that gives a clear structure to programs
and allows code to be reused, lowering development costs.
Characteristics of some OOP Languages
C++
● C++ is a cross-platform language that can be used to create high-performance applications.
● C++ is a programming language developed by Bjarne Stroustrup at Bell Labs circa 1980.
● 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.
● C++ supports the object-oriented programming, the four major pillar of object-oriented
programming (OOPs) 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.
● The language was updated 4 major times in 2011, 2014, and 2017 to C++11, C++14,
and C++17and C++21.
Usage of C++ and Need even Today
In C++ programming language, we can develop different types of secured and robust applications:

● Window application Eg: Microsoft Office


● Client-Server application
● Device drivers
● Embedded firmware etc
● Video game development. Many major video game studios use Unity Engine, PhyreEngine, and Unreal;
all are based on C++.
● Mozilla Firefox, Adobe applications, Chromium-based web browsers, and MySQL are all primarily
developed in C++.
● Operating systems such as Microsoft Windows and the macOS are to some extent developed using C++.
● Python interpreter is written in C++
Due to its incredible reliability, performance, and versatility, C++ remains in high
demand even in 2022.
C++ Program Structure
C++ Program
1. // C++ Hello Program
2. #include <iostream>
3. using namespace std;
4. int main( )
5. {
6. cout << "Hello C++ Programming";
7. return 0;
8. }
C++ Program
C++ Program : String1.cpp
● Things to note:
● Strings can be
concatenated with a +
operator.
● No messing with null
terminators or strcat() as in
C
● C++ strings are mutable –
they can be changed in
place.
C++ Program : String2.cpp

//Cout prints integers without any modification


C++ Program : cout1.cpp
#include <iostream>
Manipulators endl:
using namespace std; causes insertion point to move to
int main() { beginning of next line
cout << 29/7 << endl;
cout << "Hello there" << endl;
cout << 12 << endl;
cout << "4 + 7" << endl;
cout << 'A' << endl;
cout << "4 + 7 = " << 4 + 7 << endl;
cout << "Hello \n there" << endl;
return 0; }
C++ Comments
Difference between C and C++
Difference between C and C++ continue:
C++ Compilers
● MinGW / GCC

● Borland c++

● Dev C++

● Embarcadero

● Clang

● Visual C++

● Intel C++

● Code Block

● Turbo C++

Different IDE can be used like VSCode, Eclipse, etc.


C++ Program using cin:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "Enter a number" << endl;
cin >> al;
cout << "Your Number: " << a << endl;
return 0; }
C++ Tokens
C++ Tokens are the smallest individual units of a program.

C++ is the superset of C and so most constructs of C are legal in C++ with their meaning
and usage unchanged. So tokens, expressions, and data types are similar to that of C.

Following are the C++ tokens : (most of c++ tokens are basically similar to the C tokens)

● Keywords
● Identifiers
● Constants
● Variables
● Operators
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.
C++ Keywords

A list of 30 Keywords in C++ Language which are not available in C language are
given below.
C++ Identifiers
C++ identifiers in a program are used to refer to the name of the variables, functions, arrays,
or other user-defined data types created by the programmer.

They are the basic requirement of any language. Every language has its own rules for
naming the identifiers.

The C++ identifiers represent the essential elements in a program which are given below:

● Constants
● Variables
● Functions
● Labels
● Defined data types
C++ Identifiers
The general rules for naming variables are:

● Names can contain letters, digits and underscores


● Names must begin with a letter or an underscore (_)
● Names are case sensitive (myVar and myvar are different variables)
● Names cannot contain whitespaces or special characters like !, #, %, etc.
● Reserved words (like C++ keywords, such as int) cannot be used as names
Valid Identifiers Invalid Identifiers

1. Result 1. Sum-1 // containing special character '-'.


2. Test2 2. 2data // the first letter is a digit.
3. _sum 3. break // use of a keyword.
4. power
C++ Identifiers Example
#include <iostream>
using namespace std;
int main( ) {
int minutesPerHour = 60; // Good name
int m = 60; // OK, but not so easy to understand what m actually is
cout << minutesPerHour << "\n";
cout << m;
return 0;
}
Differences between Identifiers and Keywords
C++ escape Sequences
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.

● Arithmetic Operators
● Relational Operators
● Logical Operators
● Bitwise Operators
● Assignment Operator
● Unary operator
● Ternary or Conditional Operator
C++ Operators Types
The precedence and associativity of C++ operators:
The precedence and associativity of C++ operators:
Misc Operators
Misc Operators
Scope Resolution Operator
In C++, scope resolution operator is ::. It is used for following purposes.
1) To access a global variable when there is a local variable with same name:
2) To define a function outside a class.
#include<iostream>
3) To access a class’s static variables. using namespace std;
int x; // Global x
int main()
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0; }
Few More Operators
In C++, scope resolution operator is ::. It is used for following purposes.
1) Memory Management Operators: new, delete
2) Manipulators: eg.endl, setw( )
3) Type cast operator
Data Types
C++ Data Types
Built in Data Types Size and Range
User-Defined DataTypes:
The data types that are defined by the user are called the derived datatype or user-defined
derived data type.
These types include:

● Class
● Structure
● Union
● Enumeration
● Typedef defined DataType

Reference Variable
When a variable is declared as a reference, it becomes an alternative name for an
existing variable.
● A variable can be declared as a reference by putting ‘&’ in the declaration.
#include <iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x; // ref is a reference to x.
ref = 20; // Value of x is now changed to 20
cout << "x = " << x << '\n';
x = 30; // Value of x is now changed to 30
cout << "ref = " << ref << '\n';
cout<< &x << endl;
cout<< &ref << endl;
return 0; }
C++ Expression

● C++ expression consists of operators, constants, and variables which are arranged
according to the rules of the language.
● It can also contain function calls which return values. An expression can consist of
one or more operands, zero or more operators to compute a value.
● Every expression produces some value which is assigned to the variable with the help
of an assignment operator.
C++ Expression Types
C++ Control Structures
C++ Stream IO
C++ Stream IO Continues:
● Input Stream: If the direction of flow of bytes is from the device(for example,
Keyboard) to the main memory then this process is called input.
● Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device( display screen ) then this process is called outpu t.
● Header files available in C++ for Input/Output operations are:
1. iostream: iostream stands for standard input-output stream. This header file
contains definitions of objects like cin, cout, cerr, etc.
2. iomanip: iomanip stands for input-output manipulators. This file contains
definitions of setw, setprecision, etc.
3. fstream: This header file mainly describes the file stream.
C++ Stream IO Continues: (Predefined Streams)
● Standard output stream (cout): Usually the standard output device is the display screen.
● The C++ cout statement is the instance of the ostream class. It is used to produce output on the
standard output device which is usually the display screen. The data needed to be displayed on the
screen is inserted in the standard output stream (cout) using the insertion operator(<<).
● standard input stream (cin): Usually the input device in a computer is the keyboard. C++ cin statement is
the instance of the class istream and is used to read input from the standard input device which is usually a
keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction
operator extracts the data from the object cin which is entered using the keyboard.
● Un-buffered standard error stream (cerr): The C++ cerr is the standard error stream that is used to
output the errors. This is also an instance of the iostream class. As cerr in C++ is un-buffered it is used
when one needs to display the error message immediately.
● buffered standard error stream (clog): This is also an instance of ostream class and used to display
errors but unlike cerr the error is first inserted into a buffer and is stored in the buffer until it is not fully filled.
C++ Program : I/O Streams
#include<iostream>
using namespace std;
int main()
{
char sample[] = "GeeksforGeeks";
cout << sample << " - A computer science portal for geeks"<< endl;
int age;
cout << "\nEnter your age:";
cin >> age;
cout << "\nYour age is: " << age;
cerr << "\n An error occurred";
clog << "\n An error occurred"; return 0; }
Unformatted Input/Output operations
1. Using objects cin and cout
2. put() and get() functions:
3. getline() and write() functions:

● The key point to remember is that the write() function does not stop displaying the
string automatically when a NULL character occurs.
● If the size is greater than the length of the line then, the write() function displays
beyond the bound of the line.
Unformatted Input/Output operations Examples
#include <iostream>
#include <iostream>
#include <iostream>
using namespace std;
using namespace std; #include <string>
int main()
using namespace std;
{
// Driver Code int main()
char data;
int main() {
int count = 0;
{ char line[100];
int data;
cout << "Enter Data: ";
char val; // Get the input
// Get the data
cin.getline(line, 10);
cin.get(data);
// Input the data
cout<<"Enter value of data and // Print the data
while (data != '\n') {
val"; cout.write(line, 5);
// Print the data
cin >> data; cout << endl;
cout.put(data);
cin >> val;
count++;
// Print the data
// Print the data cout.write(line, 20);
// Get the data again
cout << data << " " << val;
cin.get(data);
return 0; cout << endl;
}
} return 0;
return 0; }
}
Formatted Input/Output
There are two ways to do so:

1. Using the ios class or various ios member functions.


2. Using manipulators(special functions)
Formatting using the ios members: Few standard ios class functions are:
1. width(): The width method is used to set the required field width. The output will be
displayed in the given width
2. precision(): The precision method is used to set the number of the decimal point to a
float value
3. fill(): The fill method is used to set a character to fill in the blank space of a field
4. setf(): The setf method is used to set various flags for formatting output
C++ Program : Formatted Input/Output
#include<bits/stdc++.h>
using namespace std;

// The width() function defines width of the next value to be displayed in the output at the console.
// The fill() function fills the unused white spaces in a value, with a character of choice.
int main()
{
cout << "--------------------------\n";
char c = 'A';
cout.width(5); // Adjusting width will be 5.
cout << c <<"\n";
int temp = 10;
cout<<temp;
cout << "\n--------------------------\n";

cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
cout<<3.1422;
cout << "\n--------------------------\n";
C++ Program : Formatted Input/Output Continues
char ch = 'a';
// Calling the fill function to fill the white spaces in a value with a character
cout.fill('*');
cout.width(10);
cout<<ch <<"\n";
int i = 1;
// Once you call the fill() function,you don't have to call it again
cout.width(5);
cout<<i;
cout << "\n--------------------------\n";
int val1=100,val2=200;
cout.setf(ios::showpos);
cout<<val1<<" "<<val2;
cout.setf(ios::showpos|ios::showpoint);
// Clear the showflag flag without affecting the showpoint flag
cout<<200.0<<endl;
cout.unsetf(ios::showpos);
cout<<200.0;
cout << "\n--------------------------\n";
return 0; }
Formatting using Manipulators
The standard manipulators are shown below:
To access manipulators that take
● boolalpha: used to turn on bool alpha flag parameters (such as setw( )), you must
● dec: used to turn on the dec flag include “iomanip” header file in your
program.
● endl: used to Output a newline character.
● and: to Flush the stream
● ends: to Output a null
● fixed: to Turns on the fixed flag
● flush: to Flush a stream
● hex: Turns on hex flag
● internal: Turns on internal flag
● left: Turns on the left flag
Formatting using Manipulators
● noboolalpha: Turns off bool alpha flag
● noshowbase: Turns off showcase flag
● noshowpoint: Turns off show point flag
○ noshowpos: Turns off showpos flag
○ noskipws: Turns off skipws flag
○ nounitbuf: Turns off the unit buff flag
○ nouppercase: Turns off the uppercase flag
○ oct: Turns on oct flag
○ resetiosflags(fmtflags f): Turns off the flag specified in f
○ right: Turns on the right flag
○ scientific: Turns on scientific flag
Formatting using Manipulators
● setbase(int base): To Set the number base to base
● setfill(int ch): Set the fill character to ch
● setiosflags(fmtflags f): Turns on the flag specified in f
● setprecision(int p): to Set the number of digits of precision
● setw(int w): to Set the field width to w
● showbase: to Turns on showbase flag
● showpoint: to Turns on show point flag
● showpos: to Turns on showpos flag
● skipws: to Turns on skipws flag
● unitbuf: turn on unitbuf flag
● uppercase: to turn on the uppercase flag ws: To skip leading white space
C++ Program : Formatted Input/Output Manipulators
#include <iostream>
using namespace std;
#include <iomanip>

ostream & curr(ostream & ostr)


{
cout<<setprecision(2);
cout<<"Rs.";
return ostr;
}
int main()
{
float amt= 34.3612;
cout<<curr<<amt;
return 0; }
References
● Is C++ Still Relevant in 2021? (makeuseof.com)
● C++ OOPs Concepts - javatpoint
● C++ Basics - GeeksforGeeks
● https://ciphertrick.com/c-beginners-introduction-to-escape-sequences/
● https://www.javatpoint.com/cpp-identifiers
● https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
● https://www.geeksforgeeks.org/unformatted-input-output-operations-in-cpp/
● https://www.geeksforgeeks.org/formatted-i-o-in-c/
● https://code365.in/syntax-for-creating-user-defined-manipulators-in-cpp/

● Download Visual Studio Code - Mac, Linux, Windows


● MinGW-w64 - for 32 and 64 bit Windows download | SourceForge.net

You might also like