0% found this document useful (0 votes)
2 views4 pages

In programming

Uploaded by

chl443736
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views4 pages

In programming

Uploaded by

chl443736
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

In programming, we can directly use some fixed values in #include<iostream>

using namespace std;


our program. These fixed values are called Literals.
Some commonly used literals in C++. int main(){ cout<< “Hello World”;
Integers(INT) return 0;
 Integers are numbers without decimal parts. For }
example: 5, -11 etc. #include<iostream>
 we refer to integers as simple int. using namespace std;
Floating-point numbers(double)
int main(){ cout << “Hi\n” << “” << “I left out the double quotation empty
 Floating-point numbers contain decimal parts. For intentionally.”; return 0; }
example 2.5, -6.3 etc.
 We refer to floating-point numbers as double.
Strings(string)
 In C++, texts wrapped inside double quotation marks
are called strings.
 “This is a string.”
 Strings are enclosed in double quotation marks.
 This basic idea of how C++ classifies numbers and
texts.

cout<< “Hello World”;


In C++, we use the cout <<output; statement to print
output on the screen.

 We can print multiple strings in a single statement by


separating them with the << operator.
Scope Resolution operator:

The scope resolution operator is special operator that allows access to global variable that is hidden by a local variable with the same name .

Syntax : Three variables with the same name x is declared.

:: variable name; X declared outside main() has global scope.

#include<iostream> which is actually hidden by the variables x declared in the outer and inner block
inside main().
using namespace std;
The variable declared inside main() in the inner block overrides both local variable
int x=5;
of outer block and the global variable.
int main()
Thus the global version of variable x is accessed with the help of scope resolution
{ int x=3; operator ::x in the inner and outer block.

cout <<’’ The local variable of outer block is :” <<X;

cout << “\nThe global variable is : ” << X;

{ int x=10;

cout << “The local variable of inner block is : ” << X;

cout << “\nThe global variable is : ” <<X;

return 0;

OUTPUT:

the local variable of outer block is 3 the local variable of inner block is 10

the global variable is 5 the global variable is 5


New operator:

The new operator denotes a request for memory allocation on the heap.

If sufficient memory is available the new operator initializes the memory and returns the

address of the newly allocated and initialized memory to the pointer variable.

int *p = NULL; //pointer initialized with NULL

p = new int; //requesting memory

Advantages of new operator compared to malloc()

 It automatically computes the size of data object. One doesn’t need to


use the sizeof() operator.
 It automatically returns the pointer data type, there is no need for using
type cast.
 New operator and delete operator can be overloaded
 It is possible to initialize the object while allocating the memory

Dynamic Memory Allocation:

Dynamic memory allocation refers to performing memory allocation manually by programmer.

Dynamically allocated memory is allocated on Heap

Non-static and local variables get allocated on stack

In C++ new and delete operator performs the task of allocating and freeing memory.

You might also like