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 to print the reverse of an array without using backward loop?

int youArray[arraysize] = {...};

...

for (int i = 1; i <= arraySize; i++)

{

cout << yourArray[arraySize - i] << endl;

...

}

...

Which base class member functions are not inherited by a derived class?

Derived classes only inherit the protected and public members of their base classes. Private member functions cannot be inherited by a derived class.

What is step involve is using data file in c plus plus programming?

Declaration of file pointer

opening of file in desired mode.

performing the desired operation.

closing the file

Write a program that will prompt you to give the number of pods and the number of beans per pod and calculate the total number of beans use C plus plus?

#include

using namespace std;

int main()

{

int pods,beans;

float total;

cout<<''enter the number of pods";

cin>>pods;

cout<<'enter the number of beans";

cin>>beans;

total=pods+beans;

cout<

return 0;

}

How to write a C plus plus Program to convert a string into an integer?

std::string input = "";

std::getline (std::cin, input); // get input from stdin

std::stringstream ss (input); // place input in a string stream

integer num = 0;

if (ss >> num) // extract integer from string stream

{

// Success!

}

else

{

// Fail!

}

Can there be at least some solution to determine the number of arguments passed to variable argument list function?

Yes. Use a control parameter before the variable argument list that determines how many arguments will follow. E.g., the printf() function uses a string parameter as the control parameter. The number of escape sequences in the string determines the number of arguments that are expected to follow, and each escape sequence in the string is expanded from left to right according to the arguments that follow. A simpler approach is to pass the number of arguments that will follow as a named argument. E.g.,

void print_nums(int n, ...)

{

// n tells you how many numbers are to be expected in the va_list.

}

Answer

Yes, there can be solution.

#1: explicitly specifing:

extern void variadic_1 (int n, ...);

variadic_1 (3, "first", "second", "third");

#2: using terminator:

extern void variadic_2 (...);

variadic_2 ("first", "second", "third", NULL);

What is the use of header file stdbool.h?

stdbool header file use for a new data type that is boolean value

How does the inline mechanism in C reduce the run-time overheads?

inline functions are compiled very fastly and uses the free memory to boot it as soon as possible

When is memory allocated for a function of a class in c plus plus?

Never. A class is a type definition that only exists in the source code, so no storage is ever allocated to it. This can be proved by the simple fact that you can never take the address of a class.

When you instantiate an object from a class, the object and its members have identity thus you can take the address of that object and its members. Similarly, as soon as you use the static members of a class, you may take the address of those members. However the class itself does not exist; its sole purpose is to assist the compiler in generating the code that allows you to call static member functions and to instantiate objects of the class. Once that code is compiled, the class definition is entirely redundant.

What is the built in function to draw lines in Visual C plus plus?

C++ has no built-in function to draw lines, nor indeed to perform any type of graphics output. The standard library is designed to be as generic as possible, and is therefore capable of supporting all platforms using text output only. Graphics output is obviously possible, but it is platform-specific so you will need a graphics API that provides functions that are specific to your operating system and/or hardware.

C plus plus for while loop example code?

The syntax for a for loop is:

for (initialization; condition; increase) {

statement;

}


for example:


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


for (int i = 0; i < 5; i++) {


cout << arr[i] << endl;

// you can do alot more in a for loop than just print to console


}


while loop syntax


while (condition) {

// statements

}


example:


int i = 0;


// the loop will end when i = 5

// unlike for loop you must increment variables yourself or

// use the break command to exit the loop

while (i != 5) {

cout << i << endl;

i++;

}


What is the output of the following program int total int sum equals total total equals 100 print sum equals percent d total equals percent d sum total?

Translation of question:

int total;

int sum = total;

total = 100;

print("sum = %d, total = %d\n", sum, total );

The output is undefined because you used total before initialising it. However, if we assume total is initialised to zero, then the output would be:

sum = 0, total = 100

Can you perform insertion and deletion in an array?

You can, but it's not as straightforward as inserting or deleting from a list. This is simply because arrays are not intended for insertions or deletions other than at the end of the array. This is because an insertion requires that the entire array be reallocated (which may require the array to be copied in its entirety) simply in order to make room for the new element, which can then simply be inserted in the unused element at the end of the new array. To insert elsewhere in the array, all the elements following the insertion point need to be copied (for a second time) into the next element, starting with the last used element. the entire process can prove quite costly, especially if the elements are complex objects which would require their copy constructors to be invoked at least once and possibly twice. This is why it is generally better to use arrays of pointers to objects rather than arrays of objects, as copying a pointer is more efficient than copying an object. However, if your array undergoes frequent resizing in order to accommodate insertions and deletions, then you really would be better off using a list.

What is difference between conditional operator and bitwise operator?

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:

1 + 2

The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:

1 + 2

The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:

1 + 2

The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:

1 + 2

The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.

Write program of summation from 1 to 5 using function c plus plus?

There are better ways to do this, but the following example will get the job done all the same.

#include <iostream>

int foo(int & X, int & Y)

{

if( X > Y ) // Ensure X < Y.

{

int T = X; X = Y; Y = T; // Swap.

}

int Total = X;

if( X!=Y) // Skip summation when equal (1 to 1 = 1).

{

int Addend = X; // Begin summation.

while(++Addend <= Y) Total += Addend;

}

return( Total );

}

int main()

{

int X, Y;

std::cout << "Enter start value: ";

std::cin >> X;

std::cout << "Enter end value: ";

std::cin >> Y;

std::cout << std::endl;

std::cout << "The summation of " << X << " to " << Y << " is: " << foo( X,Y);

std::cout << std::endl;

return( 0 );

}

How do you write a C plus plus program for calculating the sum of even numbers entered by user using static data and static member functions?

#include<iostream>

#include<vector>

class foo {

private:

static std::vector<size_t> evens;

public:

static bool add_number (size_t num);

static size_t sum();

static std::vector<size_t>& get_evens () { return evens; }

};

bool foo::add_number (size_t num) {

if ((num%2)==0) { evens.push_back (num); return true; }

return false;

}

size_t foo:sum () {

size_t sum {0};

for (auto num : evens) sum+= num;

return sum;

}

int main () {

size_t in;

while (std::cin >> in) {

foo::add_number (in);

}

for (auto num : foo::get_evens()) std::cout << num << std::endl;

std::cout << "Sum = " << foo::sum() << std::endl;

}