Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Beginners Lab Assignments Code Examples

make your functions to be inline.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* make your functions to be inline. An inline function is a function in which body is inserted in the place of its call. These functions can be compared with Macros. Inline functions are used to improve performance of the application. However, many inline functions can lead your program to grow up in size. That is why often only small functions are declared as inline. There are two ways to make your functions to be inline. The first one consists in simple definition of member function in the body of class declaration: */ class Person { public: //an inline function string getFirstName() { return firstName; } private: string firstName; string lastName; tm dateOfBirth; }; getFirstName is an inline function in this case. The use of inline functions has following advantages: The removing of some unnecessary instructions for function's call make programs faster. When many small functions are often called compiler generates more code for function's calls. The correct use of inline functions makes programs smaller. Sometimes it seems that inline functions can increase performance of an application. However, the use of many inline functions can cause different problems: Many inline functions can cause the growth of binary executable file. Inline function must be declared in every compilation unit where it is used.

Inline function is one of the important feature of C++. So, let's first understand why inline functions are used and what is the purpose of inline function? When the program executes the function call instruction the CPU stores the memory address of the instruction following the function call, copies the arguments of the function on the stack and finally transfers control to the specified function. The CPU then executes the function code, stores the function return value in a predefined memory location/register and returns control to the calling function. This can become overhead if the execution time of function is less than the switching time from the caller function to called function (callee). For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run. However, for small, commonly-used functions, the time needed to make the function call is often a lot more than the time needed to actually

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

The function in C++ language is also known as procedure or subroutine in other programming languages. To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability. Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check... Function declaration, is done to tell the compiler about the existence of the function. Function's return type, its name & parameter list is mentioned. Function body is written in its definition. Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them:

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

Get type name. name() returns a null-terminated character sequence that may identify the type. The particular representation pointed by the returned value is implementation-defined, and may or may not be different for different types. This function does not accept any parameter.

Generates a undirected random graph for the given number "edges and vertexes". The time complexity of this algorithm is O(v*e). Takes the input of the number of vertexes & edges