Found 7346 Articles for C++

Destructors in C++

Arjun Thakur
Updated on 24-Jun-2020 11:36:56

17K+ Views

Destructors in C++ are members functions in a class that delete an object. They are called when the class object goes out of scope such as when the function ends, the program ends, a delete variable is called etc.Destructors are different from normal member functions as they don’t take any argument and don’t return anything. Also, destructors have the same name as their class and their name is preceded by a tilde(~).A program that demonstrates destructors in C++ is given as follows.Example Live Demo#include using namespace std; class Demo {    private:    int num1, num2;    public:    Demo(int n1, ... Read More

Constructors in C++

Arjun Thakur
Updated on 24-Jun-2020 11:39:55

9K+ Views

Constructors are functions of a class that are executed when new objects of the class are created. The constructors have the same name as the class and no return type, not even void. They are primarily useful for providing initial values for variables of the class.The two main types of constructors are default constructors and parameterized constructors. Details about these are given as follows.Default ConstructorsDefault constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are ... Read More

Nested Classes in C++

Ankith Reddy
Updated on 24-Jun-2020 11:40:43

23K+ Views

A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class.A program that demonstrates nested classes in C++ is as follows.Example Live Demo#include using namespace std; class A {    public:    class B {       private:       int num;       public:       void getdata(int n) {          num = n;       }       void putdata() {          cout

Local Class in C++

George John
Updated on 24-Jun-2020 11:23:17

2K+ Views

A class declared inside a function is known as a local class in C++ as it is local to that function.An example of a local class is given as follows.#include using namespace std; void func() {    class LocalClass {    }; } int main() {    return 0; }In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as a local class.A local class name can only be used in its function and not outside it. Also, the methods of a local class must be defined inside it only. ... Read More

Abstraction in C++

Arjun Thakur
Updated on 24-Jun-2020 11:25:15

1K+ Views

Abstraction involves providing only the pertinent information to the outside world and hiding the background details. It relies on the separation of interface and implementation for programming.Classes provide abstraction in C++. They provide public methods for the outside world to manipulate data and keep the rest of the class structure to themselves. So the users can use the class as required without knowing how it has been implemented internally.A program to implement abstraction in C++ using classes is given as follows.Example Live Demo#include using namespace std; class Abstraction {    private:    int length, breadth;    public:    void setValues(int ... Read More

strdup() and strdndup() in C/C++

karthikeya Boyini
Updated on 24-Jun-2020 11:27:03

5K+ Views

strdup()The function strdup() is used to duplicate a string. It returns a pointer to null-terminated byte string.Here is the syntax of strdup() in C language, char *strdup(const char *string);Here is an example of strdup() in C language, Example Live Demo#include #include int main() {    char *str = "Helloworld";    char *result;    result = strdup(str);    printf("The string : %s", result);    return 0; }OutputThe string : Helloworldstrndup()The function strndup works similar to the function strndup(). This function duplicates the string at most size bytes i.e. the given size in the function. It also returns a pointer to null-terminated ... Read More

strcoll() in C/C++

karthikeya Boyini
Updated on 24-Jun-2020 11:14:50

178 Views

The function strcoll() is used to compare two strings using locale - specific collating sequence.It returns −zero, when both strings are same, greater than zero value when first string is greater than otherless than zero value, when first string is less than other.Here is the syntax of strcoll() in C language, int strcoll(const char *first_string, const char *second_string);Here is an example of strcoll() in C language, Example Live Demo#include #include int main () {    const char s1[] = "Helloworld";    const char s2[] = "Blank";    char *result;    result = strcoll(s1, s2);    if(result > 0)   ... Read More

atol(), atoll() and atof() functions in C/C++

karthikeya Boyini
Updated on 24-Jun-2020 11:18:30

555 Views

atol() FunctionThe function atol() converts string into a long integer. It returns zero, when no conversion is performed. It returns the converted long int value.Here is the syntax of atol in C++ language,long int atol(const char *string)Here is an example of atol() in C++ language,Example Live Demo#include using namespace std; int main() {    long int a;    char str[20] = "538756";    a = atol(str);    cout

Const member functions in C++

Samual Sam
Updated on 24-Jun-2020 11:19:02

17K+ Views

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided.A const member function can be called by any type of object. Non-const functions can be called by non-const objects only.Here is the syntax of const member function in C++ language, datatype function_name const();Here is an example of const member function in C++, Example Live Demo#include using namespace std; class Demo {    int val;    public:    Demo(int x = 0) { ... Read More

Ceil and floor functions in C++

karthikeya Boyini
Updated on 24-Jun-2020 11:19:37

5K+ Views

The ceil FunctionThe ceil function returns the smallest possible integer value which is equal to the value or greater than that. This function is declared in “cmath” header file in C++ language. It takes single value whoes ceil value is to be calculated. The datatype of variable should be double/float/long double only.Here is the syntax of ceil function in C++ language, double ceil(double x); float ceil(float x);Here is an example of ceil function in C++ language, Example Live Demo#include #include using namespace std; int main() {    float var = 1234.25;    float res;    res = ceil(var);   ... Read More

Advertisements