Skip to content

Commit 7d79e53

Browse files
authored
inicialComment
0 parents  commit 7d79e53

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

1_introduction.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//one line comment
2+
3+
/*
4+
* Multi line comment
5+
*/
6+
7+
/*
8+
* Tips:
9+
*
10+
* Do comment every functionally (especially functions) what are not 100 % clear what they do
11+
* When you use any functionally from other sources (like stackoverflow) put the source into comment as reference
12+
* Write clean code
13+
* Always think about teh problem first, then start writting the code
14+
* For help use offcial reference: en.cppreference.com
15+
* Coding style: https://stackoverflow.com/questions/18170944/c-coding-style
16+
*
17+
*/
18+
19+
//a library input
20+
//iostream library allows to manipuylate streams = allow to to print outputs and
21+
#include <iostream>
22+
23+
// see more on: https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm
24+
/*
25+
* For now, most of the functions are store in std namespace
26+
* To access them you have first access this namespace
27+
* You access namespace by declarring is by std::
28+
* By declaring at the beggining use don't have to declare every single function
29+
*/
30+
using namespace std;
31+
32+
/*
33+
* main() is a function where program execution begins
34+
* main has to return an int, therefore at the has be key word return followed by integer
35+
*/
36+
int main() {
37+
// prints Hello World
38+
cout << "Hello World";
39+
40+
//endl prints a new line
41+
cout << endl;
42+
43+
44+
/*
45+
* data types in C++
46+
* most of the types has unsigned type too
47+
* numeric data types long type on top of that
48+
*/
49+
50+
//boolean
51+
bool flag;
52+
//character - 1B
53+
char ch;
54+
//integer - 4B
55+
int i;
56+
//floating point - 4B
57+
float f;
58+
//double floating point - 8B
59+
double d;
60+
//valueless is void - used mainly in fuctions
61+
62+
63+
cout << "Enter a number" << endl;
64+
//read standart input
65+
cin >> i;
66+
67+
68+
return 0;
69+
}

2_functions.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
int addNumbers( int a, int b = 50){
7+
return a + b;
8+
}
9+
10+
void overloadFunction(){
11+
cout << "I am overload function with no argument" << endl;
12+
}
13+
14+
void overloadFunction( int i ){
15+
cout << "I am overload function with integer argument " << i << endl;
16+
}
17+
18+
void overloadFunction( string str ){
19+
cout << "I am overload function with string argument " << str << endl;
20+
}
21+
22+
void overloadFunction( double d ){
23+
cout << "I am overload function with double floating argument " << d << endl;
24+
}
25+
26+
bool isPrime( int num ){
27+
28+
// Corner case
29+
if (num <= 1)
30+
return false;
31+
32+
// Check from 2 to n-1
33+
for (int i = 2; i < num; i++)
34+
if (num % i == 0)
35+
return false;
36+
37+
return true;
38+
}
39+
40+
41+
//always devided your code into section by functionality - create functions
42+
int main (){
43+
44+
int num = 0;
45+
cout << "Enter a number, you want to know if it is a prime number: ";
46+
cin >> num;
47+
48+
cout << "The number " << num;
49+
string myString = ( isPrime( num ) ) ? " is a prime number" : " is not a prime number";
50+
cout << myString << endl;
51+
52+
//functions can be overloaded
53+
//function with the same name but different arguments can do different task
54+
overloadFunction();
55+
overloadFunction( 5 );
56+
overloadFunction( "test" );
57+
overloadFunction( 5.55 );
58+
59+
//implicit arguments are "preset values" in function argument
60+
//implicit arguments are used when no others are specified
61+
62+
cout << addNumbers( 10, 8 ) << endl;
63+
//when second argument is ommited implicit is used
64+
cout << addNumbers( 10 ) << endl;
65+
66+
return 0;
67+
}

3_references.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void hardSwap (int& first, int& second)
6+
{
7+
int temp = first;
8+
first = second;
9+
second = temp;
10+
}
11+
12+
void swap2 (int *first, int *second)
13+
{
14+
int temp = *first;
15+
first = second;
16+
*second = temp;
17+
}
18+
19+
void swap (int first, int second)
20+
{
21+
int temp = first;
22+
first = second;
23+
second = temp;
24+
}
25+
26+
void pointerSwap (int *first, int *second)
27+
{
28+
int temp = *first;
29+
*first = *second;
30+
*second = temp;
31+
}
32+
33+
34+
int main(){
35+
36+
/*
37+
* There are two types of references: (hard) reference & and pointer *
38+
* Pointers "pointing" to a location (physical address) in memory where variable is store
39+
* Reference is working directly with the variable on a specific address
40+
* When passing varibale (not a reference) to a function a local copy is created for teh function
41+
* To speed up your programs and work more efficiantly work with refrences and pointers
42+
*/
43+
44+
//declare local variable in main function
45+
int x = 1, y = 2;
46+
//create a general pointer
47+
int *px;
48+
//an address is assign a pointer
49+
cout << px << endl;
50+
//no value has been assigned yet
51+
cout << *px << endl;
52+
//assign a reference address to pointer
53+
px = &x;
54+
//the address has changed since the pointer is poiting to a different (new) variable
55+
cout << px << endl;
56+
//pointer is poiting to a value
57+
cout << *px << endl;
58+
//to access a value what pointer px is pointing to you have dereference a pointer
59+
//to dereference a pointer add a * infront of pointer
60+
*px = 3;
61+
//address doesnt change
62+
cout << px << endl;
63+
//only the value was chnaged - the address stayed same
64+
cout << *px << endl;
65+
66+
int &ry = y;
67+
//to access value from a reference
68+
ry = 4;
69+
cout << ry << endl;
70+
71+
72+
int x1 = 5;
73+
int x2 = 10;
74+
75+
cout << "x1: " << x1 << endl;
76+
cout << "x2: " << x2 << endl;
77+
78+
swap( x1, x2 );
79+
80+
cout << "x1: " << x1 << endl;
81+
cout << "x2: " << x2 << endl;
82+
83+
hardSwap( x1, x2 );
84+
85+
cout << "x1: " << x1 << endl;
86+
cout << "x2: " << x2 << endl;
87+
88+
pointerSwap( &x1, &x2 );
89+
90+
cout << "x1: " << x1 << endl;
91+
cout << "x2: " << x2 << endl;
92+
93+
swap2 ( px, &x2 );
94+
95+
cout << "x1: " << x1 << endl;
96+
cout << "x2: " << x2 << endl;
97+
98+
99+
return 0;
100+
}

4_classes/4_classes.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//in this main class are other classes only created and theirs methods are used
2+
//funtions stands alone whereas methods is a function inside a class
3+
4+
5+
/*
6+
* c++ program has 3 main parts: main.cpp, header file (.h) and class implementations in cpp
7+
* Header file is a "link" between main file and class files
8+
* In header files are included class implementaions
9+
* Main includes header files - therefore in main are header files an also class implementations
10+
*/
11+
12+
//include header file of my class car
13+
#include "Car.h"
14+
15+
int main(){
16+
17+
return 0;
18+
}

4_classes/Car.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Car.cpp"
2+
3+
/*
4+
* Implent my own class car
5+
* Only heads of classes are showns here
6+
* The only exception is one line function
7+
*
8+
*
9+
*
10+
*/
11+
12+
class Car{
13+
public:
14+
//constructor
15+
Car();
16+
Car( int );
17+
//copy cost
18+
//op =
19+
//destructor
20+
21+
22+
private:
23+
24+
25+
}

0 commit comments

Comments
 (0)