Found 34484 Articles for Programming

Copy constructor vs assignment operator in C++

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space. It uses the reference variable to point to the previous memory block.Copy Constructor (Syntax)classname (const classname &obj) { // body of constructor }Assignment Operator (Syntax)classname Ob1, Ob2; Ob2 = Ob1;Let us see the detailed differences between Copy constructor and Assignment Operator.Copy ConstructorAssignment OperatorThe Copy constructor is basically an overloaded constructorAssignment operator is ... Read More

Type Casting operators in C++

George John
Updated on 30-Jul-2019 22:30:25

395 Views

A cast is a special operator that forces one data type to be converted into another. As an operator, a cast is unary and has the same precedence as any other unary operator.The most general cast supported by most of the C++ compilers is as follows(type) expressionWhere type is the desired data type. There are other casting operators supported by C++, they are listed below -? const_cast (expr) The const_cast operator is used to explicitly override const and/or volatile in a cast. The target type must be the same as the source type except for the alteration of its const ... Read More

Difference between "new operator" and "operator new" in C++?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

898 Views

In C++ when we want to create a new object, we have to create a memory block into the memory, then also call constructor to initialize the memory block. We can create memory element by using the new keyword. This new operator is doing two consecutive task. But the operator new just only create memory space.New KeywordThe new operator is a special type of operator, which denotes a request for memory allocation on the heap section. When sufficient memory is available, then only new operators initializes the memory to the pointer variable. When we create an object using a normal ... Read More

What is an arrow operator, `->` in C++?

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

358 Views

The dot and arrow operator are both used in C++ to access the members of a class or structure. They are just used in different scenarios. In C++, types declared as class, struct, or union are considered "of class type". So the following refers to all three of them.a.b is only used if b is a member of the object (or reference to an object) a. So for a.b, a will always be an actual object (or a reference to an object) of a class.a->b is essentially a shorthand notation for (*a).b, i.e., if a is a pointer to an ... Read More

What's the difference between assignment operator and copy constructor in C++?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

568 Views

The Copy constructor and the assignment operators are used to initialize one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space. It uses reference variable to point to the previous memory block.Copy Constructor (Syntax)classname (const classname &obj) { // body of constructor }Assignment Operator (Syntax)classname Ob1, Ob2; Ob2 = Ob1;Let us see the detailed differences between Copy constructor and Assignment Operator.Copy ConstructorAssignment OperatorThe Copy constructor is basically an overloaded constructorAssignment operator is basically ... Read More

Pre & post increment operator behavior in C, C++, Java, and C#

George John
Updated on 30-Jul-2019 22:30:25

3K+ Views

The Pre increment and post increment both operators are used as increment operations. The pre increment operator is used to increment the value of some variable before using it in an expression. In the pre increment the value is incremented at first, then used inside the expression.if the expression is a = ++b; and b is holding 5 at first, then a will hold 6. Because increase b by 1, then set the value of a with it.Example Code#include using namespace std; main () { int a, b = 15; a = ++b; cout

How do I use the conditional operator in C/C++?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

214 Views

This conditional operator is also known as the Ternary Operator. This operator has three phase.Exp1 ? Exp2 : Exp3;where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.The ? is called a ternary operator because it requires three operands and can be used to replace if-else statements, which ... Read More

C++ Program to Find Transpose of a Graph Matrix

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

251 Views

In this program we take a matrix and prints the transpose of the matrix. In a transpose matrix, rows become columns and vice versa.AlgorithmBegin Take number of rows and columns of the matrix. Take The elements of the matrix and stored in the matrix ‘A’. The transpose matrix is found by exchanging the rows with columns and columns with rows. Print both the original matrix and the transpose. End.Example Code#include using namespace std; int main () {    int A[10][10], a, b, i, j;    cout > a>> b;    cout > A[i][j];       cout

C++ Program to Represent Graph Using 2D Arrays

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

1K+ Views

This is a C++ program represents a graph using 2D arrays.The time complexity of this algorithm is O(v*v).AlgorithmBegin Take the input of the number of vertex ‘v’ and edges ‘e’. Assign memory to the graph[][] matrix. Take the input of ‘e’ pairs of vertexes of the given graph in graph[][]. For each pair of connected vertex(v1, v2), store 1 in the graph[][] at the index (v1, v2) and (v2, v1). Print the matrix using PrintMatrix(). EndExample Code#include #include using namespace std; void PrintMatrix(int **matrix, int n) {    int i, j;       cout

C++ Program to Represent Graph Using Incidence List

George John
Updated on 30-Jul-2019 22:30:25

222 Views

This program represents a graph using incidence list and the time complexity of this algorithm is O(e).AlgorithmBegin Take the input of the number of vertex ‘v’ and edges ‘e’ and also take the input of ‘e’ pairs of vertexes of the given graph in e[][]. For each edge print the corresponding vertex involved in that connection. EndExample Code#include using namespace std; int main() { int i, v, e, j, c; coutv; coute; int edge[e][2]; for(i = 0; i < e; i++) { cout

Advertisements