Found 7347 Articles for C++

When should we write our own assignment operator in C++?

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

337 Views

Here we will see when we need to create own assignment operator in C++. If a class do not have any pointers, then we do not need to create assignment operator and copy constructor. C++ compiler creates copy constructor and assignment operator for each class. If the operators are not sufficient, then we have to create our own assignment operator.Example Live Demo#include using namespace std; class MyClass { //no user defined assignment operator or copy constructor is present    int *ptr;    public:       MyClass (int x = 0) {          ptr = new int(x);       }    void setValue (int x) {       *ptr = x;    }    void print() {       cout

ctime() Function in C/C++

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

333 Views

The C library function char *ctime(const time_t *timer) returns a string representing the localtime based on the argument timer.The returned string has the following format − Www Mmm dd hh:mm:ss yyyy, where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.The syntax is like below −char *ctime(const time_t *timer)This function takes the pointer to a time_t, which is containing the calendar time. It returns a string containing date, time info in human readable format.Example Live Demo#include #include int main () {    time_t curtime;    time(&curtime); ... Read More

isnormal() in C++

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

81 Views

In this section we will see the isnormal() function in C++. This function is present in the cmath library. This function is used to check whether a number is normal or not. The numbers that are considered as non-normal are zeros, infinity or NAN.This function takes float, double or long double values as argument. Returns 1 if the number is normal, otherwise returns 0.Example Live Demo#include #include using namespace std; int main() {    cout

Scope Resolution Operator Versus this pointer in C++?

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

205 Views

Here we will see some C++ examples and try to get what type of output will generate. Then we can understand the purpose and functions of scope resolution operator and the ‘this’ pointer in C++.If some code has some members say ‘x’, and we want to use another function that takes an argument with the same name ‘x’, then in that function, if we use ‘x’, it will hide the member variable, and local variable will be used. Let us check this in one code.Example Live Demo#include using namespace std; class MyClass {    private:       int x; ... Read More

Foreach in C++ and Java

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

125 Views

In C++ and Java, there are another type of loop called foreach loop. This is not present in C. This loop has introduced in C++11 and Java JDK 1.5.0. The advantage of this loop is that, it can access the elements very quickly without performing initialization, testing and increment/decrement. This loop is used to access every element in one array or some containers. This loop is known as foreach but to denote this loop we have to use ‘for’ keyword. The syntax is different than normal for and foreach.for(datatype item : Array) { }Let us see some examples of foreach ... Read More

Comparison of Exception Handling in C++ and Java

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

175 Views

The exception handling feature is present almost any object oriented languages nowadays. In C++ and Java also we can get this kind of feature. There are some similarities between exception handling in C++ and exception handling in Java, like in both languages we have to use the try-catch block. Though there are some difficulties also. These are like below −In C++, we can throw any type of data as exception. Any type of data means primitive datatypes and pointers also. In Java we can only throw the throwable objects. Subclasses of any throwable class will also be throwable.Example Live Demo#include ... Read More

Write a program that produces different results in C and C++

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

55 Views

Here we will see some program that will return different results if they are compiled in C or C++ compilers. We can find many such programs, but here we are discussing about some of them.In C and C++, the character literals are treated as different manner. In C, they are treated as int but in C++, they are treated as characters. So if we check the size using sizeof() operator, it will return 4 in C, and 1 in C++.Live Demo For C.Example Live Demo#include int main() {    printf("The character: %c, size(%d)", 'a', sizeof('a')); }Output(C)The character: a, size(4)Live Demo For ... Read More

C program that won’t compile in C++

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

368 Views

The C++ language is designed by adding some additional features like Object Oriented concepts with C. Most of the C programs can be compiled using C++ compiler also. Though there are some programs that cannot be compiled using C++ compiler.Let us see some code, that will compile in C compiler, but not in C++ compilers.In this program there will be one compilation error for C++ code. Because it is trying to call a function that is not declared before. But in C it may compileLive Demo For C.Example Live Demo#include int main() {    myFunction(); // myFunction() is called before its ... Read More

What is the difference between iostream and iostream.h in C++?

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

1K+ Views

Here we will see what are the differences between iostream and iostream.h in C++. The iostream.h was a header file used by the early 1990's I/O streams library. This was developed at AT&T for use with early C++. In that time C++ was not standardized.iostream header file used by the C++ standard library. This is first published in 1998, to provide access to standard I/O streams.There was never a C++ standard that made any mention of "iostream.h", but pre-standard references, such as Annotated C++ Reference Manual from 1990 did use that header file.

When to use inline function and when not to use it in C/C++?

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

2K+ Views

In C++, there is one good feature called inline function. This kind of functions are like the macros of C or C++. To use inline functions, we have to specify the inline keyword. We can use this type of functions anywhere, but we should follow some guideline.When inline can be used?Inline functions can be used in the place of macros (#define)For small functions we can use inline functions. It creates faster code and smaller executables.When functions are small and called very often, we can use inline.When we should avoid the use of inline?We should not use functions that are I/O ... Read More

Advertisements