Found 7197 Articles for C++

Core Dump (Segmentation fault) in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:54:10

5K+ Views

In this tutorial, we will be discussing a program to understand core dump (segmentation fault) in C/C++.It happens due to reasons like when code tries to write on read only memory or tries to access corrupt memory location.ExampleModifying a string literalint main(){    char *str;    str = "GfG";    *(str+1) = 'n';    return 0; }Accessing out of array index bounds#include using namespace std; int main(){    int arr[2];    arr[3] = 10;    return 0; }Accessing an address which is freed#include #include int main(void){    int* p = malloc(8);    *p = 100;    free(p);    *p = 110;    return 0; }OutputAbnormal termination of program

Converting Strings to Numbers in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:52:05

275 Views

In this tutorial, we will be discussing a program to understand how to convert strings into numbers in C/C++.C/C++ provides two ways to convert strings into numbers.Example Live DemoUsing sscanf()#include int main(){    const char *str = "12345";    int x;    sscanf(str, "%d", &x);    printf("The value of x : %d", x);    return 0; }OutputThe value of x : 12345Using stoi() Live Demo#include #include using namespace std; int main(){    string str1 = "45";    string str2 = "3.14159";    string str3 = "31337 geek";    int myint1 = stoi(str1);    int myint2 = stoi(str2);    int myint3 = stoi(str3);    cout

Convert a String to Integer Array in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:50:25

3K+ Views

In this tutorial, we will be discussing a program to understand how to convert a string into integer array in C/C++.For this we will create a new array. Traverse through the given string, if the character is a comma “, ”, we move on to the next character else add it to the new array.Example Live Demo#include using namespace std; //converting string to integer array void convert_array(string str){    int str_length = str.length();    int arr[str_length] = { 0 };    int j = 0, i, sum = 0;    //traversing the string    for (i = 0; str[i] != ... Read More

Conversion of whole String to uppercase or lowercase using STL in C++

Ayush Gupta
Updated on 16-Mar-2020 09:47:51

5K+ Views

In this tutorial, we will be discussing a program to understand conversion of whole string to uppercase or lowercase using STL in C++.To perform this transformation, C++ STL provides toupper() and tolower() functions to convert to uppercase and lowercase respectively.Example Live Demo#include using namespace std; int main(){    string su = "Tutorials point";    transform(su.begin(), su.end(), su.begin(), ::toupper);    cout

Virtual Function in C++

Ayush Gupta
Updated on 12-Mar-2020 06:40:10

2K+ Views

In this tutorial, we will be discussing a program to understand virtual functions in C++.Virtual function is the member function defined in the base class and can further be defined in the child class as well. While calling the derived class, the overwritten function will be called.Example Live Demo#include using namespace std; class base {    public:    virtual void print(){       cout

Virtual destruction using shared_ptr in C++

Ayush Gupta
Updated on 12-Mar-2020 06:36:51

192 Views

In this tutorial, we will be discussing a program to understand virtual destruction using shared_ptr in C++.To delete the instances of a class, we define the destructor of the base class to be virtual. So it deletes the various object instances inherited in the reverse order in which they were created.Example Live Demo#include #include using namespace std; class Base {    public:    Base(){       cout

Virtual base class in C++

Ayush Gupta
Updated on 12-Mar-2020 06:34:40

17K+ Views

In this tutorial, we will be discussing a program to understand virtual base class in C++.Virtual classes are primarily used during multiple inheritance. To avoid, multiple instances of the same class being taken to the same class which later causes ambiguity, virtual classes are used.Example Live Demo#include using namespace std; class A {    public:    int a;    A(){       a = 10;    } }; class B : public virtual A { }; class C : public virtual A { }; class D : public B, public C { }; int main(){    //creating class D object    D object;    cout

Using class to implement Vector Quantities in C++

Ayush Gupta
Updated on 12-Mar-2020 06:28:56

193 Views

In this tutorial, we will be discussing a program to understand how to use class to implement vector quantities in C++.Vector quantities are the ones which have both magnitude and direction. Here we will be implementing them using classes and then performing basic operations on them.Example Live Demo#include #include using namespace std; class Vector {    private:    int x, y, z;    //components of the Vector    public:    Vector(int x, int y, int z){       this->x = x;       this->y = y;       this->z = z;    }    Vector operator+(Vector ... Read More

Type Inference in C++ (auto and decltype)

Ayush Gupta
Updated on 03-Dec-2024 22:12:18

250 Views

In this tutorial, we will be discussing a program to understand Type interference in C++ (auto and decltype). In the case of auto keyword, the type of the variable is defined from the type of its initializer. Further, with decltype, it lets you extract the type of variable from the called element. auto type Example Here is the following example of auto-type in C++. #include using namespace std; int main() { auto x = 4; auto y = 3.37; auto ptr = & x; cout

Trivial classes in C++

Ayush Gupta
Updated on 12-Mar-2020 06:22:23

482 Views

In this tutorial, we will be discussing a program to understand trivial classes in C++.When a class/ struct contains explicitly defaulted value inside it, then it is known as Trivial classes. Further trivial classes have their own constructor, assignment operator and destructor.Example//using the default constructor struct Trivial {    int i;    private:    int j; }; //defining your own constructor //and then marking it as default struct Trivial2 {    int i;    Trivial2(int a, int b){       i = a;    }    Trivial2() = default; };Output(No output as we are just defining classes here and not creating object instances from them.)

Advertisements