When do we make a virtual function pure?
We make a virtual function pure whenever we wish to make our class an abstract base class (an abstract data type). Unlike a virtual function, pure virtual functions must be overridden by a derived class or by one of its derivatives (the function remains pure virtual until it is overridden, at which point it becomes virtual). Derived classes that do not provide a complete implementation for all the pure virtual functions it inherits become abstract themselves. You cannot instantiate an abstract base class other than through derivation.
To find the largest element of an array in c plus plus?
int GetMaxElement( void * array)
{
if (array != 0)
{
return(max(array[], typeof(array)));
}
return(0);
}
Is c or c a procedural oriented language?
One definition of a "procedural programming language" is a language that is used to describe how a program should accomplish the task it is to perform. This is in opposition to a "declarative programming language" that is used to describe what the program should accomplish rather than how it accomplishes the task.
What is the C plus plus program for the multiplication of two matrices?
class complex {
private:
double real;
double imaginary;
public:
complex() {...} // constructor, etc.
operator+(const& complex a) { // operator plus
this->real += a.real;
this->imaginary += a.imaginary;
}
}
How does an inline function differ from a preprocessor macro in c?
In order to get the best of both worlds, C++ introduced the inline keyword. By specifying that a function is inline, the compiler will take the function you have written and basically replace the function call that would have been generated with the function itself. In other words, using an inline function is pretty much directly putting your code there, except it's prettier and more maintainable.
Overuse of inlining function will cause bloated code for a very marginal increase in speed, if any. Inline functions are best suited for small quick functions. Also note that the inline keyword is a request to inline a function and the compiler may not necessarily honor that request. One example would be attempting to inline a recursive function.
In other languages like Java and C#, you are not able to specify what is inlined and what is not. The compiler will automatically make that decision without any input from the programmer.
Who is the owner of c plus plus language?
Bjarne Stroustrup is the author of C++. However, no one "owns" this language.
How do you write a c plus plus program to calculate the sum of squares using a function?
/**recursive function to find square root of a double to a precision of
maxDepth = 10 decimal places
returns -1.0 when given a negative number*/
#define maxDepth 10
/*firstly find the rational part*/
double getSqrt(double numIn){
/*cant take the square root of a negitive,
and a square root should not be negative
so return -1*/
int candidate = 0;
if (numIn <0)
return -1.0;
/*try every integer until you get one whose square is higher than
the number required, and this clearly one more than the
integer part of your square root*/
do{
if (candidate *candidate *1.0 numIn)
return testVal;
if (testVal * testVal >numIn)
/*most square roots are irrational, and theirfore a maximum number of recursions
must be set, otherwise infinite recursion will occur*/
if (myDepth <maxDepth)
return getIrrational(numIn, testVal-1/10^myDepth, myDepth +1);
else
return testVal-1/10^myDepth;
}
//this can probably be improved on in terms of conciseness, but the logic is the only //way to find a sqrt without going into calculous
How do you write a C plus plus program to find the first 5 multiples of a number?
#include<iostream>
#include<vector>
std::vector<size_t> multiples(size_t const num, const size_t terms)
{
std::vector<size_t> result;
size_t term=0;
while (++term<=terms)
result.push_back (num*term);
return result;
}
int main()
{
const std::vector<size_t> mults = multiples (42, 5);
std::cout << "The first 5 multiples of 42 are:";
for (auto value : mults) std::cout << '\t' << value;
std::cout << std::endl;
}
How many keywords in c plus plus and what are they?
The const keyword is the antonym of variable. While a variable is a volatile identifier and can change its value at any time, a constant identifier is non-volatile and must be assigned a value at the point of instantiation. Whenever you declare a constant you are allowing the compiler to assure you that the value will never change, whether purposely or by programming error. By allowing the compiler to assure constance, you do not need to manually check if the value has been altered. As with many other things in C++, the more responsibility you hand to the compiler, the more robust your programs will be.
What is the use of INLINE function?
it is the function declared inside the class. It is used for gaining faster speed in execution but it might give back the larger executable file
An inline function is one that is not called, its code is inserted in the position where you make a call to it. If it is called multiple times from different locations it was be inserted in all locations, increasing the size of all functions its called from but marginally increasing speed.
Take this example:
inline int AddNumbers(int a,int b)
{
return a+b;
}
int main()
{
int x = 2;
int y = 3;
int answer;
answer = AddNumbers(x,y);
answer = AddNumbers(answer,y);
return 0;
}
When the program is compiled it would look more like this:
int main()
{
int x = 2;
int y = 3;
int answer = x+y;
answer += y;
return 0;
}
Write a c plus plus program of tower of hanoi?
#include<stdio.h>
#include<conio.h>
void main()
{
int *d,*s,i,n,dir;
clrscr();
printf("Enter the number of disk\n");
scanf("%d",&n);
dir=n&1;
for(i=0;i<=n+1;i++)
{
d[i]=1;s[i]=i+1;
}
for(;;)
{
i=s[0];
if(i>n)
break;
printf("Move disk %d from tower%d to tower%d\n",i,d[i]=(d[i]+(i&1?dir:1-dir))%3+1,d[i]);
s[0]=1;
s[i-1]=s[i];
s[i]=i+1;
}
getch();
}
Advantages of new operator over malloc function in c?
1: new operator automatically computes the size of the data object. You need not use the operator sizeof.
2: new operator automatically returns the correct pointer type, so that there is no need to use a type cast.
3: it is possible to initialize the object while creating the memory space.
4: Like any other operator, new and delete can be overloaded.
How do you differentiate between a member function and normal function?
A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.
A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:
Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.
Why do you member functions defined outside the class?
A member function of a class can be defined outside the class using scope resolution :: operator
Thanks
Rajneesh
When comparing programming languages what is commonly used by all software developers?
There are many factors when it comes to choosing a programming language, primarily dependant upon the expected performance, time and budget. Even before a language is chosen, much time must be spent designing the program and often the design will determine the best language to use. in some cases it may be beneficial to modify an existing language to suit the task at hand, particularly with embedded systems.
Where performance is critical, programmers typically choose assembler, however this is usually limited to a small number of key routines since assembly programming is tedious and error-prone. C is typically used for a lot of low-level programming as C instructions can be mapped 1:1 with assembly. C++ is often used as well, as it allows any combination of low-level programming (including inline assembly), C-style programming and high-level abstraction through object-oriented programming. Indeed, the majority of operating systems, embedded systems and all major application software are written in C++ to some degree. C++ can also be found in areas where it was never specifically intended, such as advanced mathematics and theoretical physics.
Although C++ is a cross-platform language, the source code must be recompiled upon each target platform. To achieve this the code needs to be as generic as possible. In particular, generic audio and graphic libraries can help minimise development costs, but where there are differences between platforms, preprocessor directives can be used to filter the appropriate code in the absence of a generic library.
Where performance is not critical, high-level generic languages such as Java can speed up development times and thus reduce costs. Although Java is a compiled language it compiles to byte code rather than machine code. While the byte code will execute on any machine with a suitable Java Virtual Machine (and is therefore extremely portable), the need for interpretation of the byte code to machine code impacts upon performance compared to an equivalent C++ implementation. This also precludes Java from all low-level software development, including operating system kernels, drivers and embedded systems; it is intended purely for applications development.
Although C++ and Java dominate the bulk of application development, areas such as artificial intelligence often require languages that are better suited to the task, such as Lisp.
How arrays must be handled in c plus plus?
If the array is static it can declared in the structure itself:
struct myArrayTag
{
int num[12]; // array of 12 integers (e.g., 48 bytes).
} myArray;
If it is dynamic then you must use a pointer and allocate the array outside the structure. You should also maintain a variable in the structure to keep track of how many elements the array currently has:
struct myBufferTag
{
int * array; // Pointer to array of integers.
int size; // Size of array (number of elements);
} myBuffer;
Is constructors throws exception in C plus plus?
We must throw an exception from constructor in C++, but remember that in case of exception in Ctor, destructor will not be executed and you may have memory leaks. SO make sure all the data members must clean up their mess own. e.g. use smart pointers so in case of excpetion memory will released to free storage.
How do you resolve ambiguity in multiple inheritance?
Use virtual base classes. It is best if the common base class has no member variables and all member methods are pure-virtual. The multiple-inheritance class must provide all the implementation for the pure-virtual methods, even if the direct bass classes also provide their own implementations.
How do you pass the address of the object as function argument in c plus plus?
Use the address-of operator (&). Note that the function must accept a pointer to the class type. If it accepts a reference or value then you cannot pass the object's address, you must pass the object itself.
Difference between identifier and reserved word?
A keyword is a reserved word, used by the programming language to establish actions or commands. For example, in the line:
while (value < 100) {
//block of code
}
"while" is a keyword, used to indicate iteration (loop) of what's inside the block of code.
variables are user-defined words that are able to hold values. In the previous case, "value" can be thought as a variable.
Application of void data type in c plus plus programming language?
The voiddata type is used when a function doesn't return any value, and/or when it has no parameters at all. Pointer type 'void *' is a generic pointer.
A void pointer is used when it needs to be assigned to different data types later on in a program. Since it avoids type checking, void pointers should be used with care.
What is the difference between c plus plus and perl language?
Borland C++ was the successor to Turbo C++ and primarily included a better debugger. The product was later renamed Borland C++ Builder before Borland divided the company and passed all development systems to their CodeGear subsidiary, which produced CodeGear C++ Builder. However, all CodeGear products are now owned by Embarcadero Technologies, thus Embarcadero C++ Builder is the latest incarnation.
What is inheritance in visual c plus plus?
When you derive one class from another, the derived class inherits the sum of all the public and protected members exposed by the class it derives from. The underlying class is known as a base class and in the class hierarchy is an ancestor of the derived class. It is not necessary for the base class to know any of the details regarding its derivatives, as prudent use of virtual methods ensures the derived class acts correctly even when calling methods in the base class.
How much does a programming language generally cost?
Many programming languages, such as Java, are completely free to use, and being one of the major players in the programming language wars, as well as being cross-platform, in addition to being easy to learn, and in addition to it being free, many choose Java for their first language.
Another question: your question makes no sense: you cannot buy or sell programming languages.
C plus plus program that finds the minimum number?
template<typename T> size_t min(const T a[], const size_t size)
{
// assume first element (index 0) has the smallest value
size_t selected=0;
// scan remainder of array looking for anything smaller than selected
for (size_t right=selected+1; right<size; ++right)
if (a[right]<a[selected])
selected=right;
return a[selected];
}