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 is the usage of union in CPP?

A union in C or C++ is like a structure, except that each element in the structure starts at the same memory address. It is useful for storing more than one type of data, but only one at a time, in the same memory location.

It can also be used for the potentially unsafe technique of looking at the bit patterns of one data type using another data type. (It is unsafe because internal memory organization of bits and bytes is an implementation specific thing, and depending on one particular organization can lead to incorrect program behavior.)

Four components of capital structure?

the components of capital structure(CS) includes:

1. CS with equity sahres only.

2. CS with equity and preference shares.

3. CS with equity and debentures.

4. CS with equity shares, preference shares and debentures.

What is the main purpose in using a destructor?

Destructors are called automatically whenever an object falls from scope. Even if you don't actually declare a destructor, one is implied.

The primary purpose of a destructor is to give the programmer one final chance to clean up an object's memory allocations before the object is destroyed forever. If an object has allocated memory associated with it (via member pointer variables), then that memory must be released before the object is destroyed, otherwise a memory leak is inevitable.

Destructors are also important in class inheritance. When a derived object falls from scope, its immediate base class destructors are called, followed by their base class destructors. This is effectively the reverse of an object's construction, which always begins with the least-derived base classes, working up to the most-derived class, which is the object itself. Just as a derived class cannot be instantiated until all its base classes are constructed, a base class object cannot be destroyed until all its derived class objects are destroyed. All the destructors in a class hierarchy must be declared virtual to ensure the correct most-derived destructor is called whenever a base class reference falls from scope.

How do you write a c plus plus program to print a hollow square of a given size using asterisks and blanks?

void draw_box (const unsigned size)

{

if (!size) return; // size must be non-zero!

const char star {'*'}; // asterisk

const char nl {'\n'}; // newline

if (size==1)

{

// a box of size 1 is just a single asterisk

std::cout << star << nl;

return;

}

// else if (1 < size) ...

// the top and bottom lines are the same

const std::string line (size, star);

// hollow line (spaces)

const std::string hollow (size-2, ' ');

// draw the top line

std::cout << line << nl;

// draw the middle lines (if size>2)

for (unsigned i=2; i<size; ++i)

std::cout << star << hollow << star << nl;

// draw the bottom line

std::cout << line << nl;

}

How do I read a memory address with borland c?

In order to read a memory address you must hold a reference to that address. If the object at that address is a named object, then the object's name is a reference, thus we can simply take its address using the address-of operator (unary &):

int i = 42;

printf ("Variable i resides at memory address 0x%x\n", &i);

If we wish to store the address we must use a pointer variable of the appropriate type:

int* p = %i;

Like any other variable, a pointer variable has an address of its own, thus we can read its address:

printf ("Variable p resides at memory address 0x%x\n", &p);

To read the address being pointed at we simply examine the pointer's value:

printf ("Variable p refers to memory address 0x%x\n", p);

Note this address is the same as the address of i, the object being referred to (pointed at).

To read the value stored at the address being pointed at we must dereference the pointer (unary *). Dereferencing is also known as indirection because we are indirectly accessing the object's value:

printf ("Variable p refers to the value %d\n", *p); // e.g., 42

Pointers can refer to both named and anonymous objects. Anonymous objects are simply objects allocated on the heap at runtime. Since they don't exist at compile time we cannot name them:

int* anon = malloc (sizeof (int)); // allocate memory

Note that anon is the name of the pointer, not the object being pointed at.

printf ("The anonymous variable resides at memory address 0x%x\n", anon);

free (anon); // always release heap allocations as soon as we are finished with them!

Pointers also make it possible to pass memory addresses to and from functions (also known as pass by reference):

void f (int* p) {

}

int a = 42;

f (&a); // pass the address of a to the f() function

Note that all variables in C are passed by value, but when we pass a pointer by value we are passing an address and an address is a reference. Passing references is useful when the object being referred to cannot be efficiently passed by value. Typically this means any value that is larger than the word value of the underlying architecture (e.g., 4 bytes on a 32-bit system). Arrays in particular are never passed by value, hence an array implicitly converts to a pointer.

A program to find sum of digits repeatedly until a single digit is obtained?

#include<stdio.h>

#include<conio.h>

main()

{

int n,s=0,sum=0,i,d;

printf("enter the number");

scanf("%d",&n);

while(n>0)

{

d=n%10;

s=s+d;

n=n/10;

}

while(s>0)

{

i=s%10;

sum=sum+i;

s=s/10;

}

printf("%d",sum);

getch();

}

How do you save GIF extension file in Dev c plus plus?

The extension is actually immaterial. It merely serves to give the operating system a hint as to the file's content, allowing the file to be associated with a particular application, such as a GIF file viewer or an image editor application. In order to save a GIF file, you must first re-encode the image (assuming it is not already in GIF format), and save the output to a file with a GIF extension.

C plus plus file processing that will input 12 integers?

#include<iostream> #include<vector>

int main()

{

std::vector<int> integers (12);

for (size_t loop=0; loop<integers.size(); ++loop)

cin >> integers[loop];

}

What is difference between direct addressing and indirect addressing in c plus plus?

Indirect addressing uses a pointer. Indirectly accessing the memory being pointed at is known as dereferencing. Direct addressing uses a variable's name or a reference to obtain the value.

Can a constructor be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

##

Constructor called implicitly not explicitly so constructor is not virtual.

How many bytes are occupied by declaring following array of characters?

A single-byte type of array has 1 byte per character; a wide-character array has 2 bytes per character of storage. Without seeing the exact definition it cannot be determined what the actual size of the array would be.

How do you check address of char variable?

Use the address-of operator:

char c=32; // space character

std::cout<<&c<<std::endl;

Why are Logical errors harder to locate in C plus plus programs?

Logic errors in any computer language may be difficult to find. Languages that are object oriented can make it harder because you have to follow the object activations, which are separate from plain structural code.

However, it isn't any more difficult in C++ than some other OO languages to find logic errors; in all cases it can be difficult.

Why does an abstract class often define methods WITHOUT actually implementing them?

Because, it wants the implementing or child class to take care of the actual implementation details.

What is Cin and Cout?

The cin and cout objects are iostream objects somewhat equivalent to stdin and stdout.

The equivalent of printf ("Hello World\n"); is cout << "Hello World" << endl;

The equivalent of scanf ("%d", &i); is cin >> i;

Is it possible to make a program that combines a lot of other programs?

Yes. One program can execute another program very easily -- you can even do it via scripting languages and batch programming. Morevoer, programs that provide shared code via one or more libraries make it possible for another program to execute code within those libraries, just as if that code were part of the program itself (no need to execute another program). Such programs often provide application programming interfaces (APIs) to simplify the process of integration.

What is an arity?

An arity is a number of arguments or operands a function or operation takes.

How do you merge 2 Visual C programs to get a single output?

You cannot. A C program can only have one global main function but you'd be trying to compile a program that has two main functions. The only way to merge the two programs is to modularise both programs so that neither is dependent upon their main functions. Once modularised, you can include those modules in any program. Alternatively, you can create binary libraries from the modules and link them into any program.

What is platform dependency of c plus plus?

C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives.

For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation:

#ifdef __unix__

#include <unistd.h>

#elif defined _WIN32

#include <windows.h>

#endif

The definition of __unix__ and _WIN32 must be mutually exclusive.

Write any small program that will compile in C but not in C plus plus?

#include

int main (void)

{

char new [3] = "NEW";

printf ("new='%.3s'\n", new);

return 0;

}

in C++:

int main (void)

{

char newstr [4] = "NEW";

printf ("new='%.3s'\n", newstr);

return 0;

}