Found 33676 Articles for Programming

Object Slicing in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

549 Views

Object slicing is used to describe the situation when you assign an object of a derived class to an instance of a base class. This causes a loss of methods and member variables for the derived class object. This is termed as information being sliced away. For example, class Foo {    int a; }; class Bar : public Foo {    int b; }Since Bar extends Foo, it now has 2 member variables, a and b. So if you create a variable bar of type Bar and then create a variable of type Foo and assign bar, you'll lose ... Read More

transform() in C++

Samual Sam
Updated on 30-Jul-2019 22:30:26

3K+ Views

The transform function is present in the C++ STL. To use it, we have to include the algorithm header file. This is used to perform an operation on all elements. For an example if we want to perform square of each element of an array, and store it into other, then we can use the transform() function.The transform function works in two modes. These modes are −Unary operation modeBinary operation modeUnary Operation ModeIn this mode the function takes only one operator (or function) and convert into output.Example#include #include using namespace std; int square(int x) {    //define square ... Read More

What is NaN in C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

911 Views

The NaN is the abbreviation of Not a Number. It indicates undefined or non-representable floating point elements. One example of NaN is square root of some negative number, or result of 0/0.Example#include #include using namespace std; int main() {    cout >> "Square root of -5: " >> sqrt(-5) >> endl; }OutputSquare root of -5: nan

What does buffer flush means in C++ ?

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

The buffer flush is used to transfer of computer data from one temporary storage area to computers permanent memory. If we change anything in some file, the changes we see on the screen are stored temporarily in a buffer.In C++, we can explicitly have flushed to force the buffer to be written. If we use std::endl, it adds one new line character, and also flush it. If this is not used, we can explicitly use flush. In the following program at first no flush is used. Here we are trying to print the numbers, and wait for one seconds. For ... Read More

regex_error in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

258 Views

The regex library has different methods and features related to regular expressions. Here we will see some regex_errors. These are also present at regex library. During executing some regular expressions, we get some errors. That errors are mentioned here.FlagsErrorserror_collateIn the Regex, the names having invalid collation.error_ctypeIn the Regex, there is an invalid character class name.error_stackNot enough memory to determine regex can be matched or not.error_spaceConvert into Finite State Machine, when memory is insufficienterror_badrepeatThe string has repeat specifier ( *?+{) that was not preceded by a valid regular expression.error_complexityThe complexity of an attempted match against a regex exceeded a pre-set levelerror_rangeContaining ... Read More

C qsort() vs C++ sort()

Smita Kapse
Updated on 30-Jul-2019 22:30:26

579 Views

Here we will see what are the differences between qsort() in C, and sort() in C++.The C provides qsort() function, that can be used for sorting an array. The function arguments and syntax is like below.void qsort(void *base, size_t num, size_t size, int (*comparator) (const void*, const void*));This function takes the base address of that array, the number of elements of that array. Size of each item in the array, and a comparator function.The C++ provides sort() function. This is present inside C++ STL. The arguments and syntax is like below.void sort(T first, T last, Compare c);Here the order of ... Read More

Implementation of a Falling Matrix in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

592 Views

We have seen falling matrix scene in different films etc. Here we will see how to write a C++ program to do like that.To solve this problem, we have to care about these steps.Define width of the matrixTwo successive characters may or may not have same amount of gap between themA certain amount of delay between printing each line to visualize the falling effect.Example#include #include #include #include #include #include const int wd = 70; //set the width of the matrix window const int flipsPerLine =5; //five flips for the boolean array 'alternate' const int sleepTime = 50; //it will take ... Read More

Create JList and always display the scroll bar in Java?

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

511 Views

To always display the scroll bar in a JList, use the properties HORIZONTAL_SCROLLBAR_ALWAYS and VERTICAL_SCROLLBAR_ALWAYS:JList list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);The following is an example to create JList and display the scroll bar:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"};       list = new JList(sports);       ... Read More

Rint(), rintf(), rintl() in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

182 Views

Here we will see three functions. These functions are Rint(), rintf() and the rintl(). These functions are used to convert floating point values into rounded format.The rint() FunctionThis function is used for rounding floating point value to integer. The syntax is like below. If the result is outside of return type, the domain error may occur. When the argument is 0 or infinity, then it will return unmodifiedfloat rint(float argument) double rint(double argument) long double rint(long double argument)Example#include #include using namespace std; main() {    double a, b, x, y;    x = 53.26;    y = 53.86; ... Read More

Catch block and type conversion in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

190 Views

In this section, we will see how to use the catch block for exception handling and the type conversion in C++.At first, let us see a code, and we will see what will be the output, and how they are generating.Example#include using namespace std; int main() {    try{       throw 'a';    }    catch(int a) {       cout

Advertisements