Found 7401 Articles for C++

Difference between private, public, and protected inheritance in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

3K+ Views

Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to access directly the internal representation of a class type. The access restriction to the class members is specified by the labeled access modifiers: public, private, and protected sections within the class body.The default access for members and classes is private.Example Codeclass Base { public:     // public members go here protected: // protected members go here private:     ... Read More

Difference between 'struct' and 'typedef struct' in C++ program?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

5K+ Views

Basically struct is used to define a structure. But when we want to use it we have to use the struct keyword in C. If we use the typedef keyword, then a new name, we can use the struct by that name, without writing the struct keyword.In C++, there is no difference between 'struct' and 'typedef struct' because, in C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name.Though there is one subtle difference that typedefs cannot be forward declared. So for the typedef option, ... Read More

Regular cast vs. static_cast vs. dynamic_cast in C++ program

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

469 Views

static_cast − This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc.dynamic_cast − This cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritance when you cast from base class to derived class.Regular Cast − This is the most powerful cast available in C++ as it combines const_cast, static_cast and reinterpret_cast. but it's also ... Read More

What does int argc, char *argv[] mean in C++?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

596 Views

The argc stands for argument count and argv stands for argument values. These are variables passed to main function when it starts executing. When we run a program we can give arguments to that program like:$ ./a.out helloHere hello is an argument to the executable. This can be accessed in your program.Example Code#include using namespace std; int main(int argc, char** argv) {    cout

What is a segmentation fault in C/C++ program?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

919 Views

A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.Segmentation faults are mostly caused by pointers that are:Used to being properly initialized.Used after the memory they point to has been reallocated or freed.Used in an indexed array where the index is outside of the array bounds.

How to pass objects to functions in C++ Program?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

7K+ Views

There are four ways of passing objects to functions. Let's assume you have a class X and want to pass it to a function fun, thenPass by ValueThis creates a shallow local copy of the object in the function scope. Things you modify here won't be reflected in the object passed to it. For example, Declarationvoid fun(X x);CallingX x; fun(x);Pass by ReferenceThis passes a reference to the object to the function. Things you modify here will be reflected in the object passed to it. No copy of the object is created. For example, Declarationvoid fun(X &x);CallingX x; fun(x);Pass by const ... Read More

How to detect integer overflow in C/C++?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

265 Views

The only safe way is to check for overflow before it occurs. There are some hacky ways of checking for integer overflow though. So if you're aiming for detecting overflow in unsigned int addition, you can check if the result is actually lesser than either values added. So for example, Example Codeunsigned int x, y; unsigned int value = x + y; bool overflow = value < x; // Alternatively "value < y" should also workThis is because if x and y are both unsigned ints, if added and they overflow, their values can't be greater than either of them ... Read More

C++ Program to Find the Longest Increasing Subsequence of a Given Sequence

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

596 Views

Longest Increasing Subsequence is a subsequence where one item is greater than its previous item.Here we will try to find Longest Increasing Subsequence length, from a set of integers.Input: A set of integers. {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} Output: The length of longest increasing subsequence. Here it is 6. The subsequence is 0, 2, 6, 9, 13, 15.AlgorithmlongestSubSeq(subarray, n)Input: The sub array and the size of sub array.Output: Longest increasing sub sequence length.Begin    define array length of size n    initially set 0 to all entries of length ... Read More

C++ Program to Implement Jarvis March to Find the Convex Hull

Nitya Raut
Updated on 30-Jul-2019 22:30:25

897 Views

Jarvis March algorithm is used to detect the corner points of a convex hull from a given set of data points.Starting from left most point of the data set, we keep the points in the convex hull by anti-clockwise rotation. From a current point, we can choose the next point by checking the orientations of those points from current point. When the angle is largest, the point is chosen. After completing all points, when the next point is the start point, stop the algorithm.Input: Set of points: {(-7, 8), (-4, 6), (2, 6), (6, 4), (8, 6), (7, -2), (4, ... Read More

C++ Program to Implement Graham Scan Algorithm to Find the Convex Hull

Nitya Raut
Updated on 30-Jul-2019 22:30:25

3K+ Views

Convex hull is the minimum closed area which can cover all given data points.Graham's Scan algorithm will find the corner points of the convex hull. In this algorithm, at first the lowest point is chosen. That point is the starting point of the convex hull. Remaining n-1 vertices are sorted based on the anti-clock wise direction from the start point. If two or more points are forming same angle, then remove all points of same angle except the farthest point from start.From the remaining points, push them into the stack. And remove items from stack one by one, when orientation ... Read More

Advertisements