Found 7197 Articles for C++

Difference Between Inline and Macro in C++

AmitDiwan
Updated on 24-Mar-2021 13:24:36

1K+ Views

In this post, we will understand the difference between inline and macro in C++.InlineIt is a function in C++.It is parsed by the compiler.It can be defined inside or outside the class.It evaluates the argument only once.The compiler may not convert all functions to ‘inline’ function and expand them all.The short functions that are defined inside the class are automatically made as inline functions.An inline function inside a class can access the data members of the class.Inline function can be terminated using curly brackets.It is easy to debug.This is because error checking is done during compilation.It binds all statements in ... Read More

Difference Between Copy Constructor and Assignment Operator in C++

Ravi Ranjan
Updated on 20-May-2025 13:31:25

11K+ Views

A copy constructor is a type of constructor that uses another object from the same class that has been created previously, to initialize an object, whereas the assignment operator is used to assign a value to a variable. In this article, we will understand the difference between the copy constructor and the assignment operator in C++. Copy Constructor A copy constructor creates a new object by copying an existing object of the same class which has been created previously. It is of two types, i.e., Default and User-defined copy constructor. In the default copy constructor, ... Read More

Difference Between Syntax and Semantics

Kiran Kumar Panigrahi
Updated on 22-Feb-2023 14:30:33

20K+ Views

Syntax defines the rules and regulations that help write any statement in a programming language, while semantics refers to the meaning of the associated line of code in the programming language. Read this article to learn more about syntax and semantics and how they are different from each other. What is Syntax? In a programming language, Syntax defines the rules that govern the structure and arrangement of keywords, symbols, and other elements. Syntax doesn't have any relationship with the meaning of the statement; it is only associated with the grammar and structure of the programming language. A line of ... Read More

Difference Between Private and Protected in C++

Nishu Kumari
Updated on 03-Mar-2025 13:16:34

1K+ Views

In this article, we'll explain the difference between private and protected access specifiers in C++. Access specifiers in C++ control who can access the variables and functions inside a class. Two commonly used specifiers are private and protected. Private members are accessible only within the class, while protected members can be accessed by the class and its derived classes. We'll show how each specifier works in C++, especially with inheritance, using simple examples to make it easy to understand. Private Access Modifier When we declare a class member as private, it means that this member is only accessible within ... Read More

Find speed of man from speed of stream and ratio of time with up and down streams in C++

sudhir sharma
Updated on 16-Mar-2021 06:41:00

150 Views

In this problem, we are given two values S and N denoting the speed of stream in Km/h and ratio of time with up and down streams. Our task is to Find speed of man from the speed of stream and ratio of time with up and down streams.Let’s take an example to understand the problem, InputS = 5, N = 2Output15Solution ApproachA simple solution to the problem is by using the mathematical formula for the rowing problems. So, let’s see how the formula will work −speed of man = x km/h speed of stream = S km/h speed of ... Read More

Find smallest permutation of given number in C++

sudhir sharma
Updated on 16-Mar-2021 06:38:31

833 Views

In this problem, we are given a large number N. Our task is to find the smallest permutation of a given number.Let’s take an example to understand the problem, InputN = 4529016Output1024569Solution ApproachA simple solution to the problem is by storing the long integer value to a string. Then we will sort the string which is our result. But if there are any leading zeros, we will shift them after the first non zero value.Program to illustrate the working of our solution, Example Live Demo#include using namespace std; string smallestNumPer(string s) {    int len = s.length();    sort(s.begin(), s.end()); ... Read More

Find smallest number with given number of digits and sum of digits in C++

sudhir sharma
Updated on 16-Mar-2021 06:37:29

1K+ Views

In this problem, we are given two values that are the sum (denoting the sum of digits) and digit (denoting the number of digits). Our task is to find the smallest number with a given number of digits and sum of digits.Let’s take an example to understand the problem, Inputsum = 15, dgiti = 2Output69ExplanationAll 2 digits numbers with sum 15 are : 69, 78, 87, 96.Solution ApproachA simple solution to the problem is by considering all the numbers with digit count as digit and find the smallest number whose sum of digit is equal to the sum.An efficient solution ... Read More

Find smallest and largest elements in singly linked list in C++

sudhir sharma
Updated on 16-Mar-2021 06:35:28

888 Views

In this problem, we are given a singly linked list. Our task is to find the smallest and largest elements in single linked list.Let’s take an example to understand the problem, Inputlinked List : 5 -> 2 -> 7 -> 3 ->9 -> 1 -> 4OutputSmallest element = 1 Largest element = 9Solution ApproachA simple solution to the problem is using by traversing the linked list node by node. Prior to this, we will initialise maxElement and minElement to the value of the first element i.e. head -> data. Then we will traverse the linked list element by element. And ... Read More

Find smallest and largest element from square matrix diagonals in C++

sudhir sharma
Updated on 16-Mar-2021 06:33:02

1K+ Views

In this problem, we are given a square matrix of size nXn. Our task is to Find smallest and largest element from square matrix diagonals. We need to find the smallest and largest elements of the primary and secondary diagonals of the matrox.Let’s take an example to understand the problem, Inputmat[][] = {    {3, 4, 7},    {5, 2, 1},    {1, 8, 6} }OutputSmallest element in Primary Diagonal = 2 Largest element in Primary Diagonal = 6 Smallest element in Secondary Diagonal = 1 Largest element in Secondary Diagonal = 7Solution ApproachA simple solution to solve the problem ... Read More

Find single Movement in a Matrix in C++

sudhir sharma
Updated on 16-Mar-2021 06:30:24

138 Views

In this problem, we are given four values x1, y1, x2, y2 denoting two points (x1, y1) and (x2, y2). Our task is to find single movement in a Matrix. We need to find the direction using which we can move from one point (x1, y1) to (x2, y2). There can be any number of moves by the direction needed to be single and we need to return the direction in the form - “left” , “right”, “up”, “down”. Otherwise return -1, denoting “not possible”.Let’s take an example to understand the problem, Inputx1 = 2, y1 = 1, x2 = ... Read More

Advertisements