This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int n = 0; // Number of elements in array | |
int* arr; | |
cout << "Enter number of elements in array" << endl; | |
cin >> n; | |
arr = new int[n]; | |
for (int i = 0; i < n; i++) { | |
*(arr+i) = i; | |
cout << *(arr+i) << " "; | |
} | |
cout << endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arguments are passed by value. | |
// swap1 will swap x and y, not the variables that are passed into it. | |
void swap1(int x , int y) { | |
int temp; | |
temp = x; | |
x = y; | |
y = temp; | |
} | |
// swap2 takes pointers as arguments, so it will swap the initial variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Argument is passed by reference, so the initial variable changes | |
int f(int &x) { | |
// Static variable is created only once | |
// and is not destroyed after function is finished. | |
static int a = 0; | |
if (!a) a = ++x; // !a is true only if a = 0 | |
return a; | |
} | |
int main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// x1 is passed by value, initial variable doesn't change | |
// x2 is passed by reference, initial variable changes | |
int f1(int x1, int &x2) { | |
return ++x1 + (++x2); | |
} | |
int main() { | |
int x=39, *p = &x; | |
cout << p << " " << *p << endl; | |
// Result is address of x and value of x (39) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
{ | |
friend int Freund(); // function Freud() will have access to A's private fields | |
friend class B; // class B will have access to A's private fields | |
public : int x, y; | |
private : short i; | |
} A1; | |
class B | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file hello.cpp | |
#include <cstdlib> | |
#include <iostream> | |
using namespace std; | |
void HelloWorld() { | |
cout << "Hello, world!" << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int m = 5; | |
namespace space1 | |
{ | |
int x1 = 3; | |
namespace space2 | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
short x = 4, i = 0; | |
void fun1() | |
{ | |
if (i == 5) throw 2; | |
} | |
void fun2() | |
{ | |
--x; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int x = 15; | |
cout << hex << x << endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FILE* textFile; | |
textFile = fopen("file.txt", "w"); | |
for (int i = 0; i < 10; i++) { | |
fputs("1 ", textFile); | |
} | |
fclose(textFile); |