Found 7401 Articles for C++

C++ Program to use above below primitive to test whether two lines intersect

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

155 Views

Here is a C++ program to use above below primitive to test whether two lines intersect. It can be used to test whether a line intersects a line segment. It does if and only if one endpoint of the segment is to the left of the line and the other is to the right.AlgorithmBegin    For generating equation of the first line, generate random numbers for coefficient of x and y by using rand at every time of compilation.    For generating equation of the second line, generate random numbers for coefficient of x and y by using rand at ... Read More

C++ Program to implement Slicker Algorithm that avoids Triangulation to find Area of a Polygon

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

81 Views

Here is a C++ program to find the area of polygon using slicker algorithm that avoids Triangulation to find area of a polygon.It assumes the usual mathematical convention that positive y points upwards. In computer systems where positive y is downward, the easiest thing to do is to list the vertices counter-clockwise using the “positive y down” coordinates. Two effects then canceled out to produce a positive area.Functions and pseudocodeAlgorithmBegin    function Area() is used to calculate area of a polygon take the polygon p as argument.    for i = 0 to p.n-1       initialize j = ... Read More

C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line

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

343 Views

This is a C++ program to apply Above-Below-on Test to find the position of a point with respect to a Line. For any point t (xt, yt) on the plane, its position with respect to the line L connecting m and n is found by calculating the scalar s −Y = A xt + B yt + CIf Y< 0, t lies in the clockwise halfplane of L; if Y>0, t lies on the counter-clockwise halfplane; if Y= 0, t lies on L.AlgorithmBegin    Take the points as input.    For generating equation of the line, generate random numbers for ... Read More

Template Metaprogramming in C++

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

210 Views

When we write programs to do computation at compile time using a template, that is called Template Metaprogramming.Example Code#include using namespace std; templatestruct power {    enum { value = 4*power::value }; }; templatestruct power {    enum { value = 1 }; }; int main() {    cout

Variadic function templates in C++

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

219 Views

Variadic function templates in C++ is a function which can take a multiple number of arguments.Syntaxtemplate(typename arg, typename... args) return_type function_name(arg var1, args... var2)Example Code Live Demo#include using namespace std; void show() //base case. {    cout

Default virtual behavior in C++ vs Java

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

118 Views

In C++ methods are non-virtual by default. They can be made virtual function by using a virtual keyword.Example Code#include using namespace std; class B {    public: void s() //non virtual by default. Use virtual before the function to print “In Derived” {       cout

Why do we need a pure virtual destructor in C++?

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

667 Views

There are no ill-effects of allowing a pure virtual destructor in C++ program. It is must to provide a function body for pure virtual destructor as derived class’s destructor is called first before the base class destructor, so if we do not provide a function body, it will find out nothing to be called during object destruction and error will occur. We can make easily an abstract class by making a pure virtual destructor with its definition.Example Code Live Demo#include using namespace std; class B {    public: virtual ~B()=0; // Pure virtual destructor }; B::~B() {    cout

Why is a C++ pure virtual function initialized by 0?

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

2K+ Views

It’s just a syntax, nothing more than that for saying that “the function is pure virtual”.A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in declaration.Here is an example of pure virtual function in C++ programExample Code Live Demo#include using namespace std; class B {    public: virtual void s() = 0; // Pure Virtual Function }; class D:public B {    public: void s() {       cout s(); }OutputVirtual Function in Derived class

What is a virtual base class in C++?

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

1K+ Views

The virtual base class is used when a derived class has multiple copies of the base class.Example Code#include using namespace std; class B {    public: int b; }; class D1 : public B {    public: int d1; }; class D2 : public B {    public: int d2; }; class D3 : public D1, public D2 {    public: int d3; }; int main() {    D3 obj;    obj.b = 40; //Statement 1, error will occur    obj.b = 30; //statement 2, error will occur    obj.d1 = 60;    obj.d2 = 70;    obj.d3 = 80;    cout

Pure Virtual Functions and Abstract Classes in C++

Nishtha Thakur
Updated on 07-Oct-2023 02:39:22

22K+ Views

A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function. Abstract class can have normal functions and variables along with a pure virtual function. Abstract class cannot be instantiated, but pointers and references of Abstract class type can be created. ... Read More

Advertisements