Found 7347 Articles for C++

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

141 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

13K+ 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

140 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 12-Mar-2020 06:25:42

142 Views

In this tutorial, we will be discussing a program to understand Type interference in C++ (auto and decltype).In 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 typeExample Live Demo#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

310 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.)

transform_inclusive_scan() function in C++

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

53 Views

In this tutorial, we will be discussing a program to understand the transform_inclusive_scan() function in C++.Example Live Demo#include #include using namespace std; namespace point_input_iterator {    template    OutputItrator transform_inclusive_scan(InputItrator first,       InputItrator last,       OutputItrator d_first,       BinaryOperation binary_op,       UnaryOperation unary_op){             *d_first = unary_op(*first);       first++;       d_first++;       for (auto it = first; it != last; it++) {          //calculating the prefix sum          *d_first = binary_op(unary_op(*it), *(d_first ... Read More

Thread get_id() function in C++

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

110 Views

In this tutorial, we will be discussing a program to understand thread get_id() function in C++.Thread get_id() function verifies the current state of the process and then returns the id for the current thread in execution. This function doesn’t take any parameters.Example#include #include #include using namespace std; //creating thread void sleepThread(){    this_thread::sleep_for(chrono::seconds(1)); } int main(){    thread thread1(sleepThread);    thread thread2(sleepThread);    thread::id t1_id = thread1.get_id();    thread::id t2_id = thread2.get_id();    cout

Containership in C++

Ayush Gupta
Updated on 12-Mar-2020 06:08:55

2K+ Views

In this tutorial, we will be discussing a program to understand containership in C++.The parameter if a certain class contains another class is called as containership. The inside class is called contained class, while the class in which it is present is called container class.Example Live Demo#include using namespace std; class first {    public:    first(){       cout

Containers in C++ STL

sudhir sharma
Updated on 29-May-2024 13:11:59

1K+ Views

The C++ STL (Standard Template Library) is a powerful set of C++ template classes to provide general-purpose classes and functions with templates that implement many popular and commonly used algorithms and data structures like vectors, lists, queues, and stacks.It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized. Working knowledge of template classes is a prerequisite for working with STL.Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different ... Read More

Advertisements