Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Fundamentals of C++ Programming

Introduction to functions in cpp

Uploaded by

Sonwabiso tedee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Fundamentals of C++ Programming

Introduction to functions in cpp

Uploaded by

Sonwabiso tedee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Fundamentals of C++

Programming language
BY SONWABISO
C++ Programming Language

 C++ is the most used and most popular programming language developed by
Bjarne Stroustrup.
 C++ is a high-level and object-oriented programming language.
 This language allows developers to write clean and efficient code for large
applications and software development, game development, and operating system
programming.
 It is an expansion of the C programming language to include Object Oriented
Programming(OOPs) and is used to develop programs for computers.
WHY LEARN C++?

 C++ is one of the most used and popular programming languages.


 C++ is used in making operating systems, embedded systems, and Graphical User Interfaces.
 It is an object-oriented programming language that implements all the OOPs concepts such as
Abstraction, Encapsulation, and Inheritance, which gives a clear structure to programs and allows code to
be reused, lowering development costs and providing security.
 It is portable and can be used to create applications that can be adapted to multiple platforms.
 C++ is easy to learn so that you can choose it as your first programming language.
 It makes programming easy for programmers to switch to C++ because its syntax is similar to C, Java,
and C#.
C++ Data Types
PREMATIVE DATA TYPES

 Integer: The keyword used for integer data types is int. Integers typically require 4 bytes
of memory space and range from -2147483648 to 2147483647.
 Character: Character data type is used for storing characters. The keyword used for the
character data type is char. Characters typically require 1 byte of memory space and range
from -128 to 127 or 0 to 255.
 Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean
variable can store either true or false. The keyword used for the Boolean data type is bool.
 Floating Point: Floating Point data type is used for storing single-precision floating-
point values or decimal values. The keyword used for the floating-point data type is
float. Float variables typically require 4 bytes of memory space.
 Double Floating Point: Double Floating Point data type is used for storing double-
precision floating-point values or decimal values. The keyword used for the double
floating-point data type is double. Double variables typically require 8 bytes of memory
space.
 void: Void means without any value. void data type represents a valueless entity. A void
data type is used for those function which does not return a value.

 Wide Character: Wide character data type is also a character data type but this data type
has a size greater than the normal 8-bit data type. Represented by wchar_t. It is
generally 2 or 4 bytes long.

 sizeof() operator: sizeof() operator is used to find the number of bytes occupied by a
variable/data type in computer memory.
EXAMPLE

 // C++ Program to Demonstrate the correct size


 // of various data types on your computer.
 #include <iostream>
 using namespace std;

 int main()
 {
 cout << "Size of char : " << sizeof(char) << endl;
 cout << "Size of int : " << sizeof(int) << endl;

 cout << "Size of long : " << sizeof(long) << endl;


 cout << "Size of float : " << sizeof(float) << endl;

 cout << "Size of double : " << sizeof(double) << endl;

 return 0;
 }
EXAMPLE( OUTPUT)

 Size of char : 1
 Size of int : 4
 Size of long : 8
 Size of float : 4
 Size of double : 8
Datatype Modifiers

 As the name suggests, datatype modifiers are used with built-in data types to modify
the length of data that a particular data type can hold.


ADVANTAGES
 Data types provide a way to categorize and organize data in a program, making it easier
to understand and manage.

 Each data type has a specific range of values it can hold, allowing for more precise
control over the type of data being stored.

 Data types help prevent errors and bugs in a program by enforcing strict rules about how
data can be used and manipulated.

 C++ provides a wide range of data types, allowing developers to choose the best type for
a specific task.
DISADVANTAGES
 Using the wrong data type can result in unexpected behavior and errors in a program.

 Some data types, such as long doubles or char arrays, can take up a large amount of
memory and impact performance if used excessively.

 C++’s complex type system can make it difficult for beginners to learn and use the
language effectively.

 The use of data types can add additional complexity and verbosity to a program, making it
harder to read and understand.
TYPE CASTING

 Type casting is the process of converting one data type into another. In C++, type casting can be done
implicitly or explicitly. Implicit type casting, also known as coercion, occurs automatically when a value
of one data type is assigned to a variable of another data type.

 Explicit type casting, on the other hand, requires the use of casting operators such as static_cast,
dynamic_cast, reinterpret_cast, and const_cast to convert one data type into another.
 Type casting is significant in manipulating data types within C++ programs because it allows for
conversions between different data types, enabling compatibility between different parts of a program and
facilitating operations that involve different data types.

 However, improper use of type casting can lead to data loss, unexpected behavior, or even program errors,
so it should be used judiciously and with caution.
Basic Input / Output in C++

 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 output.
Header files available in C++ for Input/Output
operations are:
 iostream: iostream stands for standard input-output stream. This header file contains definitions of objects
like cin, cout, cerr, etc.

 iomanip: iomanip stands for input-output manipulators. The methods declared in these files are used for
manipulating streams. This file contains definitions of setw, setprecision, etc.

 fstream: This header file mainly describes the file stream. This header file is used to handle the data being
read from a file as input or data being written into the file as output.

 bits/stdc++: This header file includes every standard library. In programming contests, using this file is a
good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time
sensitive.
STANDARD OUTPUT STREAM(COUT)

 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(<<).
 #include <iostream>

 using namespace std;

 int main()
 {
 char sample[] = "GeeksforGeeks";

 cout << sample << " - A computer science portal for geeks";

 return 0;
 }
 OUTPUT -GeeksforGeeks - A computer science portal for geeks
STANDARD INPUT STREAM

 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.
 #include <iostream>
 using namespace std;

 int main()
 {
 int age;

 cout << "Enter your age:";


 cin >> age;
 cout << "\nYour age is: " << age;

 return 0;
 }
 Enter your age: 18
 Your age is: 18
Un-buffered standard error stream (cerr):

 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 so it is used when one needs to display the error message immediately. It does
not have any buffer to store the error message and display it later.

 The main difference between cerr and cout comes when you would like to redirect
output using “cout” that gets redirected to file if you use “cerr” the error doesn’t get stored
in file.(This is what un-buffered means ..It cant store the message)
 #include <iostream>

 using namespace std;

 int main()
 {
 cerr << "An error occurred";
 return 0;
 }
 OUTPUT: An error occurred
buffered standard error stream (clog)

 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. or the buffer is not explicitly flushed (using flush()). The
error message will be displayed on the screen too.
 #include <iostream>

 using namespace std;

 int main()
 {
 clog << "An error occurred";

 return 0;
 }
 OUTPUT: An error occurred

You might also like