0% found this document useful (0 votes)
18 views14 pages

Lecture 1.2

The document explains the differences between structures and classes in C++, highlighting their definitions, access specifiers, purposes, and typical usage. It also details fundamental data types in C++, including their meanings and sizes, as well as type modifiers and derived data types. Additionally, the document covers input and output streams in C++, including the use of cin and cout for handling user input and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

Lecture 1.2

The document explains the differences between structures and classes in C++, highlighting their definitions, access specifiers, purposes, and typical usage. It also details fundamental data types in C++, including their meanings and sizes, as well as type modifiers and derived data types. Additionally, the document covers input and output streams in C++, including the use of cin and cout for handling user input and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CONTENTS

• Difference between structure


and class
• Data types
• Input and output streams
(cin, cout)

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.

Keyword for the declaration Class Struct


Default access specifier Private Public

class myclass struct Person {


{ char name[50];
private: int citNo;
Example int data; float salary;
public: };
int_data(); int main() {
display_data(); struct Person person1, p[20];
}; return 0; }

Purpose Data abstraction and further inheritance Generally, grouping of data

Type Reference Value

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

double Double Floating-point 8

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;

2. C++ float and double


float and double are used to store floating-point numbers (decimals and exponentials).
The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the
precision of float. To learn more, visit C++ float and double.
For example,
float area = 64.74;
double volume = 134.64534;

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)

unsigned char 1 used for characters (range 0 to 255)


8
Derived Data Types
• Data types that are derived from fundamental data types are derived
types. For example: arrays, pointers, function types, structures, etc.

• We will learn about these derived data types in later tutorials.

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.

• 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.
10
Header files available in C++ for Input/Output operations are:

• 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

In this lecture we have We have discussed about


discussed about difference various data types in C++
between structure and class.

Moreover we have learnt


about basic i/o in c++

14

You might also like