Found 34494 Articles for Programming

What is the size of an object of an empty class in C++?

Samual Sam
Updated on 26-Jun-2020 09:17:42

255 Views

The following is an example to find the size of object of an empty class.Example Live Demo#include using namespace std; class p1 {    public:    void first() {       cout

How to call a parent class function from derived class function in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:18:28

9K+ Views

The following is an example to call parent class function from derived class function.Example Live Demo#include using namespace std; class p1 {    public:    void first() {       cout

What is a reference variable in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:19:43

19K+ Views

Reference variable is an alternate name of already existing variable. It cannot be changed to refer another variable and should be initialized at the time of declaration and cannot be NULL. The operator ‘&’ is used to declare reference variable.The following is the syntax of reference variable.datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variableHere, datatype − The datatype of variable like int, char, float etc.variable_name − This is the name of variable given by user.refer_var − The name of reference variable.The following is an example of reference variable.Example Live Demo#include using namespace std; int main() { ... Read More

What is the difference between an int and a long in C++?

Samual Sam
Updated on 26-Jun-2020 09:20:19

1K+ Views

intThe datatype int is used to store the integer values. It could be signed or unsigned. The datatype int is of 32-bit or 4 bytes. It requires less memory area than long to store a value. The keyword “int” is used to declare an integer variable.The following is the syntax of int datatype.int variable_name;Here,variable_name − The name of variable given by user.The following is an example of int datatype.Example Live Demo#include using namespace std; int main() {    int a = 8;    int b = 10;    int c = a+b;    cout

How to convert a single char into an int in C++

Samual Sam
Updated on 26-Jun-2020 09:09:53

596 Views

The following is an example to convert a character into int.Example Live Demo#include using namespace std; int main() {    char c = '8';    int i = c - 48;    cout

Why does C++ require a cast for malloc() but C doesn't?

Samual Sam
Updated on 26-Jun-2020 09:11:25

711 Views

In C language, the void pointers are converted implicitly to the object pointer type. The function malloc() returns void * in C89 standard. In earlier versions of C, malloc() returns char *. In C++ language, by default malloc() returns int value. So, the pointers are converted to object pointers using explicit casting.The following is the syntax of allocating memory in C language.pointer_name = malloc(size);Here, pointer_name − Any name given to the pointer.size − Size of allocated memory in bytes.The following is an example of malloc() in C language.Example Live Demo#include #include int main() {    int n = 4, ... Read More

Variable initialization in C++

karthikeya Boyini
Updated on 26-Jun-2020 09:12:44

8K+ Views

Variables are the names given by the user. A datatype is also used to declare and initialize a variable which allocates memory to that variable. There are several datatypes like int, char, float etc. to allocate the memory to that variable.There are two ways to initialize the variable. One is static initialization in which the variable is assigned a value in the program and another is dynamic initialization in which the variables is assigned a value at the run time.The following is the syntax of variable initialization.datatype variable_name = value;Here, datatype − The datatype of variable like int, char, float ... Read More

How to catch a divide by zero error in C++?

Samual Sam
Updated on 26-Jun-2020 09:13:25

521 Views

The following is an example to catch a divide by zero error.Example Live Demo#include using namespace std; int display(int x, int y) {    if( y == 0 ) {       throw "Division by zero condition!";    }    return (x/y); } int main () {    int a = 50;    int b = 0;    int c = 0;    try {       c = display(a, b);       cout

How to calculate Execution Time of a Code Snippet in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:14:20

2K+ Views

We can calculate the execution time of a code snippet by using following syntax −auto start = high_resolution_clock::now(); // Start time // Code snippet auto stop = high_resolution_clock::now(); // Stop time auto duration = duration_cast(stop - start); // DurationThe class high_resolution_clock is defined in “chrono” header file. The function now() is returning a value corresponding to the call’s point in time.A header file is used to record the time taken by that particular code.#include using namespace std::chrono;The following is an example to calculate execution time of a code snippet.Example Live Demo#include #include using namespace std::chrono; using namespace std; ... Read More

How to use enums in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:17:06

10K+ Views

Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.The following is the syntax of enums.enum enum_name{const1, const2, ....... };Here, enum_name − Any name given by user.const1, const2 − These are values of type flag.The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows −enum colors{red, black}; enum suit{heart, diamond=8, spade=3, club};The following is an example of ... Read More

Advertisements