int main() {
const int x; // This causes an error
const int x = 0; // This is correct
return 0;
}
int constantArguments(const int x) {
std::cout << x << std::endl; // This is fine
x = 5; // This causes an error
return x;
}
class Example {
public:
int x;
int GetX() const; //GetX is a constant member function
};
class Example {
public:
int x;
ClassA obj1;
ClassB obj2;
Example(int xValue, int arg1, int arg2) : x(xValue), obj1(arg2), obj2(arg1, arg2) {
class Example {
public:
const int x;
Example() {
x = 5; //This causes an error since you can't "change" the value of a const
}
}
class Example {
public:
const int x;
Example() : x(5);
}
const int x = 5)class Example {
public:
int x;
ClassA object1;
Example() {
x = 5;
object1.field1 = 1;
object1.field2 = 2;
}
}
class Example {
int x;
ClassA object1;
Example() : x(5), object1(1,2){}
}
//in file: "example.cpp"
class Example {
int x;
}
//in file: "b.cpp"
#include "example.cpp"
class B {
Example y;
}
//in file: "main.cpp"
#include "example.cpp"
#include "b.cpp"
int main() {
Example obj1;
B obj2;
return 0;
}
//in file: "main.cpp" after include statments are run
class Example {
int x;
}
class Example {
int x;
}
class B {
Example y;
}
int main() {
Example obj1;
B obj2;
return 0;
}
//in file: "example.h"
#ifndef EXAMPLE_H
#define EXAMPLE_H
class Example {
int x;
}
#endif
# indicates a Preprocessor Directive. These alter the source code as it is passed into the compiler
#include copies the code from the indicated source and pastes it where the include was called#ifndef checks if the given macro is not defined#define defines the given macro#endif closes the if statement started by #ifndefEXAMPLE_H macro is defined so the next time it attempts to include the file the code is hidden inside the if statement#pragma once which does the same thing as the header guard although it is not technically part of the C++ standardclass increment {
public:
int num;
increment(int n) : num(n){}
// This allows the functor to be called using () like a function
int operator () (int numIn) const {
return numIn + num;
}
};
int main() {
increment exampleFunctor(2);
int x = 5;
std::cout << exampleFunctor(5) << std::endl // Outputs 7
return 0;
}
[capture clause] (arguments) -> return-type
{
Definition of method
}
[capture clause] (arguments) -> return-type
{
Definition of method
}
| Access Type | Capture Clause |
|---|---|
| By Reference | [&] |
| By Value | [=] |
| Individually specify | [varA, &varB] |
#include <iostream>
using namespace std;
void print_sum(int x1, int x2) {
cout << x1 << " + " << x2 << " = " << x1 + x2 << endl;
}
int main() {
int calculated_value = 10;
auto print_plus_calculated_val = [&calculated_value](int x) {
print_sum(calculated_value, x);
};
for (int i = 0; i < 5; i++) {
print_plus_calculated_val(i);
}
}
count_ifcount_if is a STL function that counts the number of elements in a container that match the condition of the unary predicate it is givencount_if(InputIterator first, InputIterator last, UnaryPredicate pred)count_if example#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> x{ 1, 2, 3, 4, 5, 6, 7, 8 };
int numOdd = count_if(x.begin(), x.end(), [] (int in) {
return in%2;
});
std::cout<< "there were " << numOdd << " odd numbers" << std::endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string s("hello world");
std::transform(s.begin(), s.end(), s.begin(),
[](char c) { return std::toupper(c); });
std::cout << "s is: " << s << std::endl;
return 0;
}