answersLogoWhite

0

📱

C++ Programming

Questions related to the C++ Computer Programming Language. This ranges all the way from K&R C to the most recent ANSI incarnations of C++, including advanced topics such as Object Oriented Design and Programming, Standard Template Library, and Exceptions. C++ has become one of the most popular languages today, and has been used to write all sort of things for nearly all of the modern operating systems and applications." It it a good compromise between speed, advanced power, and complexity.

2,546 Questions

How does C plus plus program write in Visual studio?


You begin by writing the source code. Any errors at this stage (syntax time) must be fixed before proceeding further. The IDE (integrated development environment) will usually highlight syntax errors as they occur.

When the source code is ready it can be compiled. Any errors at this stage (compile time) must be fixed before compilation can complete. Again, the IDE should highlight these errors.

Successful compilation creates individual object files which can then be processed by the linker. Any errors at this stage (link time) must be fixed before an executable is created. Once again, the IDE will assist you.

Once the executable is created it can be run. Any errors at this stage (run time) must be fixed before code is finally ready to ship. The IDE won't help you unless you make a debug version of your executable, which can then be debugged in your IDE to locate logic errors, unhandled exceptions, etc. When the debug version works as expected, you can build a release version. After a final check for run time errors, the program is complete. Don't delay. Ship it!

Although there are many stages to creating an executable, the IDE can reduce the workload by creating a MakeFile for your project, which automatically builds the application for you (both debug and release versions) only stopping when an error is encountered.

If your source code includes conditional compilation for a variety of platforms and architectures, the source must be compiled separately for each using the intended platform for each.


What is different is pop and oop in c plus plus?

pop the work is done with the help of functions and more previlage is given to function and not the data, also you dont have data hiding,operator overloading, function overloading, access specifiers and inheritance features in pop and when your business logic expanded pop was not able to meet the required demand and because of this oops came inot action where in oops data used to get more importance,data hiding is possible with the help of access specifiers. Each object controls it's own data, Inheritance is possible and many more features are available

Example of pop is :c,fortran

Example of oops is: java,C#

Write a c plus plus program to find the area of circle?

#include<stdio.h>

void main()

{

int r;

int pi=3.14;

float cir,area;

printf("/n enter the radius of the circle");

scanf("%d",&r);

cir=2*pi*r;

area=pi*r*r;

printf("the circumference of the circle is%d",cir);

printf('the area of the circle is %d",area);

}

Algorithm to insert and delete an element from a circular queue?

The Method To Add an element in Circular Queue

# define MAXQUEUE 100 struct queue{

int items[MAXQUEUE];

int front, rear;

}

struct queue q;

q.front=q.rear=MAXQUEUE -1;

void ENQ(struct queue *pq, int x)

{

/* make room for new element*/

if(pq ->rear = MAXQUEUE - 1)

pq-> rear = 0;

else

(pq->rear)++;

/* check for overflow */

if(pq ->rear==pq->front)

{

printf("queue overflow);

exit(1);

}

pq->items[pq->rear]=x;

return;

}/* end of ENQ*/

A Method to Delete an element from Circular Queue

int DQ(struct queue *pq)

{

if(pq-> rear == pq-> front)

{

printf("queue underflow");

exit(1);

}/*end if*/

if(pq->front = = MAXQUEUE-1)

pq->front=0;

else

(pq->front)++;

return(pq->items[pq->front]);

What is the use of header file in c?

A header file is used to define constants, variables, macro's and functions that may be common to several applications. When a system consists of multiple applications that all access the same data it becomes essential that each application uses identical definitions and it is safer for all of those applications to use the same methods to read that data. updating data should only be performed by a single function, from a single application, but reading data can be safely performed by using the common defintions found in header files. you use header files in the programming language C to declare struct's functions and other things that could be usefull for your program.. it makes thing's simpler and easy to use...

What are the difference between array declaration in java and c plus plus?

There is no difference, other than that declarations in C++ can also initialise the variable in a single instruction.

C example:

int x; // declaration

x=5; // initialisation

C++ example:

int y=5; // declaration and initialisation combined.

Is Java or C plus plus faster?

Java is considerably more convenient than either C or C++ due to its extremely high level of abstraction. However, that convenience comes at the cost of both performance and efficiency.

How an object of a class that contain object of other class created in C plus plus?

An object is a thing that occupies memory, has a value, and has methods that manipulate it. As such, even elementary variables are objects.

Most, however, consider that an object in C++ is an instance of a class type.

// declaration and definition

class myClass {

... attributes

... methods

... constructors

... destructor

};

// instantiation by direct allocation

myClass myClassObject;

// instantiation by heap allocation

myClass *myClassObjectPtr = new myClass;

How do you write a program in c plus plus to swap each half of an array?

Swapping array elements is simple enough. The only issue is what to do with the middle element when there are an odd number of elements. The following program simply leaves it where it is.

#include<iostream>

#include<vector>

template<typename T>

void exchange (std::vector<T>& v)

{

if (v.size()<2) return;

size_t left = 0;

size_t right = v.size() / 2;

if (v.size()%2)

++right; // skip middle element

while (right < v.size())

std::swap (v[left++], v[right++]);

}

int main()

{

std::vector<unsigned> v {4, 8, 15, 16, 23, 42, 69};

for (auto n : v) std::cout << n << ' '; std::cout << std::endl;

exchange (v);

for (auto n : v) std::cout << n << ' '; std::cout << std::endl;

}

How t print a table in tabular form in two dimensional array in c plus plus?

#include<iostream>

#include<iomanip>

#include<vector>

#include<random>

#include<ctime>

using data_t = std::vector<std::vector<int>>;

size_t get_width (int num)

{

size_t width=1;

while (num/=10)

++width;

return width;

}

std::vector<size_t> get_column_widths (const data_t& data)

{

std::vector<size_t> width;

for (auto row : data)

{

if (width.size() < row.size())

width.resize (row.size());

for (size_t i=0; i<row.size(); ++i)

{

size_t w = get_width (row[i]);

if (width[i]<w)

width[i]=w;

}

}

return width;

}

void print_table (const data_t& data)

{

using std::cout;

using std::endl;

using std::right;

using std::setw;

std::vector<size_t> width = get_column_widths (data);

cout << right;

for (auto row : data)

{

for (size_t i=0; i<row.size(); ++i)

{

cout << setw(width[i]) << row[i] << ' ';

}

cout << endl;

}

}

int main()

{

// Random number generator (range: [1:1000])

std::default_random_engine generator ((unsigned) time (0));

std::uniform_int_distribution<unsigned> distribution (1, 10000);

// Create a 10x5 matrix:

data_t data;

for (size_t row=0; row<10; ++row)

{

data.push_back (std::vector<int>{});

for (size_t col=0; col<5; ++col)

data[row].push_back (distribution (generator));

}

print_table (data);

}

How do you write c plus plus program to insert an element into a binary search tree?

Binary Search is an algorithm that finds an element by successively halving the search space. Typically, pointers are used, one for the beginning of an array, one for the end. Then a midpoint pointer is chosen, and a test is performed. You either find the element, or you discover that the target element is before or after the midpoint. You then adjust either the start pointer or the end pointer and you iterate. When you reach the point where the pointers are out of order, you conclude that the target is not found, and you also know where to insert it. Binary Search is best implemented with an ordered array, because you want to make "random" access to each element. The problem with arrays is that they are typically fixed size, and must be dynamically adjusted when they need to grow, a potentially "expensive" operation. You can also implement a Binary Tree, but there is cost in development and processing. Even trees have issues because, when inserting and deleting elements, you must use a rebalancing algorithm, otherwise the tree might degrade to a linked list, which is not efficient when used as a search space. In C++, it is possible to declare and define a class of elements that you can add to, subtract from, and search. If you do this correctly, you could start with a static or dynamic array, and then upgrade if need be to a binary tree, and then upgrade if need be to a balanced binary tree, all the while without requiring change to the public interface. That is perhaps the most important value of an Object Oriented Language such as C++.

What is a label in c plus plus?

A label is simply a user-defined name that appears on its own line and is followed by a colon. Labels are used with goto statements. Labels are also used with switch statements, where each case label is an unique label that represents the result of the expression evaluated by the switch statement. Labels are ignored by the code that immediately precedes them, they are only used to mark the address of the code that immediately follows.

What is Matrix addition in c plus plus?

Matrix addition is simply the process of summing one array to another array of the same size. The result is another array of the same size where each element is the sum of the corresponding elements in the first two arrays, like so:

{1, 2, 3} + {4, 5, 6} = {1+4, 2+5, 3+6} = {5, 7, 9}

This can be extended to multi-dimensional matrices (an array of arrays):

{1, 2, 3}

{4, 5, 6}

+

{7, 8, 9}

{10, 11, 12}

=

{7+1, 8+2, 9+3}

{10+4, 11+5, 12+6}

=

{8, 10, 12}

{14, 16, 18}

To achieve this in C++, C-style static arrays are the easiest method:

int main()

{

int a[2][3] = {{1, 2, 3}, {4, 5, 6}};

int b[2][3] = {{7, 8, 9}, {10, 11, 12}};

int c[2][3] = {{0, 0, 0}, {0, 0, 0}};

for (size_t row=0; row<2; ++row)

for (size_t col=0; col<3; ++col)

c[row][col] = a[row][col] + b[row][col];

}

C-style dynamic arrays as well as std::vector and std::array can also be used to represent the matrices, but it is important that all dimensions are the same. Matrices of different sizes cannot be added together (the result is undefined).

When do we make a virtual function pure?

We make a virtual function pure whenever we wish to make our class an abstract base class (an abstract data type). Unlike a virtual function, pure virtual functions must be overridden by a derived class or by one of its derivatives (the function remains pure virtual until it is overridden, at which point it becomes virtual). Derived classes that do not provide a complete implementation for all the pure virtual functions it inherits become abstract themselves. You cannot instantiate an abstract base class other than through derivation.

To find the largest element of an array in c plus plus?

int GetMaxElement( void * array)

{

if (array != 0)

{

return(max(array[], typeof(array)));

}

return(0);

}

Is c or c a procedural oriented language?

One definition of a "procedural programming language" is a language that is used to describe how a program should accomplish the task it is to perform. This is in opposition to a "declarative programming language" that is used to describe what the program should accomplish rather than how it accomplishes the task.

What is the C plus plus program for the multiplication of two matrices?

class complex {

private:

double real;

double imaginary;

public:

complex() {...} // constructor, etc.

operator+(const& complex a) { // operator plus

this->real += a.real;

this->imaginary += a.imaginary;

}

}

How does an inline function differ from a preprocessor macro in c?

"Differences between inline functions and non"Non-inline functions are your standard functions. When they're called, the compiler puts all the variables and its current position on the stack, jumps to the location in memory where the function resides, executes the instructions, and jumps back to where it was originally called. All that work takes a lot more time then if you had just not used functions. By writing the code directly into your procedure and not using functions you are inlining your code. If you have that same piece of code in multiple places, maintaining your code will become a problem because your code will be duplicated everywhere (because you're not using functions in order to achieve a speed increase).

In order to get the best of both worlds, C++ introduced the inline keyword. By specifying that a function is inline, the compiler will take the function you have written and basically replace the function call that would have been generated with the function itself. In other words, using an inline function is pretty much directly putting your code there, except it's prettier and more maintainable.

Overuse of inlining function will cause bloated code for a very marginal increase in speed, if any. Inline functions are best suited for small quick functions. Also note that the inline keyword is a request to inline a function and the compiler may not necessarily honor that request. One example would be attempting to inline a recursive function.

In other languages like Java and C#, you are not able to specify what is inlined and what is not. The compiler will automatically make that decision without any input from the programmer.

Who is the owner of c plus plus language?

Bjarne Stroustrup is the author of C++. However, no one "owns" this language.

How do you write a c plus plus program to calculate the sum of squares using a function?

/**recursive function to find square root of a double to a precision of

maxDepth = 10 decimal places

returns -1.0 when given a negative number*/

#define maxDepth 10

/*firstly find the rational part*/

double getSqrt(double numIn){

/*cant take the square root of a negitive,

and a square root should not be negative

so return -1*/

int candidate = 0;

if (numIn <0)

return -1.0;

/*try every integer until you get one whose square is higher than

the number required, and this clearly one more than the

integer part of your square root*/

do{

if (candidate *candidate *1.0 numIn)

return testVal;

if (testVal * testVal >numIn)

/*most square roots are irrational, and theirfore a maximum number of recursions

must be set, otherwise infinite recursion will occur*/

if (myDepth <maxDepth)

return getIrrational(numIn, testVal-1/10^myDepth, myDepth +1);

else

return testVal-1/10^myDepth;

}

//this can probably be improved on in terms of conciseness, but the logic is the only //way to find a sqrt without going into calculous

How do you write a C plus plus program to find the first 5 multiples of a number?

#include<iostream>

#include<vector>

std::vector<size_t> multiples(size_t const num, const size_t terms)

{

std::vector<size_t> result;

size_t term=0;

while (++term<=terms)

result.push_back (num*term);

return result;

}

int main()

{

const std::vector<size_t> mults = multiples (42, 5);

std::cout << "The first 5 multiples of 42 are:";

for (auto value : mults) std::cout << '\t' << value;

std::cout << std::endl;

}

How many keywords in c plus plus and what are they?

The const keyword is the antonym of variable. While a variable is a volatile identifier and can change its value at any time, a constant identifier is non-volatile and must be assigned a value at the point of instantiation. Whenever you declare a constant you are allowing the compiler to assure you that the value will never change, whether purposely or by programming error. By allowing the compiler to assure constance, you do not need to manually check if the value has been altered. As with many other things in C++, the more responsibility you hand to the compiler, the more robust your programs will be.

What is the use of INLINE function?

it is the function declared inside the class. It is used for gaining faster speed in execution but it might give back the larger executable file
An inline function is one that is not called, its code is inserted in the position where you make a call to it. If it is called multiple times from different locations it was be inserted in all locations, increasing the size of all functions its called from but marginally increasing speed.

Take this example:

inline int AddNumbers(int a,int b)

{

return a+b;

}

int main()

{

int x = 2;

int y = 3;

int answer;

answer = AddNumbers(x,y);

answer = AddNumbers(answer,y);

return 0;

}

When the program is compiled it would look more like this:

int main()

{

int x = 2;

int y = 3;

int answer = x+y;

answer += y;

return 0;

}

Write a c plus plus program of tower of hanoi?

#include<stdio.h>

#include<conio.h>

void main()

{

int *d,*s,i,n,dir;

clrscr();

printf("Enter the number of disk\n");

scanf("%d",&n);

dir=n&1;

for(i=0;i<=n+1;i++)

{

d[i]=1;s[i]=i+1;

}

for(;;)

{

i=s[0];

if(i>n)

break;

printf("Move disk %d from tower%d to tower%d\n",i,d[i]=(d[i]+(i&1?dir:1-dir))%3+1,d[i]);

s[0]=1;

s[i-1]=s[i];

s[i]=i+1;

}

getch();

}

Advantages of new operator over malloc function in c?

1: new operator automatically computes the size of the data object. You need not use the operator sizeof.

2: new operator automatically returns the correct pointer type, so that there is no need to use a type cast.

3: it is possible to initialize the object while creating the memory space.

4: Like any other operator, new and delete can be overloaded.