#include <iostream> #include <vector> using namespace std; /// functions int multiply(int a, int b){ return a*b; } int multiply(int a){ // Functions overload. Same name but different number or types of input are treated as seperate funcrtions return a * a; } void DoubleByValue(int a){ // Passing by Value (a local copy of variable a is created which does not affect outside) a = 2 * a; cout << " The value of is " << a << endl; } void DoubleByReference(int &a){ // Passing by reference; means that variable a is no longer copy but the actual value itself // Changes to a persist outside local scope of function a = 2 * a; cout << "The value of a is " << a << endl; } class Polygons{ public: // these fields can be accessed outside }; int main() // The main loop is actually just a function that returns some integer... irrelevant { // Without namespace specification: std::cout << "Hello world!" << endl; // Since specified std namespace can just use: cout << " endl = means new line but can also use \\n \n"; // ***************************************************************************** // Variables and all such // First defintiion requires type definition but change doesnt int num = 5; // intgers in range - 2^31 ... 2^31 - 1 cout << "Num = " << num << endl; num = 6; cout << "Num = " << num << endl; // Other variable types string mystring = "This is a string "; char mychar = 'H'; // Just single characters double myfloat = 3E-5; cout << myfloat << endl; //~? Typeconversion e.g. 37/5 is not that same as doing it explicitely // ***************************************************************************** // Loops for (int i=0; i != 5; ++i){ // Note: ++i =/= i++ and ++i = i + 1; cout << " i = " << i << endl; } int loopcounter = 0; while (loopcounter > -10){ cout << loopcounter << endl; loopcounter -= 2; // same as loopcounter = loopcounter -1; also works for += int foo = loopcounter; } // Variables defined inside loops, functions, etc stay in their local scope // cout << foo<< endl; // Uncommenting this line crashes the code // ********************************************************************************* // Vectors vector<int> vec; for (int i = 0; i != 5; ++i){ vec.push_back(i); // Append to end of vector } // Alternatively: vector<int> vec {0,1,2,3,4}; does the same //Accesing vectors: // index loop for (int i=0; i != vec.size(); ++i){ // Note that index of first vector is always 0 cout << "vec[" << i << "] = " << vec[i] << endl; } cout << endl << endl; // Easy iterators for (int x : vec){ // can ause "auto" instead of specificying type int cout << x << endl; } cout << endl << endl; // Proper iterators for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it){ // it is an iterator object, which itself is not the value but a placeholder // object that can move along // To get the actualy value that the iterator is pointing too use: *it cout << *it<< endl; } cout << endl << endl; vector<int>::iterator it = vec.begin(); while (it != vec.end()){ cout << *it << endl; ++it; } cout << endl << endl; //******************************************************************************************** // functions cout << "2 * 3 is " << multiply(2,3) << endl; cout << "2 * 2 is " << multiply(2) << " (overloaded function example)"; cout << endl << endl << endl; // Passing by value vs passing by reference num = 10; cout << "Passing by Value: " << endl; cout << " Before num = " << num << endl; DoubleByValue(num); cout << " After num = " << num << " (unchanged) " << endl; cout << "Passing by Reference: " << endl; DoubleByReference(num); cout << " After num = " << num << "(variable changed outside of scope) " << endl; cout << endl << endl << endl; //********************************************* // Pointers num = 10; // Imagine that this were a big fat variable // Pointer points to the location in memory for another variable (only 2 bytes) int* myptr = # // The * after int indicates that variable is a pointer that points the variables of type int // The &myobj return the address in RAM on where this address is stored // Get value to which pointer points to by using * again in front of cout << " Pointer ptr points to memory address " << myptr << " which has value " << *myptr << endl; return 0; // The exit code for // ****** classes }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}