Programming Articles - Page 2671 of 3366

Ternary Operator in Python?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

11K+ Views

Many programming languages support ternary operator, which basically define a conditional expression.Similarly the ternary operator in python is used to return a value based on the result of a binary condition. It takes binary value(condition) as an input, so it looks similar to an “if-else” condition block. However, it also returns a value so behaving similar to a function.Syntax[on_true] if [expression] else [on_false]Let’s write one simple program, which compare two integers -a. Using python if-else statement ->>> x, y = 5, 6 >>> if x>y:    print("x") else:    print("y") yb. Using ternary operator>>> x, y = 5, 6 >>> ... Read More

What is the use of cin.ignore() in C++?

Nishu Kumari
Updated on 11-Jun-2025 15:09:43

67K+ Views

In C++, cin.ignore() is a built-in function that belongs to the istream class and is defined in the header. It is used to skip characters left in the input buffer, especially the newline character (''), which can cause issues with input operations like getline(). In this article, we will explain the use of cin.ignore() in C++. Syntax of cin.ignore() Below is the syntax of the cin.ignore() function. cin.ignore(numeric_value, delimiter_char); // Example: cin.ignore(1000, ''); Here, numeric_value is the maximum number of characters to skip, and delimiter_char is the character at which the input should stop ... Read More

How to write a singleton class in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

1K+ Views

Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. For example, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. You can create a singleton class using the following code.Example#include using namespace std; class Singleton {    static Singleton *instance;    int data;    // Private constructor so that no objects can be created.    Singleton() {   ... Read More

What is volatile keyword in C++?

Akansha Kumari
Updated on 15-Jul-2025 17:06:41

12K+ Views

C++ Volatile KeywordIn C++, the volatile keyword is a type qualifier that tells the compiler that the value of a variable may be changed at any time unexpectedly, so it should not optimize access to that variable. This change in a variable may be influenced by any external factors such as hardware, signal handlers, or concurrent threads. This concept of volatile is mostly used when working with systems programming, embedded systems, and multi-threaded applications. SyntaxHere, a variable is declared volatile, which means the compiler volatile data_type variable_name; Example This example represents a variable whose value may be changed from an external source. ... Read More

What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__ in C/C++?

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

2K+ Views

Here we will see what are the differences between __FUNCTION__, __func__ and the __PRETTY_FUNCTION__ in C++.Basically the __FUNCTION__ and __func__ are same. Some old versions of C and C++ supports __func__. This macro is used to get the name of the current function. The _PRETTY_FUNCTION__ is used to return the detail about the function. Using this we can get which function is used, and in which class it is belonging, etc.Example#include using namespace std; class MyClass{    public:       void Class_Function(){          cout

What is a reentrant function in C/C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

4K+ Views

Here we will see what is the reentrant function in C or C++. One function is said to be a reentrant function if there is a provision to interrupt that function in the course of execution, then service the ISR (Interrupt Service Routine) and then resume the task. This type of functions is used in different cases like, recursions, hardware interrupt handling.For a reentrant function there should be some properties. These are listed below −This type of function will not use any global or static variable. There are no restrictions, but it is generally not advised. This is because the ... Read More

What is converting constructor in C++ ?

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

211 Views

In this section we will see what is the conversion constructor or converting constructor in C++ class. A constructor is a special type of function of class. It has some unique property like, its name will be same as class name, it will not return any value etc. The constructors are used to construct objects of a class. Sometimes constructors may take some arguments, or sometimes it does not take arguments.When a constructor takes only one argument then this type of constructors becomes conversion constructor. This type of constructor allows automatic conversion to the class being constructed.Example#include using namespace std; ... Read More

What is an unsigned char in C++?

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

6K+ Views

In C++ we have seen there is character type data called char. Sometimes we have seen unsigned char also. So here we will see what is basically the unsigned char means. What are the basic differences between signed char and unsigned char?Signed char and unsigned char both are used to store single character. The variable stores the ASCII value of the characters. For an example if ‘A’ is stored, actually it will hold 65. For signed char we need not to write the signed keyword. But for unsigned, we have to mention the keyword. The syntax is like below.unsigned char ... Read More

Meaning of 'const' last in a function declaration of a C++ class?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

826 Views

Sometimes we can find the keyword ‘const’ present at the last of function declaration. So what does it mean?Using this one function can be made as constant. The idea of constant function is that, the function cannot be modified from the objects, where they are called. It is recommended to use the constant functions in our program.Let us see one example of constant function.Example#include using namespace std; class MyClass {    int value;    public:       MyClass(int val = 0) {          value = val;       }       int getVal() const ... Read More

How to initialize private static members in C++?

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

24K+ Views

Here we will see how to initialize the private static member variables initialization in C++. We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class.To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.The following code will illustrate the of static member initializing technique.Example#include using namespace std; class MyClass{    private:       static int st_var;    public:       MyClass(){          st_var++; //increase the value ... Read More

Advertisements