Vijay Krishnavanshi
Open Source Developer at KDE
string name = "Brice";#include <iostream>using namespace std;int main(){string name("");cout << "What is your name?" << endl;cin >> name;cout << "Hello " << name << ", how are you today?" << endl;return 0;}
#include <iostream>using namespace std;int main() // Every C++ programs start by running this function{cout << "Hello world!" << endl;return0;}
type functionName(arguments){[...]instructions;[...]return value;}
int add(int a, int b){int result;result = a + b;return result;}int main(){cout << "Write a first integer: "; cin >> int1;cout << "Write a second integer: "; cin >> int2;int int3 = add(int1, int2);cout << int1 << "+" << int2 << "=" << int3 << endl;return 0;}
void sayHello(){cout << "Hello!" << endl;}
int addTwo(int a){a = a + 2;return a;}int main(){int number(6), result;result = addTwo(number);cout << "number=" << number << " and result=" << result << endl;return 0;}// number = 6// result = 8
int addTwo(int& a){a = a + 2;return a;}int main(){int number(6), result;result = addTwo(number);cout << "number=" << number << " and result=" << result << endl;return 0;}// number = 8// result = 8
void copyBook(string text) // long to copy{}void copyBook2(string& text) // can modify original text{}void copyBook3(string const& text) // fast copy and read-only{}
#ifndef MATH_H_INCLUDED // Ensure to include this file only once#define MATH_H_INCLUDED // in the compilation process// Add 2 numbers and return their sumint add(int a, int b);// Take number as argument and return number + 2int addTwo(int a);#endif
#include "math.h" // Link the headerint add(int a, int b){int result;result = a + b;return result;}int addTwo(int a){a = a + 2;return a;}
#include <iostream>#include "math.h" // Link the functions of math filesint main(){int number;cout << "Enter an integer: "; cin >> number;addTwo(number);cout << "Your number + 2 = " << number << endl;return 0;}
type name[size];int const numberOfClasses(5); // The size of the array is knownstring classes[numberOfClasses];
string classes[5];classes[0] = "B1"; // Arrays starts with the index 0classes[1] = "B2";classes[2] = "B3";classes[3] = "M1";classes[4] = "M2";
cout << "The class for B2 students is: " << classes[1] << endl;for(int i=0; i<numberOfClasses; i++) // numberOfClasses = 5{cout << classes[i] << endl;}
#include <vector> // Do not forget to include the "vector" library!vector<type> name(initialSize, defaultValue);vector<double> scores;vector<string> students(5);vector<string> teachers(10, "no name");
vector<string> students(5, "no name");students[0] = "Justin BIEBER";students[1] = "Bruce LEE";students[2] = "Jack JOHNS";
cout << students[1] << endl; // Display Bruce LEEfor (int i=0; i<students.size(); i++) //size() returns the array size{cout << students[i] << endl;} // Display all the students names
vector<double> scores; // Declare an empty arrayscores.push_back(18);scores.push_back(11);for(int i=0; i<scores.size(); i++) { cout << scores[i] << endl; }
1811
vector<double> scores; // Declare an empty arrayscores.push_back(18); // Add the value 18scores.push_back(11); // Add the value 11scores.pop_back(); // Remove the last value of the arrayfor(int i=0; i<scores.size(); i++) { cout << scores[i] << endl; }
18
type name[sizeA][sizeB]...[sizeN];int const sizeX(5);int const sizeY(4);int scores[sizeX][sizeY];
int myAge(30);cout << "The value of myAge is: " << myAge << endl;cout << "The address of myAge is: " << &myAge << endl;
A pointer is a variable containing the address of another variable
type *name;int *p1;double *p2;sting *p3;unsigned int *p4;vector<string> *p5;
type *name(0);
int *p1(0);
int myAge(30);int *myPointer(0);myPointer = &myAge;
cout << myPointer << endl; // Output 0x1ab23c4d for examplecout << *myPointer << endl; // Output 30By Vijay Krishnavanshi