C++ intro
C++ intro
• 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
EXAMPLE:--
int main () {
// Creating variables
int myNum = 5; // Integer (whole number)
// Print variable values
cout << "int: " << myNum;
return 0;
}
#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;