c++ Thread Example

In this c++ thread example, we wrap a class object in a thread and execute it while running a loop from the main thread. std::thread myThread(ShowMeTheThread());...myThread.join() // Why wrap a process or class in a c++ thread? Generally, we would run a method or class in a thread when it either has a long running task or when something is going to probably stop it which if it were run in the...
Read More

C++ Random Generator

True c++ random number generators for computers are difficult to come by, but one that will suit most intents and purposes can be found here. The function searched for by most frequently by c++ random or c++ srand time . #include <ctime>srand(time(0)); // seed the random number generator with your clockint x = rand() % 15 + 1; // Numbers from 1 to 15Do not use for real money casino games...
Read More

C++ enum sample code

enum colors{ red, green, blue};std::cout << "Red " << red << " Green " << green << " Blue " << blue << "\n"; enum is short for enumeration and in this process a list is assigned numerical values, each item in the list, one more than the item before it. If one arbitrarily assigns a value of 4 to an enum item, the next item will be 5 // Example...
Read More
c++ logging example code

C++ logger class ( Basic )

Sometimes its necessary to take everything we have covered already and write some basic sample class like this c++ logging example. There are logger classes that are generally used for debugging, timing performance and a plethora of other things. Central to most logger or logging classes is the theme of being able to set the level of the warnings that the programmer wants to see while coding....
Read More

C++ continue vs break

for ( int i = 0; i < 5; i++) if ( i % 2 ) continue; // next point of execution is the for ( ... The continue statement above will not allow the program to execute any of the code below which is inside the same for loop. The next instruction to be executed will be the test for i < 5 in order to ascertain whether the code should continue running or not. The code below shows the use of the...
Read More

C++ Class Examples

C++ class constructorclass Entity{ public: private: Entity();} /* Lesson Setting up a Class. Several ways to do so. Constructor parameters or initial vars in the public class Author : Trevor Weir Date : Mar 19, 2019 ( 1st day of spring ) */ #include <string> #include <iostream> class Entity{ private: int m_score; std::string m_name; public: Entity() :m_score(0), m_name("unknown"){};...
Read More

C++ Vectors vs Arrays

#include <vector>vector<int> v;for( int i=1; i < 5 ;i++) v.push_back(i); #include <iostream> #include <vector> using namespace std; int main() { vector<int> g1; for (int i = 1; i <= 5; i++) g1.push_back(i); cout << "Output of begin and end: "; for (auto i = g1.begin(); i != g1.end(); ++i) cout << *i << " "; } Line 2: #include <vector> // Use of...
Read More

C++ Pointer to String Array

string var friends = {"Amy","John","Joyce","Deszrene","Samson"};string *ptr;ptr = &friends ; // pointer to the 3rd position in arraystd::cout << "Value of friends position 3 = " << *ptr << std::endl; C++ string pointers to specific positions in the string array Other good articles on c++ pointers to string arrays are over here. Feel Free to Copy the code below and test it...
Read More

C++ Pointer to Char Array

C++ Pointer to Char Array examples below. char var = {'1','2','3','\0'};char *ptr; // c++ pointer to a char arrayptr = &var ; // points to the start of the arraystd::cout << "Address of var is " << &var << std::endl;std::cout << "Value of var is " << *ptr << std::endl;Last std::cout will print all the characters starting from position 0. Other users...
Read More

C++ Pointers and References

int var = 10;int *ptr;ptr = &var;std::cout << "Address of var is " << &var << std::endl;std::cout << "Value of var is " << *ptr << std::endl; Why we need C++ Pointers and References C++ pointers are not difficult to learn. In truth, there are a few C++ tasks that are performed more easily with pointers, and other C++ tasks, such as dynamic memory...
Read More
Top