C++
please visit sololearn for learn by coding
23 flashcards · Programming Languages · by Daniel Blackbird
what is IOstream header ?
The <iostream> header defines the standard stream objects that input and output data.
what is this ? namespace std ;
In our code, the line using namespace std; tells the compiler to use the std (standard) namespace. The std namespace includes features of the C++ Standard Library.
what is main function in c++?
The entry point of every C++ program is main(), irrespective of what the program does.
how to Print hello world with output operation ?
cout << "Hello world!";
what is semicolon in c++ ?
In C++, the semicolon is used to terminate a statement. Each statement must end with a semicolon. It indicates the end of one logical expression.
what is output of this ? cout << "This " << "is " << "awesome!";
how to insert a line breakand end of output of COUT with using <<
#include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; cout << "I love programming!"; return 0; }
how to insert a line break and end of output of COUT without using addition <<
#include <iostream> using namespace std; int main() { cout << "Hello world! \n"; cout << "I love programming!"; return 0; }
Comment a line is c++
// prints "Hello world"
Multi-Line Comments
/* This is a comment */
The prefix example increments the value of x, and then assigns it to y. The postfix example assigns the value of x to y, and then increments it.
what is Relational Operators ?
operators like >= <= == !=
declare a Int array that size is 9 and name is AR and how to show last array
declare a array int anArray[ 9 ]; for showing => anArray(8)
declare a pointer to this : int double float char
assign the address of a variable to the pointer
int score = 5; int *scorePtr; scorePtr = &score; cout << scorePtr << endl; //Outputs "0x29fee8"
what is output of this code ? int var = 50; int *p; p = &var; cout << var << endl; cout << p << endl; cout << *p << endl;
// Outputs 50 (the value of var) // Outputs 0x29fee8 (var's memory location) /* Outputs 50 (the value of the variable stored in the pointer p) */
what is stack and heap in memory ? why use heap memory ?
The stack: All of your local variables take up memory from the stack. The heap: Unused program memory that can be used when the program runs to dynamically allocate the memory. Many times, you are not aware in advance how much memory you will need to store particular information in a defined variable and the size of required memory can be determined at run time. You can allocate memory at run time within the heap for the variable of a given type using the new operator, which returns the address…
new int;
This allocates the memory size necessary for storing an integer on the heap, and returns that address.
what is this code doing in memory ? int *p = new int; *p = 5;
The pointer p is stored in the stack as a local variable, and holds the heap's allocated address as its value. The value of 5 is stored at that address in the heap.
how to delete a pointer from memory ?
delete p; // free up the memory
int *p = NULL; // Pointer initialized with null p = new int[20]; // Request memory delete [] p; // Delete array pointed to by p
Note the brackets in the syntax. Dynamic memory allocation is useful in many situations, such as when your program depends on input. As an example, when your program needs to read an image file, it doesn't know in advance the size of the image file and the memory necessary to store the image.
While the size allocated for varying data types depends on the architecture of the computer you use to run your programs, C++ does guarantee a minimum size for the basic data types: for the variable size of the char. sizeof(char)
how delete pointer? delete pointer p (code) delete p; The delete operator frees up the memory allocated for the variable but does not delete the pointer itself, as the pointer is stored on the stack. Pointers that are left pointing to non-existent memory locations are called dangling pointers. For example: int *p = new int; // request memory *p = 5; // store value delete p; // free up the memory // now p is a dangling pointer p = new int; // reuse for a new address The NULL pointer is a constan…