0% found this document useful (0 votes)
2 views

C++ intro

C++ is a widely used programming language developed in the 1980s as an extension of C, known for its object-oriented programming capabilities and high control over system resources. It is utilized in various applications, including operating systems and game development, and features a syntax that allows for efficient input and output operations. C++ supports multiple data types and variables, enabling developers to create robust programs with reusable code.

Uploaded by

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

C++ intro

C++ is a widely used programming language developed in the 1980s as an extension of C, known for its object-oriented programming capabilities and high control over system resources. It is utilized in various applications, including operating systems and game development, and features a syntax that allows for efficient input and output operations. C++ supports multiple data types and variables, enabling developers to create robust programs with reusable code.

Uploaded by

cutenayeon33
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

C++ Introduction

• C++ is a popular programming language.


• C++ is used to create computer programs, and is one of
the most used language in game development.
• C++ was developed as an extension of C, and both
languages have almost the same syntax.
• C++ was developed at AT & T Bell lab in 1980’s by Bjarne
Stroustrup
• C++ is also khown as Object-Oriented Programming
(OOPs) Language.
• C++ gives programmers a high level of control over system
resources and memory.
Why Use C++

• C++ is one of the world's most popular programming


languages.
• C++ can be found in today's operating systems, Graphical User
Interfaces, and embedded systems.
• C++ is an object-oriented programming language which gives
a clear structure to programs and allows code to be reused,
lowering development costs.
• C++ is portable and can be used to develop applications that
can be adapted to multiple platforms.
• C++ is fun and easy to learn!
• As C++ is close to C, C# and Java, it makes it easy for
programmers to switch to C++ or vice versa.
C++ Syntax
• Example:--

• #include <iostream> #include <iostream>


using namespace std; using namespace std;

main() { int main() {


cout << "Hello World!";
code return 0;
} }
• Example explained:--
• Line 1: #include <iostream> is a header file library that lets us work with input and output objects,
such as cout (used in line 5). Header files add functionality to C++ programs.
• Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
• Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
• Line 4: Another thing that always appear in a C++ program is int main(). This is called a function.
Any code inside its curly brackets {} will be executed.
• Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to
output/print text. In our example, it will output "Hello World!".

Note: Every C++ statement ends with a semicolon ;
• Remember: The compiler ignores white spaces. However, multiple lines makes the code more
readable.
• Line 6: return 0; ends the main function.
• Line 7: Do not forget to add the closing curly bracket } to actually end the main function.
Basic Input / Output in C++
• C++ comes with libraries that provide us with many ways for performing input and
output. In C++ input and output are performed in the form of a sequence of bytes
or more commonly known as streams

• 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.
• In c++, cout sends formatted output to standard output devices, such as the
screen. We use the cout object along with the << operator for displaying output.

• #include <iostream>
• using namespace std;

• int main() {
• // prints the string enclosed in double quotes
• cout << "This is C++ Programming";
• return 0;
• }
• 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.
• In C++, cin takes formatted input from standard input devices such as the
keyboard. We use the cin object along with the >> operator for taking
input.

• EXAMPLE:--
• #include <iostream>
• using namespace std;

• int main() {
• int num;
• cout << "Enter an integer: ";
• cin >> num; // Taking input
• cout << "The number is: " << num;
• return 0;
• }
DATA TYPES
• C++ supports the following data types:
1. Primary or Built-in or Fundamental data type
2. Derived data types
3. User-defined data types
DATA TYPES
• 1. Primitive Data Types: These data types are built-in or
predefined data types and can be used directly by the user to
declare variables. example: int, char, float, bool, etc. Primitive
data types available in C++ are:
Data Type Size Description

boolean 1 byte Stores true or false values


char 1 byte Stores a single character/letter/number, or ASCII
values
int 2 or 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 6-7 decimal
digits
double 8 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 15 decimal
digits
C++ Variables
• Variables are containers for storing data values.
• In C++, there are different types of variables (defined with
different keywords), for example:--
• int - stores integers (whole numbers), without decimals, such
as 123 or -123
• float/double - stores floating point numbers, with decimals,
such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values
are surrounded by single quotes
• string - stores text, such as "Hello World". String values are
surrounded by double quotes
• bool - stores values with two states: true or false

• 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.
Syntax:--

type variableName = value;

EXAMPLE:--

Int myNum = 5; // Integer (whole number)


float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
Practice:--
• Integer (int)
#include <iostream>
using namespace std;

int main () {
// Creating variables
int myNum = 5; // Integer (whole number)
// Print variable values
cout << "int: " << myNum;
return 0;
}

• float vs. double


• The precision of a floating point value indicates how many digits the value can have after the
decimal point. The precision of float is only six or seven decimal digits, while double variables
have a precision of about 15 digits. Therefore it is safer to use double for most calculations

#include <iostream>
using namespace std;

int main () {
// Creating variables
float myFloatNum = 5.75; // Floating point number
// Print variable values
cout << "float: " << myFloatNum;
return 0;
}
• DOUBLE (double)
#include <iostream>
using namespace std;

int main () {
// Creating variables
double myDoubleNum = 5.75834; // Floating point number
// Print variable values
cout << "float: " << myFloatNum;
return 0;
}

• CHARACTER (char)
#include <iostream>
using namespace std;

int main () {
// Creating variables
char myLetter = A; //Character
// Print variable values
cout << "char: " << myLatter;
return 0;
}
• STRING (string) :--To use strings, you must include an additional
header file in the source code, the <string> library:
#include <iostream>
#include<string>
using namespace std;
int main () {
// Creating variables
string myString = “HELLO”; //STRING
// Print variable values
cout << string: " << myString;
return 0;
}

•BOOLEAN (bool)
int main(){
// Creating varaibles
bool myBoolean = true;
//Print varaible values
cout<< “ bool:” <<mystring;
return o;
}
Constants in C++
As the name suggests the name constants are given to such variables or values in C/C++
programming language which cannot be modified once they are defined.

#include <iostream>
using namespace std;

int main() {
// int constant
const int intVal = 10;

cout << "Integer Constant: " << intVal << "\n";


return 0;
}

You might also like