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];
}
What is the difference between base class pointer and derived class pointer in c plus plus?
Your understanding of is a in C++ (polymorphism, in general) is wrong.
A is B means A has at least the properties of B, possibly more, by definition.
This is compatible with your statements that a Dog has a Pet and that [the attributes of] a Pet is[are] a subset of [attributes] of Dog.
It's a matter of definition of polymorphism and inheritance. The diagrams you draw are aligned with the in-memory representation of instances of Pet and Dog, but are misleading in the way you interpret them.
Pet* p = new Dog;
The pointer p is defined to point to any Pet-compatible object, which in C++, is any subtype of Pet (Note: Pet is a subtype of itself by definition). The runtime is assured that, when the object behind p is accessed, it will contain whatever a Pet is expected to contain, and possibly more. The possibly more pat is what the Dog in your diagram is. The way you draw your diagram lends to a misleading interpretation.
Think of the layout of class-specific members in memory:
Pet: [pet data]
Dog: [pet data][dog data]
Cat: [pet data][cat data]
Now, whenever Pet *p points to, is required to have the [pet data] part, and optionally, anything else. From the above listing, Pet *p may point to any of the three. As long you use Pet *p to access the objects, you may only access the [pet data], because you don't know what, if anything, is afterwards. It's a contract that says This is at least a Pet, maybe more.
Whatever Dog *d points to, must have the [pet data] and [dog data]. So the only object in memory it may point to, above, is the dog. Conversely, through Dog *d, you may access both [pet data] and [dog data]. Similar for the Cat.
Let's interpret the declarations you are confused about:
Pet* p = new Dog; // [1] - allowed!
Dog* d = new Pet; // [2] - not allowed without explicit casting!
My understanding is that 1 should not be allowed without warnings because there is no way a pointer should be able to point to an object of its superset's type (Dog object is a superset of Pet) simply because Pet does not know anything about the new members that Dog might have declared (the Dog - Pet subset in the diagram above).
The pointer p expects to find [pet data] at the location it points to. Since the right-hand-side is a Dog, and every Dog object has [pet data] in front of its [dog data], pointing to an object of type Dog is perfectly okay.
The compiler doesn't know what else is behind the pointer, and this is why you cannot access [dog data] through p.
The declaration is allowed because the presence of [pet data] can be guaranteed by the compiler at compile-time. (this statement is obviously simplified from reality, to fit your problem description)
1 is equivalent of an int* trying to point to a double object!
There is no such subtype relationship between int and double, as is between Dog and Pet in C++. Try not to mix these into the discussion, because they are different: you cast between values of int and double ((int) double is explicit, (double) int is implicit), you cannot cast between pointers to them. Just forget this comparison.
As to [2]: the declaration states "d points to an object that has [pet data] and [dog data], possibly more." But you are allocating only [pet data], so the compiler tells you you cannot do this.
In fact, the compiler cannot guarantee whether this is okay and it refuses to compile. There are legitimate situations where the compiler refuses to compile, but you, the programmer, know better. That's what static_cast and dynamic_cast are for. The simplest example in our context is:
d = p; // won't compile
d = static_cast<Dog *>(p); // [3]
d = dynamic_cast<Dog *>(p); // [4]
[3] will succeed always and lead to possibly hard-to-track bugs if p is not really a Dog.
[4] will will return NULL if p is not really a Dog.
I warmly suggest trying these casts out to see what you get. You should get garbage for [dog data] from the static_cast and a NULL pointer for the dynamic_cast, assuming RTTI is enabled.
Why ternary operator is not overloaded?
The ternary operator (known as the conditional operator in C++) cannot be overloaded because it is impossible to pass a test operand and two expression operands (either or both of which may be comma-separated) to a function. You can only pass values or references as arguments to a function. Even if it were possible, built-in functions and operators that rely on the conditional operator would likely break. Like all the other operators that cannot be overloaded (sizeof, typeid, ::, . and .*) the results must always be predictable because built-in operators and functions rely on them so heavily.
Is cin a function or object in c?
cin is an object........ An Example is // By Codex
#include <iostream> using namespace std; int main(){ int age;
cout << "How Old Are You?\n";
cin >> age;
cout << "You are << age << years old\n";
system("pause")
return 0;
}