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

Where is C used?

C is ideally suited to low-level programming, but has been used to write operating system kernels, device drivers, subsystems programming and applications software of all types. C++ now dominates across all these domains as it is capable of producing far more efficient machine code much more easily. However, even C++ programmers will make use of C-style programming in combination with its object-oriented and generic programming paradigms.

Why is dosh is used in c plus plus?

The header dos.h is not part of C++ -- it is a third party header and is non-generic (platform and/or implementation dependent). In Visual C++, for instance, dos.h provides "definitions for MS-DOS interface routines". Since MS-DOS has been completely replaced by Windows, dos.h exists purely for historical reasons such as compatibility with older 16-bit C programs. These days, if you're targeting Microsoft operating systems, you will generally include windows.h instead.

Print your name ten times on the screen using a do-while loop?

#include<stdio.h> #include<conio.h> void main() { clrscr(); int x=0; do { printf("Kaushal"); printf("\n"); x++; }while(x<10); getch(); }

Write a C program to find the sum of all prime numbers in an array?

Borrowing the isPrime function from another answer of mine. Note that this will result in terrible performance for large arrays of numbers. (You should look into one of the sieve algorithms for this)

// returns 1 if n is prime, 0 otherwise

void isPrime(const int n) {

// We know that if n is composite, then we will find a factor in the range [2,sqrt(n)]

// so we compute the square root only once to limit our number of calculations.

const int sqrt_n = sqrt(n);

// Iterate through possible factors

int i;

for( i = 2; i <= sqrt_n; ++i ) {

// If n is divisible by i (n%i==0) then we have a factor

if(!(n % i)) {

return 0;

}

}

// If we get here, we know n has no factors other than itself and 1

return 1;

}

// returns the sum of all prime numbers in nums

int findPrimeSum(const int numsLength, const int[] nums) {

int sum = 0;

// iterate through nums and add up all the primes

int i;

for(i = 0; i < numsLength; ++i) {

if( isPrime(nums[i] ) {

sum += nums[i];

}

}

return sum;

}

How do you display the square root symbol on the output screen using Borland C Plus Plus?

Its Unicode value is 221A according to System tool Character map Advanced view Unicode subrange Math operators.

But I haven't done C in awhile, so I don't know how to or if you can.

ASCII value of root symbol is 251. In C we can print this symbol by printing the character value as below

printf("%c",251);

this will print the root symbol

How do you delete dev cpp source files when it says you don't have permission?

This has nothing to do with devC++, it has to do with Windows file permissions. You clearly don't have the access level required to delete these files so you'd be best advised to leave them well alone or ask your supervisor to delete them for you.

How do you control hardware in C?

Platform-dependent. If you have an OS on your computer, then most likely you cannot access hardware directly from userland programs.

What you meant by function call function definition and the function declaration in c and c plus plus?

A function call is a function that is called from within another function.

Functions must be declared before they can be called, in order to establish the function's prototype (signature) so the compiler knows which function to call.

The function definition contains the implementation of the function and may be specified in the declaration itself (inline), or it can be defined in another file altogether.

Declarations are typically placed in a header file so that they may be included (#include) in any files that require them, including the file containing the definition of the function declared in the header file. Including a file is the same as typing the file contents in full at the point of inclusion, thus ensure the declarations are available to the compiler before they are used.

The following example shows a forward declaration of a function that is defined later.

// forward declaration.

int square(int);

// the main function (the entry point of application).

int main()

{

// a function call.

int s = square( 5 );

// returns the value 25 to the calling process (e.g., the operating system).

return( s ); }

// definition of the function declared earlier.

int square(int data)

{

// returns the square of the data to the calling function, e.g., main().

return( data * data );

}

Function is a self contained block or a sub program of one or more statements that performs a special task when called.

What is overriding in c?

Overriding relates to derived classes, where the derived class provides a new implementation for a method declared in the base class. The override is said to be a more-specialised implementation of the base class method, which is itself described as being a generic method. However, the derived class method can still call the base class method, if required.

When the designer of a class can predict that their class will be derived from, they will normally provide virtual methods. These methods are expected to be overridden by the derived class. Overriding a non-virtual method can have side effects if the method is also overloaded. Overriding just one overloaded method will effectively hide all the other overloads in the base class, which may be undesirable.

C plus plus program to find prime number?

Here's an example. I've included analysis.

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main(void)

{

/* User inputs high and low ends of range to search.

/ This could easily be done as function inputs, if

/ if so desired. */

int testprime,testdivisor, minprime, maxprime;

printf("Search for primes between:\nLow end: ");

scanf("%d",minprime);

printf("Top end: ");

scanf("%d",maxprime);

int isprime; // 0 indicates a number is not prime..

for(testprime=minprime;testprime<=maxprime;testprime++)

{

isprime = 1;

for(testdivisor=2;testdivisor<sqrt(testprime);testdivisor++) // Primes divide by 1!

{

isprime = testprime%testdivisor; // % finds remainders, so 7%3 returns 1.

// If 0 is returned, a divisor has been found.

if(isprime == 0) break; // Hence, not prime. "break;" exits the loop.

}

if(isprime != 0) printf("%d is prime.\n",testprime);

}

system("pause");

return 0;

}

What is the difference between Typedef and Reference in C plus plus?

A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.

Both Java and C plus plus compile source code to object code yet Java is called an interpreted language while C plus plus is called a compiled language explain this notion?

It can easily be explained by the fact that Java does not compile to object code. Java compiles to byte code that is suitable for interpretation by the Java virtual machine. C++ compiles to native machine code and therefore does not require interpretation of any kind. As a result, C++ programs run somewhat quicker than equivalent Java programs. However, because Java is interpreted, it is highly portable. Any machine with a Java virtual machine implementation (which is pretty much every device today) can run Java programs built from a single compilation. C++ requires that the code be written specifically for each platform and that the source be compiled separately upon each supported platform.

What is object slicing in c plus plus?

Object slicing typically occurs when operating upon a base class reference, even when the reference is actually an instance of a derived class. You might expect polymorphic behaviour but it doesn't happen, due to object slicing. Most of the time this is only to be expected (such as when passing a derived object to a base class copy constructor or assignment operator), but when you actually expect polymorphic behaviour it can catch you out. The following example of partial assignment demonstrates this:

#include

struct A {

int a_var;

A(int a): a_var(a) {}

void debug() { std::cout<<"a_var="<

A& operator=(const A& rhs){ a_var=rhs.a_var; std::cout<<"Copying from A"<

};

struct B : public A {

int b_var;

B(int a, int b): A(a), b_var(b) {}

void debug() { A::debug(); std::cout<<" b_var="<

A& operator=(const B& rhs){ A::operator=(rhs); b_var=rhs.b_var; std::cout<<"Copying from B"<

};

int main()

{

B b1(1,2);

B b2(3,4);

std::cout<<"b1 : "; b1.debug(); std::cout<

std::cout<<"b2 : "; b2.debug(); std::cout<

A& ref=b1;

ref=b2;

std::cout<<"b1 : "; b1.debug(); std::cout<

std::cout<<"b2 : "; b2.debug(); std::cout<

}

In the above example, it may seem that b1 and b2 should be equal since we've assigned b2 to a reference to b1. But because the reference is actually to the base class, A, rather than the derived class, B, the object is sliced and only A::a_var is copied; base class A has no storage for B::b_var. The output clearly shows that only the A assignment operator is executed:

b1 : a_var=1 b_var=2

b2 : a_var=3 b_var=4

Copying from A

b1 : a_var=3 b_var=2

b2 : a_var=3 b_var=4

To fix this, we must reference the derived class B instead of the base class A. Thus we must replace the line A& ref=b1; with B& ref=b1;.

The output then becomes:

b1 : a_var=1 b_var=2

b2 : a_var=3 b_var=4

Copying from A

Copying from B

b1 : a_var=3 b_var=4

b2 : a_var=3 b_var=4

In this trivial example the problem is patently obvious but when passing references around functions it may not be entirely clear exactly why the behaviour is not as expected. Remember that if you pass references to base classes you are expected to operate upon the base class only. If you expect polymorphic behaviour, you must use virtual methods to perform the operations instead. The only other option is to invoke runtime type information, which only serves to impede performance and is really a sign of poor design.

What is the use of complementry operator in c?

The bitwise complement or one's complement operator (~) is used to switch the state of all the bits in a value. Thus 1's become 0, and 0's become 1.

One of its many uses is to unset individual bit(s) in a bitmap. We do this with a bitwise AND of the bitmap and the bitwise complement of the bit(s) we want to unset.

Original bitmap: 01011100

Bit to unset: 00000100 (e.g., bit 2 (bits are zero based from right))

// Using one's complement and bitwise AND

~00000100 & 01011100

11111011 (one's complement of bit 2)

&

01011100 (original bitmap)

=

01011000 (original bitmap with bit 2 unset)

Note that this formula works even if bit 2 were already unset:

11111011 (one's complement of bit 2)

&

01011000 (original bitmap, with bit 2 unset)

=

01011000 (original bitmap unchanged)

What can you do with the Visual C plus plus App-Wizard?

The App-Wizard is the Application Wizard. You use it to create a framework for your application by choosing the type of application and which features you require. The wizard generates the source files and headers for you according to your choices, you simply need to flesh it out with your specific implementation.

When does c plus plus use generic function implicitly?

C++ uses the generic function implicitly whenever the base class implementation (the generic method) is also the most-derived implementation.

Html vs traditional programming language?

HTML is not a programming language so it is very different to programming languages. HTML is really just for formatting text and laying out pages, what we call marking up a page. So it is a Markup Language. It can't really do anything interactive with you. Web pages that can do things normally have programming code built into them, with languages like Javascript. HTML can't even do simple things like calculations. Calculations are fundamental to programming languages, as are many other things like: making decisions, repeating instructions, storing data, processing data, and many other things. HTML can't do any of those things. HTML borrows some things from programming, like the facility to use comments, encouraging people to lay out their code properly and the use of simple English-like commands.

If people have learned how to use HTML, it is still a big step up to grasp the concepts of programming. There are also very many programming languages, many of them specialising in doing certain kinds of jobs. Many are very complicated and technical and can be quite cryptic, making them hard to learn. They can also be very strict in how you have to do things, and even simple errors can stop your program working. Programming can be very frustrating because of that. You need to learn and understand a lot more things to write programs than you need for creating HTML pages. HTML is a lot less complicated. You can even make some basic mistakes and your page will still work. HTML can be learned very simply and quickly. It is very easy to show someone how to create a simple web page with HTML. So there is a very big difference between HTML and programming languages.

How do I round up to the next largest whole number in C plus plus?

It's either floor(x)+1 or ceil(x) depending on what you want to get for integral x numbers:x or x+1

Which binding in C plus plus is preferred and why?

There is no preference as such. The type of binding you use is more dependant upon the design and circumstance rather than any preference you may have. Static binding is certainly more predictable and therefore easier to program, but dynamic binding offers much greater flexibility.

How do you write a linked list insertion and deletion implementation in c plus plus?

There is no single container that is ideally suited to every task, therefore the container you choose is often a compromise, depending upon which operations you need to perform the most.

Linked lists are ideally suited to applications where a lot of insertions need to occur within the middle of the list. The downside is that you must first traverse the list from the first or last element in order to reach the insertion point; there is no constant time random access. Thus the average search time is O(n/4) for a list of n elements. For a forward list, search times increase to O(n/2).

Traversing an array has a similar complexity, but because array elements occupy contiguous memory, they are actually quicker to traverse. And if the array is also sorted, traversal becomes O(n log n) by taking advantage of binary search traversal (recursively starting at the middle element and discounting one half of the array on each recursion). However, arrays are suited to insertions at the end of the array only. This is achieved by reserving more memory than is actually needed, but when that reserve runs out, the entire array has to be reallocated, which can result in every element being copied to new memory.

Lists are not ideally suited to sorting algorithms other than those solely reliant upon sequential access (such as merge sort). However, it can still be quicker to copy the list to an array, sort the array, and then reconstruct the list from the sorted array.

Ultimately no-one can tell you which is the best container to use in any given situation. Never assume that just because a container is ideally suited to a specific task that it is ideal for all the tasks you actually need to perform. Always measure the performance and try other containers to see which works best overall.

As a rule of thumb, consider the vector as your default option. Measure the performance using high-resolution timers, then try an alternative container.

How will you overload a template function?

The only reason to overload a template function is to provide an overload that differs in the number and type of arguments that cannot be addressed by the template function alone. You simply declare them as you would any other overload. The overloads may themselves be template functions, but there must be no ambiguity: every template function must generate an unique function signature. Remember that template functions generate overloads at compile time on an as-required basis.

What is the physical level in c plus plus?

When we write code we generally use and write generic routines that can be executed upon any machine. We are not overly concerned with the hardware we are targetting, because abstraction takes care of the machine specifics for us. Thus we can concentrate on writing code specific to the user, rather than worry about the underlying architecture. However, while C++ allows us to use generic and highly abstract interfaces (the standard library is a prime example), it also allows us to use mid-level C-style code, as well as low-level, inline assembly, should we require it. When we use inline assembly we are working at the physical level and coding specifically to the machine, just as if we were writing the code in pure assembler. At this level there is no abstraction whatsoever, other than that provided by the symbolic language itself, and our code will only work on the specific architecture we are targetting. While this allows us to exploit the hardware to the fullest, there are relatively few cases where this is actually necessary. Both C and C++ were specifically designed to produce the machine-specific code for us, and are both capable of optimising that code in order to exploit the underlying hardware's features (that is one of the roles of the compiler). So while we can use C++ to write machine-specific code when we need it, most of the time we will make use of abstraction and generic code that can be run on any machine, and make use of compiler directives to filter any machine-specific code, and only use inline assembly when it is absolutely necessary to do so.