Lecture 1.2
Lecture 1.2
1
Difference between structure and class
• The language, through which user can interact with computer is
known as computer language or programming language.
Class Structure
A class in C++ can be defined as a collection of related A structure can be referred to as a user defined data type
Definition variables and functions encapsulated in a single possessing its own operations.
structure.
Usage Generally used for large amounts of data. Generally used for smaller amounts of data.
2
Data Types
The table below shows the fundamental data types, their
meaning, and their sizes (in bytes):
Data Type Meaning Size (in Bytes)
int Integer 2 or 4
float Floating-point 4
char Character 1
wchar_t Wide Character 2
bool Boolean 1
void Empty 0
3
1. C++ int
The int keyword is used to indicate integers.
Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
For example,
int salary = 85000;
As mentioned above, these two data types are also used for exponentials. For example,
double distance = 45E12 // 45E12 is equal to 45*10^12
4
3. C++ char
Keyword char is used for characters.
Its size is 1 byte.
Characters in C++ are enclosed inside single quotes ' '.
For example,
char test = 'h’;
4. C++ bool
The bool data type has one of two possible values: true or false.
Booleans are used in conditional statements and loops (which we will learn in
later chapters).
For example,
bool cond = false;
5
5. C++ void
The void keyword indicates an absence of data.
It means "nothing" or "no value".
We will use void when we learn about functions and pointers.
Note: We cannot declare variables of the void type.
6
C++ Type Modifiers
We can further modify some of the fundamental data types by using type modifiers.
There are 4 type modifiers in C++. They are:
signed
unsigned
short
Long
We can modify the following data types with the above modifiers:
int
double
char
7
Data Type Size (in Bytes) Meaning
signed int 4 used for integers (equivalent to int)
unsigned int 4 can only store positive integers
used for small integers (range -
short 2
32768 to 32767)
used for large integers (equivalent
long at least 4
to long int)
used for large positive integers or 0
unsigned long 4
(equivalent to unsigned long int)
used for very large integers
long long 8
(equivalent to long long int).
used for very large positive integers
unsigned long long 8 or 0 (equivalent to unsigned long
long int)
used for large floating-point
long double 12
numbers
used for characters (guaranteed
signed char 1
range -127 to 127)
9
Input and output streams (cin, cout)
• 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.
• iostream: iostream stands for standard input-output stream. This header file
contains definitions to objects like cin, cout, cerr etc.
• iomanip: iomanip stands for input output manipulators. The methods
declared in this 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.
• The two keywords cout and cin in C++ are used very often for printing
outputs and taking inputs respectively. These two are the most basic
methods of taking input and printing output in C++. To use cin and cout in C+
+ one must include the header file iostream in the program.
11
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(<<).
• Example:
#include <iostream>
using namespace std;
int main()
{
char sample[] = "GeeksforGeeks";
cout << sample << " - A computer science portal for geeks";
return 0;
}
12
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.
• Example:
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "\nYour age is: " << age;
return 0;
}
13
Summary
14