C++ unit 1
C++ unit 1
INTRODUCTION TO C++
• C++ is a cross-platform language that can be used to create high-performance
applications.
• C++ was developed by Bjarne Stroustrup, as an extension to the C language.
• C++ gives programmers a high level of control over system resources and memory.
EVOUTION OF C++
• The C++ language is an object-oriented programming language & is a combination of
both low-level & high-level language – a Middle-Level Language. The programming
language was created, designed & developed by a Danish Computer Scientist – Bjarne
Stroustrup at Bell Telephone Laboratories (now known as Nokia Bell Labs) in Murray
Hill, New Jersey.
• As he wanted a flexible & a dynamic language which was similar to C with all its
features, but with additionality of active type checking, basic inheritance, default
functioning argument, classes, inlining, etc. and hence C with Classes (C++) was
launched.
• C++ was initially known as “C with classes, ” and was renamed C++ in 1983. ++
is shorthand for adding one to variety in programming;
ADVANTAGES OF OOP
• Modularity: OOP divides complex systems into smaller components, making the
codebase easier to comprehend, create, and maintain.
• Reusability: Inheritance allows code reuse, improving code quality and saving time.
• Encapsulation: Protects data integrity and privacy by restricting direct access and
allowing controlled access through methods.
• Flexibility and Scalability: OOP enables easy addition and modification of features
without impacting the entire codebase.
• Code Organization: OOP promotes a structured approach, enhancing collaboration
and code readability.
• Code Maintenance: Changes and bug fixes can be made to specific objects or classes
without affecting other parts of the system, reducing errors and improving debugging.
• Code Reusability: OOP encourages the development of reusable code elements, saving
time and improving system reliability.
• Better Problem Solving: OOP models real-world systems, allowing developers to
create intuitive solutions that closely mimic real-world circumstances.
Devices, making C++ ideal for forming the base layer of many important applications.
For these reasons, C++ is an excellent choice for building software that is fast, efficient at
managing system resources, and reliable in performing critical tasks.
What is C++ used for?
C++ can be found in a wide variety of industries. Check out the following list for some
of the most notable, interesting, and even surprising real-world applications of C++.
Operating systems
C++ plays an important part in well-known operating systems, from MacOS and
Windows to mobile operating systems like iOS. In general, operating systems need to be fast
and efficient at managing system resources. With the low-level (close to machine code)
capabilities of C++, developers can structure their code to make even the smallest details of an
operating system fast and energy efficient.
Game development
C++ is one of the most widely-used programming languages in game development. It
has been used to create: games, such as World of Warcraft, Counter-Strike, and StarCraft, game
engines like Unreal Engine, and gaming consoles, including Xbox, PlayStation, and Nintendo
Switch.
Gaming involves lots of resource-intensive functions, whether it’s generating graphics or
facilitating multiplayer sessions. C++ helps in optimizing the use of resources, such as allowing
programmers to adjust exactly how data structures and memory allocation are handled.
IoT devices
IoT (Internet of Things) devices include embedded systems that often rely on C++.
Some examples of these devices are appliances, cars, smartwatches, TVs, and medical devices.
Because the hardware is often embedded inside of an object, these devices have to operate with
limited computing resources and strict power requirements. C++ helps programmers control
how these resources are used in order to run programs more efficiently.
Databases
C++ is used to build popular database tools like MySQL and MongoDB. These
databases form the backbone of many well-known applications, including ones created by
Google, Netflix, YouTube, Adobe, and more. C++ is ideal for helping structure databases to
support efficient storage.
Web browsers
C++ plays a role in web browsers, such as Google Chrome, Mozilla Firefox, Safari, and
Opera. It is used to develop back-end services that retrieve information from databases and
render code into interactive web pages. C++ helps web browsers carry out these tasks with
speed and minimal delays, which means we don’t have to wait long for content to appear on
our screens.
Machine learning tools
Machine learning tools, such as TensorFlow, rely on C++ as a back-end programming
language. Even though data scientists can use TensorFlow with Python, for example, the core
machine learning calculations are carried out with C++ code. In fact, C++ has a large collection
of libraries that power these highly-sophisticated calculations that train machine learning
models.
AR/VR applications
C++ is widely used for developing augmented reality and virtual reality applications.
In fact, many of these applications run on Unreal Engine, which is built using C++. AR/VR
applications need to handle large amounts of data that adjust continuously based on input from
camera sensors and user interactions. C++ is ideal for helping process all of this data while
creating seamless virtual experiences.
Scientific research
C++ plays a notable role in scientific research, from space exploration to high-energy
particle physics. At CERN, scientists use C++ for analyzing data on subatomic particles that
can help answer questions about how our universe originated. At NASA, C++ has been used
to build autonomous driving systems that help the Mars Rovers navigate unknown terrain.
Many parts of the International Space Station are built using C++ as well.
Financial tools
Many banks and financial institutions use C++ to build their software infrastructure,
which powers applications for banking, trading, and financial modeling. The speed and reliable
performance of C++ is ideal for processing millions of daily transactions, facilitating a high
volume and frequency of trades, and creating data simulations for large portfolios.
Flight software
C++ is a commonly-used language for flight software that powers commercial airplanes
and military and defense aircraft (such as the F-16 and F-35 planes). C++ is used to build a lot
of the safety-critical software, which is required to follow strict coding standards to make sure
that the wings, engines, and other critical parts work predictably during each flight.
Google search engine
Google’s search engine relies on C++ to make sure that search results show up quickly
and with minimal response time. C++ also powers the machine learning and other complex
algorithms that make sure we get the best possible results for our search. Google’s crawling
and indexing infrastructure is also implemented in C++.
Medical technology
C++ can be found in a variety of medical applications, from MRI machines to lab
testing equipment to systems that handle patient information. C++ is also used to model data
and run data simulations for bioinformatics research, because many advanced algorithms
written for the medical and biological sciences are implemented in C++.
Telecommunications
C++ is widely used in building telephone, Internet, and other telecommunications
infrastructure. It’s ideal for managing resources to handle tons of simultaneous communication
with speed and efficiency.
Movie production
Many of the graphics and special effects that you see in movies are created with C++.
C++ is great for this role because it can handle large files with high-quality footage and run
many of the advanced calculations that make the special effects possible.
width(int width) Using this function, we can specify the width of a value to be
displayed in the output at the console.
setf(arg1, arg2) Using this function, we can set the flags, which allow us to
display a value in a particular format.
unsetf(char ch)
Using this function, we could clear the flag specified fixed by
the function setf().
In C++, we can read the input entered by a user at console using an object cin of istream class
and we can write the output at console using an object cout of ostream class. Through
the cin and cout objects, we can access the formatted I/O functions.
get() Reads a single character from the user at the console, and
returns it.
getline(char* arr, int size) Reads a line of characters, entered by the user at the console
which ends with a newline character or until the size of .
put(char ch)
Writes a single character at the console.
C++ MANIPULATORS
C++ program structure is divided into various sections, namely, headers, class
definition, member functions definitions and main function.
• Comments are a vital element of a program that is used to increase the readability of a
program and to describe its functioning. Comments are not executable statements and
hence, do not increase the size of a file.
• Headers: Generally, a program includes various programming elements like built-in
functions, classes, keywords, constants, operators, etc., that are already defined in the
standard C++ library.
Eg: #inc1ude<iostream>, it includes the contents of iostream in the program. This enables the
programmer to use standard input, output and error facilities that are provided only through
the standard streams defined in <iostream>.
• Main Function: The main () is a startup function that starts the execution of a c++
program. All C++ statements that need to be executed are written within main ( ). The
compiler executes all the instructions written within the opening and closing curly
braces' {}' that enclose the body of main ( ).
SCOPE RESOLUTION OPERATOR IN C++
• Scope resolution operator (::) in C++ is used to define a function outside a class or when
we want to use a global variable but also has a local variable with the same name.
Syntax of Scope Resolution Operator
The scope resolution operator follows this general syntax:
scope_name :: identifier
where scope_name is the name of the scope where identifier is defined.
Example:
#include <iostream>
using namespace std;
char c = 'a'; // global variable (accessible to all functions)
int main()
{
char c = 'b'; // local variable (accessible only in main function)
cout << "Local variable: " << c << "\n";
cout << "Global variable: " << ::c << "\n"; // Using scope resolution operator
return 0;
}
Output of program:
class Game
{
public:
void play(); // Function declaration
};
// function definition outside the class
void Game::play()
{
cout << "Function defined outside the class.\n";
}
int main()
{
Game g;
g.play();
return 0;
}
Output of program: