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

What are the three required sets of statements for every function that a programmer writes in C plus plus?

There is no requirement for any statement in a C++ function, let alone three sets of statements. For instance, the following is a perfectly valid function:

void foo(){}

Clearly this does nothing as it has no statements in the function body, but it is nevertheless a valid function.

Perhaps you mean something else by "statements". The only requirement of a function is that it have a return type, a valid name, an argument list and a function body. The return type may be void, of course, and the argument list may be empty, but it must include the ellipses. The function declaration need not include the function body, and the argument list need only specify the type of argument (the argument names are optional and need not match those declared in the actual definition).

The function name and the arguments define the function signature (the prototype), thus the three required "components" of a function are the return type, the signature and the function body.

What are the advantages of sorting c plus plus?

It's always much quicker (on average) to search a sorted array than it is to search an unsorted array. This is because you can start in the middle of the array. If the value you seek is there then you're done. If it is not there, but the value you seek is less than the value you found, then you immediately know it must be in the left half of the array, otherwise it must be in the right half. In other words you've reduced the number of items to be searched by 50%. If you repeat the process with this smaller subset you'll reduce the search to 25% of the original array, then 12.5%, and so on. Eventually you'll find the value you're looking for. The worst case for any search is when the value does not exist. But you'll know that as soon as the reduced subset has no elements.

We often use Big-O notation to determine how long we expect an operation to take. We know that we can access any element in an array in constant time, which is denoted O(1). We also know that we can compare values in constant time. So we're really only interested in the cumulative time it takes to search the array. For an array of n elements, the worst case involves inspecting every element, which is denoted O(n), which means O(1)*n, or n constant-time operations. For a large array of say, 1,000,000 elements, that's a lot of constant-time operations.

If we imagine an unsorted array, it will always take O(n) time to determine that a value does not exist. But if we sort the array and inspect each element in turn, it will only take O(n/2) time on average because we can stop searching as soon as we find a value that is greater than the one being sought. However, the worst case is still O(n) if the value being sought happens to be greater than any in the array. But if we start in the middle of a sorted array, the average and worst cases both drop to just O(log n).

Note that there will be some overhead in recalculating the subset boundaries, which is itself a constant-time operation, but this is only of concern when dealing with small arrays. It would, in fact, be much quicker to search small arrays one item at a time. For larger arrays, reducing the problem by 50% on each iteration means the overhead quickly becomes irrelevant, but it only works when the array is sorted. Hence new programmers tend to spend an inordinate amount of time studying sorting algorithms to ensure their programs operate as efficiently as possible.

Some algorithms are better than others, but there is no single algorithm that is suitable in every situation. For instance, insert sort is the ideal sorting algorithm for small subsets, but when dealing with a subset of more than a few hundred items it is woefully inadequate. And while quicksort performs extremely well when dealing with large amounts of data, it is unstable (equal items may not be in the same order they were input) and it doesn't work at all when the amount of data is so enormous that it simply will not fit into working memory. For that you need merge sort which uses multiple tape drives or separately controlled disks to sort the data. With slow-to-access media such as this, efficiency is far more important than raw speed alone.

What is the technical name for calling a base class constructor using derived class constructor?

You cannot actually call any constructor, you can only invoke construction, either by instantiating a static instance of a class or by dynamically creating one with the new operator. In the case of base class construction via a derived class, the base class constructor is invoked by the derived class' initialisation list.

Every class constructor has an initialisation list whether you define one or not (the compiler will generate one automatically if you don't). When you derive one class from another, the derived class initialisation list invokes a call to the base class default constructor. The only exception is the compiler-generated copy constructor which automatically calls the base class copy constructor.

If you define your own initialisation list, then you can explicitly invoke any base class constructor overload, thus making your construction code all the more efficient. However, copy constructors should always invoke the base class copy constructor, so if you define a copy constructor, you must explicitly invoke the base class copy constructor -- the compiler will not invoke it implicitly from a user-defined copy constructor.

While many programmer's use the constructor's body to initialise a class, this is highly inefficient. Even if you don't specify an initialisation list, one is created for you, resulting in every base class and every member variable being initialised twice, which can quickly add up to a substantial cost in performance.

The constructor's body should only really be used for initialisation when it would be difficult or impossible to do so from the initialisation list. Remember that your object doesn't physically exist until initialisation is complete, so you may not have access to some members, particularly base class members, at certain points in the initialisation process.

Initialisation must be done from the ground up, starting with the base classes and ending with the actual class members, and all in the order they were declared. Note that only direct base classes (or virtual base classes) should be invoked from the initialisation list. The base classes themselves should invoke their own base class constructors, if they have any. Thus no matter which derivative you construct, the least-derived class is always constructed first.

Is it possible to use a for loop in place of a while loop?

Yes.

while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below:

for(;condition;)

eg:

for(;i<=n;)

Write a program in C plus plus to store 100 element within array and find the particular element if exist?

#include<iostream>

#include<iomanip>

#include<time.h>

template<typename T>

size_t find(T& data, T a[], size_t size)

{

size_t index=0;

do {

if(a[index]==data)

break;

} while(++index<size);

return(index);

}

template<typename T>

void print(T a[], size_t size)

{

using std::cout;

using std::endl;

using std::setw;

size_t index=0;

do{

if(index&&index%20==0)

cout<<endl;

cout<<setw(3)<<a[index];

}while(++index<size);

cout<<endl;

}

int main()

{

srand((unsigned)time(NULL));

const size_t size=100;

unsigned int a[size];

size_t index=0;

do{

unsigned int data=rand()%100;

do{

data=rand()%100;

} while(find(data,a,index)<index);

a[index]=data;

} while(++index<size);

print(a,size);

}

What is the difference between the structure tag and structure variable?

The structure tag is a type. The structure variable is an instance of that type.

How to Write a c plus plus program to insert a value n after the node in the linked list?

To insert a value after a node in a linked list, first allocate and initialize the node, then add it to the current node...

newnode* = new node (n);

newnode->next = current.next;

if (current != head) current->next = newnode; else head = newnode;

What are the benefits of byte as a datatype?

A byte is the smallest unit of addressable storage. Although a bit is smaller than a byte, a single bit cannot be addressed directly; we always deal with groups of bits and a byte is the smallest group of bits that can be physically addressed. However, once we have addressed a byte, we can then examine the individual bits within it using the bitwise logic operators (AND, OR, NOT and XOR).

On most systems a byte is exactly 8 bits in length. The reason for this is simply that we can represent any 8-bit value using a convenient two-digit hexadecimal notation, where each hex digit represents exactly 4-bits (often called a nybble because it is half-a-byte). Thus an 8-bit byte can be represented by any hexadecimal value in the range 0x00 to 0xff (or 0 to 255 decimal).

(Some systems use odd-size bytes, such as a 9-bit byte. For this we typically use 3-digit octal notation because an octal digit represents exactly 3 bits. Such systems are rare, but we sometimes come across other odd-sized bytes, especially in older data transfer systems such as dot-matrix printers which utilised a 7-bit byte. However, in modern architecture, we can safely say that a byte is always at least 8 bits long.)

Not all programming languages utilise a byte data type as such. C, for instance, doesn't have a built in byte data type but it does have a char data type which is always 1 byte in length. There's no real reason why there isn't a byte data type in C, but when all data types are measured in terms of bytes it was probably deemed unnecessary to say that a byte is 1 byte in length. Although a char is typically used to encode a single character from a character set (and has built in overloads specific to that purpose), the encoding is no less numeric than a byte would be, so there was no real need for a separate byte data type.

Although a single byte can represent any decimal value in the range 0 to 255, it is more correct to say that a single byte can represent any one of 256 unique abstractions. Whether it is a single character from a character set, an unsigned value in the range 0 to 256, or a signed value in the range -128 to +127, these are merely abstractions. How abstractions are interpreted is entirely down to the programmer and/or the programming language.

What is the structure of nylon 66?

Nylon is a generic name for a synthetic linear polymer with repeating amide groups (-NH-CO-) which is used in the manufacture of textile fibres. Carothers produced Nylon 66 by condensation reaction of adipic acid (a dicarboxylic acid with 6 carbon atoms) and hexamethylenediamine (a diamine with 6 carbon atoms) give the [-NH-(CH2)6-NH-CO(CH2)4-CO-] repeating unit. Nylon revolutionised the textile industry and was the forerunner for many of today's modern, synthetic fabrics.

You cannot throw an exception from a destructor?

Sure you can, but it's never a good idea. You must never allow an exception to escape from a destructor. To do so would terminate the destructor prematurely, unwinding the call stack in search of an exception handler. This means that any remaining resources consumed by the instance, including its base classes, would be left in an invalid state with no possible way to recover those resources besides terminating the program. Any object that has the potential to throw an exception during its own destruction should always be treated with suspicion; it is a major design flaw.

Why wont your c plus plus program stay open?

If you are talking about the program executing, but the output screen being displayed for a flash and then disappearing, I suggest adding getch() or getchar() function at the end of your main function. This will make sure that the output screen waits for you to press a character before the program terminates.

How should you design a scientific calculator using C plus plus?

The easiest way to implement a calculator is an RPN calculator (enter the numbers first, perform the operation last). You need a last-in-first-out stack (there's a "stack" class in C++, but you can also implement your own using an array or a linked list), and a set of functions that pop the last elements from the stack and push the result (e.g. Add() pops the last 2 values and pushes their addition).
You'll need the math.h library for scientific operations.

How do you pass an array to a copy constructor in c plus plus?

You cannot pass an array to a copy constructor. A copy constructor only accepts a constant reference to the object being copied, which must be of the same class as the object being constructed. An array is not an object of any class, and therefore cannot be used in any copy constructor.

Although you cannot pass an array to a copy constructor, you can pass an array to a non-trivial constructor. It is not recommended, however, as there's no way to bounds-check the array being passed, which could result in an invalid object being created -- which is never a good thing. Even if you pass the array and its dimension(s) to the constructor, how can you guarantee those dimensions are valid for the array being passed? And what will you do if they are invalid? After all, you cannot veto the construction of an object once you've called its class constructor.

Not knowing why you want to pass an array to a copy constructor, or how you intend to initialise the members via an array, makes it somewhat difficult to determine the best solution for you. However, I would consider using mutators instead of constructors. There's still the problem with bounds-checking but at least you won't have to deal with it during the object's construction.

What is Quadrature?

It has several meanings, none of which have anything to do with computer programming. In mathematics, a Quadrature is a numerical integration.

Why recursive solution is better for tree traversal?

Because a tree is a recursive data-structure. It's easier to write (and easier to understand) a recursive program for handling it.

What Cygwin packages do I need to download for compiling C plus plus files?

g++, gdb and make. A simple search for "Cygwin c++" will tell you all you need to know.

What is the use of Constructor with Dynamic allocation?

Constructors are necessary to initialize classes. It allows to avoid to a lot of problems with unauthorized access of memory.

Dynamic allocation makes possible allocation of memory during execution of program. If you do not use dynamic allocation, all required memory will be allocated during initialization phase (constructors are usually responsible for that). But you can't use more memory. Dynamic allocation was designed to overcome such problems.