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 does the compiler interpret more than one definition of the same name?

The compiler disambiguates function overloads by their signatures. A function's signature is defined by its name and its formal parameters, but not the return type. As you probably know, function overloads cannot differ by return type alone, thus the return type does not form any part of the signature. The compiler determines which function to call based upon the arguments that are passed by the individual callers.

Is it necessary to provide a definition to the pure virtual function in the inherited derived class?

No, it is not necessary, but the derived class will be rendered abstract as well. Sometimes that can be desirable. Only derived classes that provide a complete implementation of all inherited pure-virtual methods can actually be instantiated. However, derived classes can also inherit pure-virtual implementations from any intermediate base classes, but not from the least-derived abstract base classes (where the pure-virtual methods originated). Abstract base classes can also provide their own default implementations of their own pure-virtual methods, but they cannot be called implicitly, even from the base class itself. These methods must still be overridden by derived classes, even if an override only needs to explicitly call the base class method.

What does it mean to be concrete derived class?

A concrete derivative is a class that fully implements all the pure-virtual methods it inherits from its abstract base classes. Some methods may be inherited rather than implemented, however only a concrete class can actually be instantiated.

Basic structure of c n c plus plus?

The basic structure of a C or C++ program is built around types. A structure is a type. A function is a type. A class is a type. All of these types can be built from primitive (built-in) types and can be used to create ever-more complex types.

How do you separate digits in c plus plus?

Repeatedly divide the number by 10 and store the remainder (the modulo).

By way of an example, if the number were 12345:

12345 % 10 = 5 (first digit)

12345 / 10 = 1234

1234 % 10 = 4 (second digit)

1234 / 10 = 123

123 % 10 = 3 (third digit)

123 / 10 = 12

12 % 10 = 2 (fourth digit)

12 / 10 = 1 (fifth digit)

This algorithm forms the basis of number reversals. The following function demonstrates the most efficient way of reversing any number in the range -2,147,483,648 to 2,147,483,647, inclusive.

int RevNum( int num )

{

const int base = 10;

int result = 0;

int remain = 0;

do

{

remain = num % base;

result *= base;

result += remain;

} while( num /= base);

return( result );

}

What are public data members in a class?

Public data members are akin to structure data members (which are public by default). Since they are public, they are fully exposed outside of the class. Data validation becomes impossible. Even if you define an interface, there's no requirement to use it since the members are readily available. If the members must be validated, do not assign them public access.

How do you do create a working copy of a class in c plus plus?

To create a working copy of a class you simply copy the class definition.

struct A { int data; }; // original class

struct B { int data; }; // copy of the class

However, it makes no practical sense to have two copies of the same class. Remember that a class is nothing more than a type definition and we only ever need one definition for any given type. If we copy a class, we actually define two completely independent types. A and B are not of the same type, even though their implementations are completely identical in every way. The names alone are what actually differentiate them.

Once defined, we can use the exact same definition in any program that requires it without copying it. If we choose to copy classes rather than reuse existing ones, we only increase the maintenance burden because any changes to one copy will have to be replicated in all the copies (assuming we wish all copies to behave identically), and that can quickly lead to inconsistencies.

To avoid the need for copies, we simply place the class definition in a header file which can then be included (using the #include compiler directive) in any program that requires it. The class implementation is (usually) placed in a corresponding source file which must also be part of the compilation but if we wish to use the class in more than one program then we can simply compile the source as a library and link to the library instead.

Which C plus plus keyword allows a function outside of a class to access private class members?

The keyword is friend. The external function must be declared a friend of the class (from within the class itself) in order to become a member of the class and thus gain access to the private (and protected) members of the class.

What is structure alignment in C?

Structure alignment relates to the way objects in memory must be allocated according to the underlying architecture. For instance, a machine might expect an int to be aligned upon a 4-byte boundary. If that is the case then a structure with a member int must be aligned upon a 4-byte boundary and the int member offset from that boundary by some multiple of 4 bytes.

Consider the following:

struct X_ { char c; int i;

} X;

assert (sizeof(X)==sizeof(int)+sizeof(char)); /* oops */

Assuming sizeof(int) is 4 and sizeof(char) is 1 and that an int must be aligned on a 4-byte boundary, the actual length of this structure must be 8 bytes, not the 5 we expected. If it were anything other than 8, then an array of such structures would have misalignments in 3 out of every 4 elements. Note that in order to correctly align this structure, the compiler must insert 3 padding bytes immediately after the char member. This ensures that the int member is offset 4 bytes from the start of the structure. So long as the structure itself is aligned upon a 4-byte boundary (which it now will be because 8 is a multiple of 4), the int will always be correctly aligned.

Inserting padding bytes between members can often increase a structure's size far more than we might expect, particularly if there are many members. To avoid this, it is best to declare members in order of size, largest first. In this way, whenever padding is required, the padding is more likely to be placed at the end of the structure rather than between two or more members.

struct X_ {

char c;

int i;

char c2;

} X;

Given the same criteria as before, this structure would be 12 bytes in length. But by changing the order of the members, we can reduce this to just 8 bytes:

struct X_ {

int i;

char c;

char c2;

} X;

The reason this works is because a char can always be aligned on a 1-byte boundary, so two consecutive char members will easily fit inside a 4-byte word. Thus we only require 2 bytes of padding instead of 4.

Note that it is never safe to assume the lengths of any data type in C other than char which is always sizeof(char)==1. The lengths of all other types are measured in terms of a char, but will vary from system to system according to the underlying architecture. Always use the sizeof() operator to determine type lengths. Note that the sizeof() operator computes data type lengths at compile time, so there is no runtime cost, nor is there any need to store the return value.

Can you do c plus plus programming on a mac?

You may use one of several open source compilers and code editors (or even IDEs) to develop and compile C++ code that will operate on a Mac.

What are the image formats supported by c plus plus?

C++ does not provide any native support for graphics of any kind, including graphic image formats. This is because graphics are platform-specific while C++ is a generic language. You can, of course, use graphics in C++, but you need a graphics library and API that is specific to your platform and hardware. There is no generic code available as the code you use is entirely dependant upon the library.

Does inline function increase the code size?

The inline specifier might increase the code size, but it might also reduce it.

It depends on the size of the inlined function versus the overhead of setting up a stack frame and invoking the call/return sequence. Often, the inline specifier is used for very short, usually one line functions, and the intent is to sacrifice a bit of code size for execution size.

Keep in mind that the inline specifier is only a compiler hint, and that the compiler may or may not actually inline the function, depending on context.

What is a constant member function c plus plus?

A constant member function is an instance method that can only modify the mutable members of the class instance. This is achieved by implicitly declaring the this pointer constant. The this pointer is a hidden parameter that is implicitly passed to every instance function, and that points to the current instance of the class. Static member functions do not have an implicit this pointer and therefore cannot be declared const.

Consider the following simple class that has both mutable and non-mutable member variables, and both constant and nonconstant member functions:

struct foo

{

void constant()const;

void nonconstant();

private:

int data1;

mutable int data2;

};

void foo::constant()const

{

data1++; // not permitted: the implicit this pointer is declared const

data2++; // ok: data2 is mutable

}

void foo::nonconstant()

{

data1++; // ok: the implicit this pointer is declared non-const

data2++; // ok: data2 is mutable

}

Note that data2 can always be modified since it is declared mutable, but data1 can only be modified by nonconstant member functions. Members should only be declared mutable when they are only used internally by a class (such as when locking a mutex for thread safety), or where a value needs to be calculated and cached the first time it is accessed.

What are separators in c plus plus?

Separators include commas, colons, semi-colons and white-space.

Semi-colons are used to indicate the end of a code block or to separate one function from the next.

Commas are used to separate function arguments, or to separate structure variables and class member initialisation lists (comma-separated lists).

Colons are mainly used to signify class inheritance and the start of class initialisation segments.

White-space separates a variable type from its name, but is also used to aid the readability of code.

What do you mean by free store in c plus plus?

The free store in any language refers to the heap. The three main areas of memory that all C++ programs use are the heap, the call stack and static memory. Static memory is allocated at compile time, is fixed-length and caters for all static variables, global variables and constant variables. Call stacks are also fixed-length and are allocated to threads of execution as they are instantiated (each thread has its own stack). The free store or heap is essentially all remaining memory accessible to our program. To use the heap we must request memory from the system as it is required and release it when we are finished with it.

What people have to pay EI and CPP?

if you can't collect EI after 65 do you have to pay EI after 65

Why it is required to create mainframe object on the heap?

You need to create the mainframe object on the heap so that the object does not go out of scope and get automatically deleted when your function exits. You could create it on the stack if the function doing that does not exit until everything is deleted, such as in the main loop of CWinApp. There are also issues with available memory in stack versus heap, as the stack size is preallocated while the heap can grow to the size of available memory.

Why scope resolution operator cannot be overloaded?

Generally the operators that can't be overloaded are like that because overloading them could and probably would cause serious program errors or it is syntactically not possible,

For instance the sizeof operator returns the size of the object or type passed as an operand. It is evaluated by the compiler not at runtime so you can not overload it with your own runtime code. It is syntactically not possible to do. Even if it was pointer arithmetic relies on the correct value being returned by this operator since the compiler already knows how to calculate the correct value all overloading would do would be to allow you to calculate an incorrect value, something that would almost certainly lead to the program not working correctly.

Scope resolution and member access operators work on names rather than values. C++ has no syntax for writing code that works on names rather than values so syntactically these operators can not be overridden.

Again what useful purpose would overloading the conditional operator produce? I can think of none.

What are delphi visual basic c c plus plus and java?

Delphi is an Embarcadero product (formerly developed by Borland and then CodeGear). It is basically an object-oriented version of Pascal, ideally suited to database connectivity.

Visual Basic is Microsoft's IDE for object-oriented BASIC, ideally suited to general purpose Windows development.

C/C++ are general purpose languages. C++ is the successor to C, which introduced object-oriented programming to the C language. Everything you could do in C you can also do in C++, with minor modification. Both Embarcadero and Microsoft have their own versions (Turbo C++ and Visual C++ respectively). Versions of C++ are also available for the Mac and other Unix platforms.

Java is a general purpose language developed by Sun Microsystems. It is loosely based upon C++, with very similar syntax, but with a much simpler object model and full cross-platform support. However it is much slower than C++ because it must run in a virtual machine environment. Microsoft initially developed Visual J++ to compete with Java, but has since abandoned this in favour of Visual C# and the newer Visual F#, both of which rely heavily on the .NET Framework.