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 you can use while loop in place of select case?

While is NOT a replacement for SWITCH -- CASE

However , still if this is the requirement then , you can do this :

While (1)

{

if (case1 ) {}

if (case2 ) {}

:

:

:

if (case n ) {}

if (case default ) {}

} //end of while loop

Program of swapping two no?

There are several ways to do this in C:

// nums to swap

int a = 5;

int b = 10;

// using a temporary variable - often the fastest method

int temp;

temp = a;

a = b;

b = temp;

// using addition (no additional storage needed)

a = a + b;

b = a - b;

a = a - b;

// using xor (no additional storage needed)

a = a^b;

b = a^b;

a = a^b;

// using only one line (no additional storage needed) // note, this appears to be a compiler-dependent way to swap variables

a = a + b - (b=a);

Can the parameter of the copy constructor be passed by value?

No. The parameter of the copy constructor is always passed by reference, specifically, a const reference to the class.

Think about it. If it were passed by value, then the compiler would already know how to copy the object into the formal parameter, but the purpose of the copy constructor is to provide the code to copy the object, so its kind of a "cart before the horse" thing to think about call by value here.

What is meant by interface of a class and implementation of a class in c plus plus?

The interface of a class is defined as being the methods that allow the outside world to interact with objects instantiated from that particular class type. That interface may be declared within the class itself or it may be inherited from base classes, or it may be some combination of the two. The interface may also be virtual or even pure-virtual, either in whole or in part.

The implementation determines how that interface operates. Although the interface and the implementation go hand-in-hand they are treated separately because a derived class can override the generic interface exposed by the base class in order to provide a more specialised implementation of the base class interface, as well as provide new interfaces of its own.

The generic interface needn't be virtual in order to be overridden, but it must be virtual in order to allow polymorphic behaviour. Where the base class cannot provide an implementation because it is intentionally abstract and therefore relies solely upon a derived implementation, it will provide a pure-virtual interface that absolutely must be implemented by all derivatives. If a class declares or inherits a pure-virtual interface with no implementation, that class automatically becomes abstract. Only classes that provide a complete implementation for all pure-virtual interfaces (including any generic implementations inherited from base classes derived from the base class), can actually be instantiated.

When calling a base class method from a non-member function, one would rightly expect the implementation of the derived class to be executed. In order to achieve this, the generic interface must be virtual and the virtual table ensures the call is routed to the correct implementation. Without this, only the base class method can be called, which may result in unexpected behaviour in the derived class since the base class would be unaware of its specific behaviour.

While it is possible to allow a base class to gain access to a derived class interface without exposing a virtual interface, this can only be done when the base class already knows exactly what type of derivative to expect. Knowing the type of its derivative allows the base class to make an explicit dynamic cast of itself in order to call the appropriate interface. But it cannot predict the future: if you derive a new class of object that it knowns nothing about, it cannot gain access to its specialised interface. Even dynamic casting is impossible without knowing what type of object to cast to. Virtual interfaces overcome this problem by ensuring that no matter what derivatives are created either now or in the future, the base class can access each of their specific implementations through just one generic interface, without the need for any dynamic casting (which is almost always a sign of poor design).

When a base class interface is routed to a derived class implementation, that specific implementation has complete access to the derived class' interface. Thus interfaces that the base class doesn't even know exist can be implemented via calls through the base class' virtual interface which is the only interface the base class really needs to know anything about. To put it another way, all derivatives of the same base class can be treated just as if they were the base class itself, with a common, generic interface, and yet each instance can exhibit entirely different behaviour, according to the specific implementations defined by each derivative. That is, one interface/multiple implementations.

Derived class implementations may also, optionally, call any base class method and access any base class variable (other than the base class' private members, of course). This makes it possible to provide a generic implementation of the virtual interface that can then be augmented or replaced completely by the more specific implementations of the derived interface. Even pure-virtual methods, which are normally not implemented in an abstract base class, can provide a generic implementation where appropriate. Although the implementation cannot be inherited, the interface must be inherited because it is virtual. However, once a derived class implements a pure-virtual interface, that implementation can then be inherited by its derivatives, to be further specialised and or augmented as necessary.

Program for derived class in cpp?

#include<iostream>

class base

{

int m_data;

public:

base(const int data):m_data(data){}

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

base& operator=(const int rhs){m_data=rhs;return(*this);}

base& operator=(const base& rhs){m_data=rhs.m_data;return(*this);}

virtual ~base(){}

};

class derived

{

public:

derived(const int data):base(data){}

derived(const derived& cpy):base(cpy){}

derived& operator=(const int rhs){return(base::operator=(rhs));}

derived& operator=(const derived& rhs){return(base::operator=(rhs));}

virtual ~derived(){}

};

int main()

{

derived d=42;

}

Which header file must be included to use the function pow?

The std::pow() function can be found in the <cmath> header.

What is CIN?

cin is the object of istream class i.e, input class.

Where to use polymorphism?

Polymorphism is used whenever you wish to achieve specific behaviour from a generic object, where the object's actual type may not be known or would be impossible to determine at compile time. By declaring virtual methods in the generic type (the base class), and overriding them in the derived type, you ensure that the derived object does "the right thing" regardless of its actual type, and regardless of whether the method is called directly or indirectly via the base class. With polymorphism, there is no need to determine the actual runtime type of the object, you get that for free simply by calling the appropriate virtual methods.

What is sort in programming?

Sorting in programming is the process of arranging elements in some prescribed order.

An example might be, given arrays of people's names and birthdates, to sort them by birthdate.

How do you invoke function result () takes two float argument and returns an integer in c plus plus program?

You can do this by creating a forwarddeclaration of the function. You can call the forward drclared function inside the main to use it.


int result(float num1, float num2);


int

main(void)

{

int value = result(3.14, 2.74);

return (0);

}


int

result(float num1, float num2)

{

int value = 0;

// function codes goes here

// you can alter the value of variable 'value'

return (value);

}


The returning value of the 'result()' function is assigned to variable 'value' in 'main()'.

What is the function of the shift plus tab?

<Shift><Tab> takes you backwards through the tab order instead of forwards.

How do DriveSpy specify file headers?

The file header section conatins the hexidecimal number values for many knownfile types. These hexidecimal numbers are the header data contained in the first several bytesof all specialized data files, such as Microsoft word documents or excel spreadsheets and any associated templates. The file header uniquely identifies the file type.

You can use the file header information in Drivespy to search for specific files that might have had their extensions changed.

Which type of programming error leads to garbage stink value in a c plus plus program?

The most common error in C/C++ is a buffer overrun. Even if no problems arise through normal use, hackers can exploit the overrun to inject their code into yours. While this can highlight the problem, not all hackers have good intentions...

Another common error is to assign instead of compare a value, due to the similarity between the operators. The compiler can't always see the problem and replicating the problem at runtime may not be obvious.

if( x = y) // should be x == y

{

// always executes

}

else

{

// never executes

}

What is the conversion 77f to c?

To convert F to C, subtract 32, then multiply by 5/9

77-32 = 45

45x5/9 = 25

Going the other way, to convert C to F

Multiply by 9/5, then add 32

How object take place in memory in case of oops?

Objects are stored in memory according to their members. They are very similar to struct types, insofar as the total memory assigned to an object is equal to the total size of all its members, plus any padding required for member alignment. If the object contains pointers, the memory allocated to those pointers will reside elsewhere, outside of the object, regardless of whether that memory belongs to the object or not (it is not included in the object's memory footprint). Additional memory is also set aside for the v-table if the class contains any virtual methods.

What is meant by compile time operator in c?

A compile time operator is an operator involved in an expression where the result is known at compile time. An example is the expression 1 + 2 * 4. Since 1, 2, and 4 are literal expressions, the result is known at compile time, and there is no need to generate code to evaluate it. The compiler is free to substitute the expression 9 in place of the expression 1 + 2 * 4. Don't bother doing it yourself - sometimes the clarity of writing what you are trying to accomplish makes for more well documented code.

If you do not have such a compiler, it is high time that you upgrade.

What is functions in c?

A function is a small set of instructions designed to operate on its given input (aka parameters or arguments) and perform some action or return some output. Generally in programming a commonly used strategy is to take a large program and break it into smaller chunks, which are turned into functions.

So say that you are writing a large program and constantly have to see which of two numbers is larger, you could write a function:

int larger(int a, int b)

{

if(a > b)

return a;

else

return b;

}

Now in your program, you can simply say:

int x = larger(number1, number2);

What is the importance of Swapping two variables?

Suppose we ask the user to enter a range with an upper and lower limit. We would naturally expect the upper limit to be higher in value than the lower limit. If this is not the case, we can either reject the values and ask for new input or we can simply swap them.

if we have an array of unsorted values and we wish to sort them, we need to exchange values. In order to exchange any two values we must swap them.

Can a derived class pointer be converted into a pointer to its public base class?

Yes, via a static up-cast. Up-casting to a base class is always safe so there's no need to dynamically cast (dynamic casting is only a requirement when down-casting, but even then that's usually a sign of a poorly-designed virtual interface).

Consider the following classes:

class A{};

class B : public class A{};

If we create an instance of B we can then point at that instance using a derived class pointer:

B b;

B* pb = &b;

To "convert" pb to a base class pointer we statically cast it like so:

A* pa = (A*) pb;

Or, more simply, we can just point at the derivative's address:

A* pa = &b;

Note that we don't actually convert anything. Although pa points to the base class, the actual instance we're pointing at is still a derived class. This is what enables us to pass pointers and references to derived classes into functions that actually expect pointers or references to base classes. Even though those functions can have no prior knowledge of what derivatives it might receive in the future, they simply don't have to. As far as the functions are concerned, you've passed a base class, not a derivative. But if you call a virtual method of the base class then the derived class method will be called instead -- and that's precisely what one expects of a derivative. This is how inheritance and polymorphism work.

If you really want to convert to a base class (completely eliminating the derived class), then you must call the base class copy constructor, passing the derived class as the argument:

B b;

A a(b);

Note that a and b are now completely separate instances and a is nothing more than the base class component of b, devoid of all its derived class components. Thus if you were to mutate a, you will not mutate b, and vice versa. The casts shown previously do not create separate instances -- there is only the one instance -- so any mutations affect all pointers to that one instance, whether they be base class pointers or derived class pointers.

You can also use the copy constructor to point at a new instance of the base class, creating the new instance on the heap rather than the stack:

A* pa = new A(b);

In order to make use of A's assignment operator, you must provide a cast operator in B:

class B : public A

{

public:

operator A(){return(A(*this));}

};

Now you can perform the following assignment:

A a;

B b;

a = b;

Note that a and b remain completely separate instances.

Why can't you use a equals equals sign for a for statement?

This answer applies to programming languages in general. The equals sign is used for assignment. For example, the code "$variable = 1" assigns the value of 1 to the variable $variable. If you want to test whether values are the same (equality) the double equals sign is used. For example, when you want to verify that $variable has been assigned the value of 1 you would use: $variable == 1