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!
}
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++;
}
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
How many bytes required for int float double char boolean?
Int: 4 bytes
Float: 4
double: 8
char: 1
boolean: 1
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 );
}
#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;
}
P 2x-9y q 5y plus 6-4x r 3x plus 3y-5 then p plus q plus r?
p + q + r = (2x - 9y) + (5y + 6 - 4x) + (3x + 3y - 5)
= x - y + 1
How do you spell M C plus ing as in I am mcing an assembly?
You don't so much spell it as phrase it correctly. MC++ is a noun, not a verb, so the grammatically correct phrase is "I'm coding an assembly in MC++".
The only time you should calculate it instead of storing is when you only use that attribute once. Otherwise, store it to avoid repeating calculations.
Difference between sizeof and strlen?
The sizeof operator returns the total size, in bytes, of the given operand, whereas the strlen function returns the number of characters in the argument up to but not including the first null-terminator.
Consider a character buffer allocated 50 bytes to which you assign the string "Hello world". The sizeof operator will return 50, but the strlen function returns 11.
How do you use try block in cpp?
A try statement is used in conjunction with one or more catch blocks to provide exception handling. If an exception is thrown by a try block, the corresponding catch block will handle the exception. If no catch block is provided for a particular exception, then a runtime error occurs instead. Try-catch statements are used to provide graceful resolutions to potential runtime errors.
What is the purpose of using new and delete operations?
New and delete are akin to malloc and free, respectively, in C. Although both malloc and free are still available in C++ (for backward compatibility and interoperability with C-style code), the new and delete operators are much simpler to use because the allocation and deallocation of all dynamic memory consumed by an object is handled by the object's own constructors and destructors. Thus there is no need to calculate how much memory to allocate; the new operator can work that out all by itself, based upon the class of object being instantiated, and the object itself can allocate/deallocate any additional dynamic memory as and when it actually requires it.
This mechanism of construction/destruction allows highly complex dynamic objects and object hierarchies to be instantiated at will with just a single call to the new operator. Each object takes care of itself (as do all embedded objects and/or base classes of the object), thus all the programmer has to do is instantiate and delete an object as and when required.
Which members of the circle class are encapsulated?
The attributes that are common to all circles are a centre point and a radius. However, a circle is a shape thus it should also encapsulate attributes or interfaces that are common to all shapes, such as width(), height(), draw(), move(), fill(), outline() and so on. Thus it makes sense for your circle class to inherit from a common generic base class such as shape, where shape provides a common, pure-virtual interface and encapsulates attributes that are common to all shapes, such as line-width, line-colour and fill-colour.
Ultimately there is no single object model for a circle -- the implementation and encapsulation are entirely dependant upon your needs and the level of functionality you require. If your requirements are different for every application then you might want to separate all the interfaces over several classes and only include what you actually need using multiple inheritance. Thus you can have simple circles which use the minimum memory, as well as more specialised circles which contain more specific functionality.