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 does the delete operator do in addition to deallocation of memory space?

The delete operator calls the destructor of the object referenced by its operand. If the destructor is virtual, the destructor of each superclass up to the top of the inheritance hierarchy is also called, in order. If you don't define a destructor for a class, the compiler defines a default destructor that has no effect. Fundamental types (char, int, float, etc.) do not have destructors, so using delete has no other effects.


As an aside: when you use inheritance, make sure to make your destructors virtual, so that objects are properly destroyed!

Also note that you should not use C's free() on a pointer that you got from C++'s new, or use C++'s delete on a pointer you got from C's malloc(). These are not guaranteed to work, and mixing them might cause Big Bad Things to happen. In general, there is no reason to use malloc()/free() in C++ at all.

What is unary minus operator?

No. The subtraction operator is a binary operator that returns the result of subtracting the rhs operand from the lhs operand. The unary minus operator simply negates the rhs operand.

int x = -5; // unary minus. x is (-5)

int y = -x; // unary minus. y is (+5)

y -= x; // binary minus/assign operator. y is (+10)

--x; // unary decrement operator. x is (-6)

y -= (-x); // binary minus/assign and unary minus operators. y is(+4)

What is the advantage of cin getline over cin?

The getline() function reads a string of characters up to a newline character or a predefined delimiter. Although extracting directly from cin is faster, the onus is on the programmer to ensure the input is valid at all times, and to clear the buffer before accepting new input. Keyboard entry is particularly problematic, thus it is easier to treat each input as a string which can be easily verified and convert to a numeric value where required.

What is stray pointer?

stray pointer is a that pointer which pin points nothing or anywhere but we dont know...

for example:

int *ptr;

ptr=new int[10]; //this memory is on heap.

and at the end of the programm if we dont delete this memory mean to say

if we dont deallocate this memory then this type of pointer is pointing towards nothing or anywhere because when we work on heap deletion of pointer is must if we dont delete pointers than they pin point stray or anywhere so that sort of pointer is stray pointer.

i think you understand...

How do we tuple initialize a class or structure?

Consider the following structure:

struct X {

int a;

double b;

// ...

};

Here we could initialise with a std::tuple<int, double>. To achieve this we simply define a constructor that accepts the required tuple:

#include<tuple>

struct X {

int a;

double b;

X::X (std::tuple<int, double>& t): a {std::get<0>(t)}, b {std::get<1>(t)} {}

// ...

};

Note that any constructor that has one argument is known as a conversion constructor, in this case converting from tuple to X. It is usually a good idea to declare such constructors explicit, particularly if you also provide the complementary conversion operator (from X to tuple).

#include<tuple>

struct X {

int a;

double b;

explicit X::X (const std::tuple<int, double>& t): a {std::get<0>(t)}, b {std::get<1>(t)} {} operator std::tuple<int, double> (void) const {return std::make_tuple (a, b);}

// ...

};

Syntex for accessing data members of the structure in c plus plus?

Use the member accessor (.) operator.

struct object {

int m_data;

};

int main()

{

object o;

o.m_data = 100;

std::cout << o.m_data << std::cout;

return(0);

}

C plus plus program that display student name course and grade using arrays of strings?

enum field { name, course, grade }; std::string student[3];

student[name] = "Joe Bloggs";

student[course] = "C++ Programming";

student[grade] = "A+";

Why the format specifiers are not used in cpp?

You might be wrong: printf and scanf are usable in C++ just as in C. With format specifiers.

The most common function of an array is to store variables however there will be times when an array is used to store objects. briefly describe four such objects?

Any name can be placed in an array, be it a primitive name or an object name, even a function pointer. Four examples of common objects that may be placed in an array include any object defined in the STL (standard template library), which includes vectors, lists, iterators and maps. All are self-explanatory, although a vector is simply an array implemented as an object. Therefore an array of vectors is nothing more than an array of arrays (effectively a multi-dimensional array). However, vectors are the "correct" way of implementing arrays in C++ (unless you specifically wish to use C-style code in your C++ projects), thus a multi-dimensional array is best implemented as a vector of vectors.

Most c plus plus programmers use uppercase letters for variable names true or false?

False. Most C++ programmers use uppercase for macros (precompiler definitions), making them less likely to be confused with actual variables, constants or functions in the C++ source code. Macros are not actually part of the C++ language because the compiler never sees them, but they allow the precompiler to perform preprocessing tasks that would be difficult or impossible to accomplish with C++ code alone.

How do you convert time from 24 hour to 12 hour in C Plus Plus?

#include<iostream>

using namespace std;

int main()

{

int t1;

int t2;

char a;

char p;

cout << "enter two numbers for example 0930 for 9:00 !" << endl;

cin >> t1;

cout << "enter number two" << endl;

cin >> t2;

cout >> "totel amount of hours are">>h>>endl;

system("PAUSE");

return 0;

}

What is the significance of asmlinkage modifier in C?

The asmlinkage tag tells gcc that that the function should not expect to find any of its arguments in registers (a common optimization), but only on the CPU's stack. Many kernel functions use the fact, that system_call consumes its first argument, the system call number, and leaves other arguments (which were passed to it in registers) on the stack. All system calls are marked with the asmlinkage tag, so they all look to the stack for arguments.

What are the types to pass a structure to functions?

A structure is a type so you just need to pass the structure as you would any other data type: by reference or by value.

What are the features differentiate java from c plus plus and c?

Java compiles to byte code suitable for interpretation by the Java virtual machine, whereas C and C++ both compile to native machine code. Thus C and C++ programs perform better than equivalent Java programs. However, Java programs can run on any machine with a suitable Java virtual machine implementation, which is pretty much everything these days. C and C++ programs must be compiled separately upon each supported platform, provided the source code is either generic or includes compiler directives to filter the platform-specific code. Java programs need only be compiled once, thus cross-platform development is greatly simplified, at the cost of performance.

C and Java cannot really be compared since C does not support object-oriented programming concepts. C++ is object-oriented but, unlike Java, it is not 100% object-oriented as it supports the concept of primitive data types that it inherited from C. Java is more closely related to C#, which is 100% object oriented.

How do arrays differ from single memory position variables?

A single memory position variable can store only one value of its type.

An array can store n number of values, where n is the size of the array.

Program to find factorial of a number using inheritance?

/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/

#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

{

int n,f;

clrscr();

printf("Enter number whose factorial is to be calculated:

");

scanf("%d",&n);

if(n>0)

{

f=fact(n);

printf("factorial of %d is %d",n,f);

}

else

printf("Factorial of numbers less than 1 does not

exist");

getch();

}

int fact(int n)

{

int facto=1;

if(n>1)

facto=n*fact(n-1);

else

return 1;

return(facto);

}

What is x2 plus 3x plus 7 equals 6x plus 18?

xx+3x+7=6x+18

xx+3x+7-(6x+18)=6x+18-(6x+18)

xx-3x-11=0

Factors of -11:

1,-11

-1,11

Doesn't factor evenly, use quadratic

Can you delete array?

If the array was allocated with new, then delete it with delete []. Otherwise, if it was allocated with malloc() then delete it with free. Otherwise, you cannot delete it because it was pre-allocated at link-load time by the compiler.

What function is used to release the memory on heap?

The free() function releases memory obtained with the malloc() family of functions. The delete and delete [] operators release memory obtained with the new operator.

Note: realloc() can allocate and free memories as well as reallocate.

Is a subclass only method through a base class variable allowed?

Yes, it is allowed, but it is considered poor design to do so. The problem is not that your subclass contains a specialised, non-generic method, it is only in how you are accessing it. When you hold a variable, pointer or reference to a base class then you are expected to treat that class generically. If you cannot do this, then you should seriously rethink your design. Always ask yourself why you are attempting to extort non-generic behaviour from a generic class.

That said, it is sometimes the case that the base class design is outwith your control. It is still the result of poor design but in these cases you have little option but to make use of dynamic downcasts in order to extort specific behaviour from your subclass. The only alternative is to veto the base class completely and write your own, but if this requires a major rewrite then a dynamic downcast might be the better option in the short-term.

Another option would be to use an intermediate subclass, but this is only suitable when you have two or more subclasses that share a common interface, but where that interface is not generic enough for the base class itself. This won't resolve the problem of extorting non-generic behaviour when you hold a reference to the base class, but it can help reduce the number of dynamic downcasts to a minimum (the ideal situation is no dynamic downcasts, but the option is there if you really must use it).

Percolating the specific implementation into the base class can often be a simpler option. Although you should generally avoid this, if you are designing a closed hierarchy then the cost of the reduced encapsulation may be considered a more preferable option to the additional expense of a dynamic downcast.

Can you declare float variable as increment operator in for loop?

I don't really understand your question, but for example the following code is perfectly legal:

float f;

for (f=0.0; f<=1.00001; f += 0.1) printf ("%g\n", f);