C++ Lecture 14
C++ Lecture 14
02
LECTURE 14
04
Modern C++
Today’s Agenda
01 What is Modern C++ ?
01
02 What is C++ 11 , C++ 14 and C++ 17?
04
05 New features of C++
05
What Is Modern C++?
C++ is one of the most widely used programming language in the world.
It’s been around from the last 40 years and was initially much like C language
due to backward compatibility.
.
But to meet modern computing requirements it was redesigned in the year
2011 and the name Modern C++ was given.
What Is Modern C++?
. C++ 17: which adds some more new. features to the language
The most popular compilers are GCC C++, Clang C++, and Microsoft C++ and
following table lists their support for C++ 17:
.
Compiler C++17 Support
GCC8 Full Support For C++17
Clang C++ Implements all the features of C++17
Since we are using Code Blocks IDE which has support for GCC compiler so we
will be using GCC.
C++14/17 has lot of new concepts which were not present in classic C++.
All these new features make Modern C++ programs type-safe and easier to
write, extend, and maintain. .
• GCC,
• MinGW,
• Digital Mars,
• Microsoft Visual C++, .
• Borland C++,
• LLVM Clang,
• Watcom,
• LCC and
• the Intel C++ compiler.
Important Difference Between Standard
C++ and C++11/14/17
1. The names of all C++ header files do not contain .h extension
5 All predefined object like cout , cin etc are now placed inside something called
namespace
#include <iostream>
int main()
{
std::cout<<"Welcome To c++ 14";
return 0; .
}
What Is A Namespace?
Before we discuss exactly what a namespace is, it is probably best to
consider a simple example of when and why we would need them.
Using namespaces, we can create two variables or functions having the same
name
.
Syntax:
namespace <some_name>
{
entities
}
What Is A Namespace?
2. Default namespace is global namespace. and can access global data and
functions by proceeding (::) operator.
Points To Remember
3. We can create our own namespace and anything declared within namespace
has scope limited to namespace.
The built in C++ library routines are kept in the standard namespace called std
The keyword using is used to introduce a name from a namespace into the
current declarative region.
So using namespace std means that we are going to use classes or functions
(if any) from "std" namespace .
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
.
cout<<“Enter 2 int”<<endl;
cin>>a>>b;
c=a+b;
cout<<“Nos are “<<a<<“ and “<<b<<endl;
cout<<“Their sum is “<<c;
return 0;
New Data Types Added
long long : a new data type of integer family supporting size of 8bytes.
The “Auto” Keyword
• In Modern C++, the compiler can automatically determine the data type of a
variable at the point of declaration using it’s initialization expression:
• So,
int x = 4;
• can now be replaced with .
auto x = 4;
• The auto keyword can also automatically detect a function’s return type
• For example:
• Since x + y evaluates to an integer, the compiler will detect this function should
New Ways Of Initialization
• Copy initialization
• Direct initialization .
• Uniform initialization
New Ways Of Initialization
.
• Direct initialization : is done by using parenthesis.
• Modern C++ allows us to initialize data members at the point of declaration and
this is called In-Class Initialization
class Circle
{
int radius=10; // OK , from C++ 14 onwards
.
.
.
};
• Modern C++ provides us an easy way to traverse an array called “Range Based For
Loop”.
• Syntax:
for(<data type> <var_name>: <array_name>)
{ .
//loop body
}
• Example:
int arr[ ]={10,20,30,40};
for(int x:arr)
End of Lecture 14
For any queries mail us @: [email protected]
Call us @ : 0755-4271659, 7879165533
Thank you