title-img


Messages Order C++

In real life applications and systems, a common component is a messaging system. Thea idea is that a sender sends messages to the recipient. The messages might be sent for example over the network. However, some network protocols don't guarantee to preserve the order of sent messages while they are received by the recipient. For example, if someone sends a text messages hello, hi and what's up, they might be received in the order what's up, hello, hi. In many systems the expected behavior is to

View Solution →

C++ Class Templates

A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments. Here is an example of a class, MyTemplate, that can store one element of any type and that has just one member function divideBy2, which divides its value by 2. template <class T> class MyTemplate { T element; public: MyTemplate (T arg) {element=arg;}

View Solution →

Preprocessor Solution C++

Preprocessor directives are lines included in the code preceded by a hash sign (#). These lines are directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements. #define INF 10000000 if( val == INF) { //Do something } After the preprocessor has replaced the directives, the code will be if( val == 10000000) { //Here INF is replaced by the value with

View Solution →

Operator Overloading C++

Classes define new types in C++. Types in C++ not only interact by means of constructions and assignments but also via operators. For example: int a=2, b=1, c; c = b + a; The result of variable c will be 3. Similarly, classes can also perform operations using operator overloading. Operators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax

View Solution →

Overload Operators C++

You are given a class - Complex. class Complex { public: int a,b; }; Operators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax is: type operator sign (parameters) { /*... body ...*/ } You need to overload operators + and << for the Complex class. The operator + should add complex numbers according to the rules of complex ad

View Solution →