Unit – 1
Introduction to C++
Fig. Structure of Object Oriented Programming
Fig shows how objects can communicate with other objects with the help of functions. Here data is not accessible to external functions which ensure data security.
Advantages of OOP
2. Explain input and output in C++?
Input/output functions allow to take input from user using console and prints the output of program on the screen respectively.
Cout
Output: Hello World – This is the string.
Cin
Cerr
Output – Error.
Clog
Output – Error
3. What are tokens?
A token is the smallest element of a C++ program that is meaningful to the compiler. The C++ parser recognizes these kinds of tokens:
4. Explain keyword and identifiers ?
Keywords:
Identifiers
5. Explain the data types in C++?
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
1. int
The int keyword is used to indicate integers.
Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 214748647.
For example,
int salary = 85000;
2. 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
3. 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';
Note: In C++, an integer value is stored in a char variable rather than the character itself. To learn more, visit C++ characters.
4. wchar_t
Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1.
It is used to represent characters that require more memory to represent them than a single char.
For example,
wchar_t test = L'ם' // storing Hebrew character;
6. Explain derived data types ?
Derived Data types
7. Explain void data types?
Void data type is used for value les entity. It is used for function which do not return any value.
8. Explain type modifiers ?
Data Type | Size in bytes | Range |
short int | 2 | -32,768 to 32,767 |
unsigned short int | 2 | 0 to 65,535 |
unsigned int | 4 | 0 to 4,294,967,295 |
Int | 4 | 2,147,483,648 to 2,147,483,647 |
Long int | 4 | -2,147,483,648 to 2,147,483,647 |
Unsigned long int | 4 | 0 to 4,294,967,295 |
Long long int | 8 | -(2^63) to (2^63)-1 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
signed char | 1 | 128 to 127 |
unsigned char | 1 | 0 to 255 |
Float | 4 |
|
Double | 8 |
|
long double | 12 |
|
wchar_t | 2 or 4 | 1 wide character |
9. Explain type casting?
Implicit type casting.
Explicit type casting.
Converting by assignment
In this required type is defined explicitly in front of the expression. It is a forceful casting.
Conversion using cast operator
Unary operator is the cast operator it forces one data type to get converted into another.
10. Explain constant and operator ?
11. Explain strings in C++?
The string is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Following is the memory presentation of above defined string in C/C++ −
String Presentation in C/C++
The C++ compiler automatically places the '\0' at the end of the string when it initializes the array.
Example:
#include <iostream>
using namespace std;
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Greeting message: Hello
C++ supports a wide range of functions that manipulate null-terminated strings −
Sr.No Function & Purpose
1 strcpy(s1, s2); Copies string s2 into string s1.
2 strcat(s1, s2); Concatenates string s2 onto the end of string s1.
3 strlen(s1); Returns the length of string s1.
4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same;
less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.