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 difference between linear and circular queue?

What is the difference between linear and circular queue? In: http://wiki.answers.com/Q/FAQ/2545-37 [Edit categories]


The Queue by Default is Linear, it would be termed as Circular if the Last Element of the Queue pointsto the first element of the List

When is it necessary to use member wise initialization list in c plus plus?

Initialization lists makes a difference when we have objects as members. Instead of using default initialization followed by assignment, the initialization list can initialize the object to its final value. This can actually be noticeably faster.

When you need to initialize constant member, references and pass parameters to base class constructors, initialization list is the only choice. if you have members of class type with no default constructor available, initialization is the only way to construct your class.

There is only one way to initialize base class instances and non-static member variables and that is using the initializer list.

If you don't specify a base or non-static member variable in your constructor's initializer list then that member or base will either be default-initialized (if the member/base is a non-POD class type or array of non-POD class types) or left uninitialized otherwise.

Once the constructor body is entered, all bases or members will have been initialized or left uninitialized (i.e. they will have an indeterminate value). There is no opportunity in the constructor body to influence how they should be initialized.

You may be able to assign new values to members in the constructor body but it is not possible to assign to const members or members of class type which have been made non-assignable and it is not possible to rebind references.

For built in types and some user-defined types, assigning in the constructor body may have exactly the same effect as initializing with the same value in the initializer list.

If you fail to name a member or base in an initializer list and that entity is a reference, has class type with no accessible user-declared default constructor, is const qualified and has POD type or is a POD class type or array of POD class type containing a const qualified member (directly or indirectly) then the program is ill-formed.

C code programming to reverse a number?

/*The coding style used in this source code is for convenience.

* It is widely used style of coding. */

#include <stdio.h>

void main() {

int number, modulus, reverse;

reverse = 0;

printf("Enter a number \n");

scanf("%d", &number);

while(number != 0) {

modulus = number % 10;

reverse = (reverse * 10) + modulus;

number =number / 10;

}

printf("The reversed number is %d", reverse);

getch();

}

How much does C plus plus cost?

The express edition of Microsoft Visual C++ is free. The non express editions range in price between a few hundred dollars US to tens of thousands of dollars US depending on team integration and technical support.

Is it necessary to implement all the methods of abstract classes in derived class?

An Abstract class is a way to organize inheritance, sometimes it makes no since to implement every method in a class (i.e. a predator class), it may implement some to pass to its children but some methods cannot be implemented without knowing what the class will do (i.e. eat() in a Tiger class). Therefore, abstract classes are templates for future specific classes.

A disadvantage is that abstract classes cannot be instantiated, but most of the time it is logical not to create a object of an abstract class. heloooooo

What is an abstract datatype?

Abstract data type

A mathematical entity consisting of a set of values (the carrier set) and a collection of operations that manipulate them. For example, the Integer abstract data type consists of a carrier set containing the positive and negative whole numbers and 0, and a collection of operations manipulating these values, such as addition, subtraction, multiplication, equality comparison, and order comparison.

Abstraction

To abstract is to ignore some details of a thing in favor of others. Abstraction is important in problem solving because it allows problem solvers to focus on essential details while ignoring the inessential, thus simplifying the problem and bringing to attention those aspects of the problem involved in its solution. Abstract data types are important in computer science because they provide a clear and precise way to specify what data a program must manipulate, and how the program must manipulate its data, without regard to details about how data are represented or how operations are implemented. Once an abstract data type is understood and documented, it serves as a specification that programmers can use to guide their choice of data representation and operation implementation, and as a standard for ensuring program correctness.

Above retrieved from Answers.com

Viper1

Static and dynamic memory allocation in c plus plus?

dynamic memory allocation is that type of memory which create to allocate the memory on running time or at compile time by the function of malloc , calloc , realloc and free.

dynamic memory allocation is give the best utilization of memory which gives the sufficient use of memory.

In C plus plus What symbol is used to denote the declaration of a dynamic array?

We don't generally use dynamic arrays in C++, we use vectors instead. The problem with dynamic arrays is that we must keep track of how many elements are allocated in the array, and must manually manage the memory allocated to the array, just as we do in C. Vectors do this automatically for us and allow us to pass dynamic arrays into functions by reference. This ultimately makes it much easier to use a dynamic array without having to worry about how it is physically implemented.

Write a c plus plus program of array of size 10 and print the maximum value from it?

#include<iostream>

template<class T>

T max(T a[], size_t size)

{

T max=a[0];

for(size_t index=1; index<size; ++index)

if( max<a[index] )

max=a[index];

return(max);

}

int main()

{

int a[10]={6,3,1,8,4,0,9,2,5,7};

int max = max(a, 10);

// assert(max == 9);

}

What is the C plus plus code to print all ASCII codes and the equivalent characters?

Although character data types such as char are intrinsically numeric, whenever you print a char you automatically print the symbol associated with the character code (the char's value), never the code. In order to print the code you must cast the character to a numeric data type, such as int.

char c = 'A'; // ASCII value 65 decimal (0x41)

std::cout << static_cast<int>(c); // puts the value 65 on std::cout

How to write an effective program using c plus plus?

Here is a simple program that will tell you how to make an algorithm:

int main();

{

int length;

int width;

int total;

printf("What is the width: ");

scanf("%d", &width);

printf("What is the length: ");

scanf("%d", &length);

total = width * 2 + 2 * length; /*Here is the algorithm for finding the perimeter of a square*/

printf("The perimeter is: %d", total);

return 0;

}

Output:

What is the width: 32

What is the length: 55

The perimeter is: 174

How do you print 1 to 10 numbers using while loop in C?

#include <iostream>

int main()

{

int i=0;

while( i<10 )

printf( "%d\n", ++i );

return( 0 );

}

What is the difference between null and void pointers?

A void pointer is a pointer that has no type information attached to it.

A null pointer is a pointer that points to "nothing". A null pointer can be of any type (void included, of course).

What is the difference between a class and a union in c plus plus?

The data members of a class are each allocated separate memory addresses. A union's members are all mapped to the same address. If you change the value of one union member, you change them all.

What is memory leakage in c plus plus?

Memory leakage occurs when you allocate memory dynamically but do not keep track of the allocation. If you lose track of an allocation then you cannot release the memory back to the system until the program terminates. If you continually allocate without releasing, you not only waste a lot of memory unnecessarily, you could easily run out of memory altogether. The following code demonstrates this:

#include <iostream>

int main()

{

int* p=NULL;

do {

p=(int*)malloc(sizeof(int));

} while(p);

return(0);

}

In the above code, p is continually allocated memory which is not released before the next allocation. The program will eventually exit when you run out of memory, releasing all memory back to the system, but you should never code like this. Always release memory as soon as it is no longer required.

#include <iostream>

int main()

{

int* p=(int*)malloc(sizeof(int));

while(p)

{

delete(p); // release memory

p=(int*)malloc(sizeof(int));

}

return(0);

}

This time the program will never terminate so long as p is non-NULL. Although the program may never exit, memory is no longer leaking.

Obviously these programs are merely demonstrations and not useful in any way (we're not actually using the memory we allocate). In real applications, however, you will always delete pointers as soon as you're finished with them and especially when you wish to re-use a pointer, as per the second example.

So long as you maintain at least one pointer to allocated memory you can return that memory back to the system as soon as it is no longer required. If you fail to keep track of allocated memory, leakage is only to be expected. Even if it doesn't cause any problems, as per the first example, it is bad programming practice to allow any pointer to allocated memory to fall from scope.

Why not you simply put the decimal integer into the original string as compared to the format specifier?

You can certainly do that ...

printf ("This is a number: 12345\n");

... but that does not have the same value as placing the value in a variable and converting the variable into a string ...

int i = 12345;

printf ("This is a number: %d\n", i);

That's the whole point of format specifiers - to initiate a conversion from one place to another.

Features of constructor of a derived class in c plus plus?

class base

{

base():m_data(0){}

base(int i):m_data(i){}

base(const base& b):m_data(b.m_data){}

private:

int m_data;

};

class derived : public base

{

derived():base(){} // call base class default constructor

derived(int i):base(i){} // call base class overloaded constructor

derived(const derived& d):base(d){} // call base class copy constructor

};

When is a friend function compulsory?

A normal member function has the following qualities:

1. Has private access to the class.

2. Is scoped to the class.

3. Can be invoked on an instance of the class.

Static functions have the first two qualities only. Friend functions have the first quality only.

It therefore follows that friend functions are only compulsory when you need quality 1 only.

What are the syntax for one dimensional array in c plus plus?

type_of_data array_name[number_of_elements_ in_array];

For instance,

int myArray[10];

It creates array of type int, and array consists of 10 elements.

What are the different types of function in c plus plus programming?

There are five types of functions and they are:

  1. Functions with no arguments and no return values.
  2. Functions with arguments and no return values.
  3. Functions with arguments and return values.
  4. Functions that return multiple values.
  5. Functions with no arguments and return values.

Functions with no arguments and no return value.

A C function without any arguments means you cannot pass data (values like int, char etc) to the called function. Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C. This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.

Functions with arguments and no return value.A C function with arguments can perform much better than previous function type. This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments. Functions with arguments and return value.This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function. And this type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value. The data returned by the function can be used later in our program for further calculations. Functions with no arguments but returns value.We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful. The best example of this type of function is "getchar()" library function which is declared in the header file "stdio.h". We can declare a similar library function of own. Functions that return multiple values.So far, we have learned and seen that in a function, return statement was able to return only single value. That is because; a return statement can return only one value. But if we want to send back more than one value then how we could do this?

We have used arguments to send values to the called function, in the same way we can also use arguments to send back information to the calling function. The arguments that are used to send back data are called Output Parameters.

It is a bit difficult for novice because this type of function uses pointer

What is function overridding in c?

Function overriding applies to class methods and is only applicable within a derived class. Derived classes inherit all the public and protected members of their base classes, and all inherited methods can be overridden by the derived class to provide implementations that are specific to the derivative. The base class implementations should be thought of as being more generalised implementations that are specific only to the base class itself. However, some or all of the base class implementations may well be sufficient for your derivative in which case there is no need to override those methods at all. You only need to override methods that require more specialised implementation.

Methods that are declared virtual within the base class are expected to be overridden by the derived class. By contrast, all pure-virtual methods must be overridden otherwise the derived class, like its base class, is rendered abstract. You cannot instantiate an abstract class -- it is intended to be a conceptual object (like a shape) rather than an actual object (like a square or a circle).

You may also override non-virtual methods, however the fact they are non-virtual in the first place means that, barring an oversight on the part of the base class designer, it is not expected that you do so. That doesn't mean that you cannot override them, but it does mean that the base class methods (including all overloaded version of the override) are effectively hidden from the derived class. Often this would be undesirable but, occasionally, that's exactly what you want.

Base classes cannot foresee what classes you might derive from them in the future. Fortunately they do not need to know anything about those classes (if they did, you'd have to continually modify the base class every time you created a new derivative). By declaring methods to be virtual, the virtual table ensures that all derived classes will "do the right thing" even when those methods are called implicitly from within the base class implementations. As a result, there is no need for a base class to ever know the runtime information of its derived classes (and if there is, you'll immediately know there's something very wrong with your class design).

Often an override simply needs to augment a base class method rather than override it completely. This is achieved by calling the base class method explicitly from anywhere within the overridden implementation. This reduces the need to duplicate otherwise functional code that already exists in the base class. It should be noted that base class methods should never be called explicitly from within the base class itself; the override is always expected to be called (implicitly) and this behaviour should only ever be over-ruled by the derivative. Calling a base class method explicitly from outwith the derived class may cause unwanted side-effects because the derived class will no longer "do the right thing".

Pure-virtual methods of a base class may or may not provide an implementation, but either way you must provide an implementation within your override. If calling the base class method explicitly causes a compile time error (stating no such method exists) then the base class provides no implementation at all, therefore you are expected to provide a complete implementation. As an example, a shape class may have a pure-virtual Draw() method but it cannot implement it without knowing what type of shape it is. Whereas a circle class that is derived from the shape class will always know how to draw itself and must therefore provide the complete implementation of the Draw() method.

Overridden virtual methods come into their own when dealing with collections of objects. The objects themselves needn't be of the same type, but so long as they all share a common base class, you can build a collection from the base class alone (all the objects have an "is-a" relationship with their common base class). The collection itself is only concerned with what the base class is actually capable of, as defined by the base class interface, but the virtual table ensures that, regardless of the runtime type of the derived classes, every object in the collection exhibits the correct behaviour.

Write a c programm of insertion of element in the array?

#include<stdio.h>

#include<conio.h>

void main()

{

int a[20],i,j,n;

clrscr();

printf("Enter the no of element you want to insert in the array : ");

scanf("%d",&n);

printf(" Enter the Array element : ");

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

printf("The array is : ");

for(i=0;i<n;i++)

{

printf("%d",a[i]);

}

printf(" Enter the element which you want to insert");

scanf("%d",&j);

printf("The array is : ");

for(i=0;i<n+1;i++)

{

if(i==n)

a[i]=j;

printf("%d",a[i]);

}

getch();

}

What is nested class and what is its use in c plus plus?

A nested structure is simply one structure inside another. The inner structure is local to the enclosing structure.

struct A { struct B {}; };

Here, we can instantiate an instance of A as normal.

A a;

But to instantiate B we must qualify the type because it is local to A:

A::B b;

If B were only required by A, then we can prevent users from instantiating instances of B simply by declaring it private:

struct A { private: struct B {}; };

What is the method used to implement an if else statement in C?

The else statement is an optional part of an if statement that is executed if the primary if condition is false.

if (condition) true_statementelse false_statement