Advantage and disadvantage of inline function in c plus plus?
The advantage of an inline function is that it eliminates the function calls completely, thus improving the performance wherever those calls appear. The disadvantage is that it increases code size, which can be detrimental to performance. For this reason, declaring a function for inline expansion is merely a hint to the compiler. If the increased code size would be detrimental, the compiler is free to ignore the inline declaration and retain the function call instead.
While the programmer is free to manually expand their own functions, this only serves to increase maintenance should the function ever need to be changed, and could lead to errors should those changes not be propagated correctly. The advantage of having a function, even if it is only ever called once (or from within a loop), is to simplify your code and make it easier to read and maintain. If the function is simple, or is called seldom, then it is a good candidate for expansion, but its almost always better to let the compiler decide which functions should be inline expanded.
Thousands! Programming languages number in the thousands, from general purpose programming languages such as C++, Java, and others, to special purpose languages which are used in one application. They can be ordered by type (structured, object-oriented, functional, etc.) or by history, or syntax. See the related list of programming languages.
What is namespace in c plus plus program?
A namespace is similar to a class in object oriented programming. A namespace contains functions defined by the programmer. for example namespace std contains functions like cout and cin.
namespaces can be globaly declared like so: "using namespace std;"
which includes all the functions located in the namespace std.
if you only need to use cout you can globaly declare only cout like this "using std::cout;"
or
std::cout<<"calling cout directly from namespace std";
you can make your own namespaces as well
namespace mynamespace;
void myfunction(){
code for function
}
and use it
using mynamespace::myfunction;
The main use of a namespace is to reduce or eliminate collisions with names that may be duplicated but have different functionality. For example, I may want to use an object with the name of 'cout', but that name already exists. If I place it in a different namespace I would be able to use it with that name.
C plus plus code to read the data from any file?
The following function will read any valid file name one byte at a time.
int read_file(char* filename)
{
FILE* input=fopen( filename,"rb" );
if( !input )
{
std::cerr<<"File access error: "<<filename<<std::endl;
return( -1 );
}
char ch;
while( fread( &ch, 1, 1, input )==1 )
{
// process the byte...
}
fclose( input );
return( 0 );
}
What is difference between c and pro c?
C is the computer language bases on K&R's language that has evolved over the years. It includes C++, as extended by Stroustroup..
Pro*C is an Oracle precompiler that will read a C or C++ program, detect Oracle extensions to it, and convert it to native code for subsequent processing by the C or C++ compiler. This involves building the data structures and function calls needed to use Oracle while passing the non-Oracle C or C++ code through intact. The amalgamated code is then processed by the C or C++ compiler and it now works with Oracle.
What are real life application of data structure?
In real life the importance of data structure is much more bcoz it providing a disconnected transaction between database & workstation
just take a example that when some one going to on line shopping at that time at first he/she wants to select the items to purchase simultaneously if the person donot want some selected items then he/she can also retrieve from the list,these all are done in data structure when you at last enter the submit button at that time this will retrieve from the database & enters in to sales list.
Means these all transaction are done in data structure there is no need to transect with data base by that transaction will be faster,secure,good performance,efficient etc
What is the definition of a base-class constructor?
A base class constructor is simply a constructor that is declared within a base class. There is nothing particularly special about them since all constructors are only of relevence and applicable to the classes in which they are declared.
Swap the value of two variables using call by value by c plus plus?
Lets start simple by swapping two int variables in a function call. You must use a & to pass the variables by reference so that when you edit their values the original variables get changed as well.
void swapVariables(int &var1, int &var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}
You could change this function to use any variable type that you want or you could use a template function instead so that the same function could be used with any variable type.
template <class T>
void swapVariables(T &var1, T &var2)
{
T temp = var1;
var1 = var2;
var2 = temp;
}
Another interesting thing you can do when swapping two variables is use an xor function to swap the two variables without using a temp variable.
void swapVariables(int &var1, int &var2)
{
var1 = var1^var2;
var2 = var1^var2;
var1 = var1^var2;
}
Which can also be simplified to:
void swapVariables(int &var1, int &var2)
{
var1 ^= var2 ^= var1^= var2;
}
Can a function be called from more than one place in a program in c plus plus?
In C and C++, as well as in many (all?) languages, a function can be called from more than one place in a program. That's the purpose of functions - to encapsulate pieces of code that are needed in more than one place in the program.
Can we write a c plus plus program without objects?
Since you can create programs in C plus plus (C++) without creating any object you can call the C++ a semi-object-oriented programming language. The C++ programming language was first released in 1983 and it was designed by Bjarne Stroustrup.
How do you return by reference in C plus plus?
Returning by reference implies that the reference will not fall from scope when the function returns (because references must never be NULL). Thus you cannot return a reference to a variable that was declared local to the function.
int& foo()
{
int local;
return(( int & ) local); // Error: cannot return a reference to a local variable.
}
However, you can return a reference to a parameter that was itself passed by reference, since that reference is guaranteed to exist (it can't be NULL).
const int& GetMax( const int& byRef1, const int& byRef2 )
{
return( byRef1 > byRef2 ? byRef1 : byRef2 ); // this is fine.
}
Generally, the only time you will return by reference is when returning a class instance member or a reference to the class instance itself, since they always remain in scope after the function call.
class bar
{
public:
bar():m_num(0){}
const int& GetIntRef()const{return((const int&)m_num);} // constant reference to instance member.
int& GetIntRef(){return((int&)m_num);} // non-constant reference to instance member.
const bar& AsRef()const( return((const bar& )*this );} // constant reference to this instance.
bar& AsRef()( return((bar&)*this );} // non-constant reference to this instance.
private:
int m_num;
};
Note that use of const is not obligatory when returning references. However, when values must not be changed, it's good policy to enlist the help of the compiler wherever possible. The previous example returns both constant and non-constant references. The function that makes a call to GetIntRef() or AsRef() will determine whether the returned reference should be constant or not. By implementing both methods, we cater for both scenarios.
C plus plus program to list all Armstrong number?
#include<iostream>
#include<vector>
unsigned count_digits (unsigned num, const unsigned base=10)
{
unsigned count=1;
while (num/=base) ++count;
return count;
}
class number
{
std::vector<unsigned> value;
unsigned base;
public:
number (const unsigned _value, const unsigned _base=10): value {}, base {_base} { *this = _value; }
number& operator= (const unsigned _value);
operator unsigned () const;
bool is_narcissistic () const;
};
number& number::operator= (unsigned _value)
{
unsigned count = count_digits (_value, base);
value.resize (count);
while (count)
{
value[value.size()-count--] = _value%base;
_value/=base;
}
return *this;
}
number::operator unsigned () const
{
unsigned num = 0;
for (unsigned index=0; index<value.size(); ++index)
num += value[index]*static_cast<unsigned>(std::pow (base, index));
return num;
}
bool number::is_narcissistic () const
{
unsigned num = 0;
for (unsigned index=0; index<value.size(); ++index)
num += static_cast<unsigned>(std::pow (value[index], value.size()));
return num == static_cast<unsigned> (*this);
}
unsigned main()
{
const unsigned min=1;
const unsigned max=100;
std::cout << "Narcissistic numbers in the range " << min << " through " << max << ":\n\t";
for (unsigned n=min; n<=max; ++n)
if (number(n).is_narcissistic())
std::cout << n << ' ';
std::cout << '\n' << std::endl;
}
What is modularization in c plus plus?
Modular design is the process in which you take a large problem and break it down into smaller problems, and address each smaller problem individually. In terms of C programming, it would mean taking a large programming task and breaking it down into several smaller logically separate tasks. Each of these smaller tasks would most likely be represented as a function in the program.
What are the disadvantages of bubble sort?
The advantage of sorting is that it is quicker and easier to find things when those things are organised in some way. The disadvantage is that it takes time to sort those things. In computing terms, the cost of searching a few unsorted items is minimal, but when searching a large number of items (millions or perhaps billions of items), every search will have an unacceptable cost which can only be minimised by initially sorting those items. The cost of that sorting process is far outweighed by the speed with which we can subsequently locate items. Inserting new elements into a sorted set also incurs a cost, but no more than the cost of searching for an element, the only difference is we search for the insertion point rather than a specific element.
What punctuation ends most lines of C and C plus plus code?
C programs are composed from data types and functions. Functions are composed from one or more statements. A statement is composed from one or more expressions terminated by a semi-colon. A semi-colon without an expression is itself a statement (an empty statement).
Every C program must have at least one function, the global main function. This serves as the entry-point of the application. When we return from the main function, the program terminates normally.
The C standard requires that the global main function return an integer to the execution environment, where the value 0 is conventionally used to indicate "no error". As such, the minimum C program is as follows:
int main (void) {
return 0;
}
What is function calling in c plus plus?
Function calling is where your code branches off to execute a function and then returns to the instruction following the call. The function may also return a value that can be stored and/or processed by the code that called it. Functions allow common code to be separated from the code that uses the common code, thus reducing maintenance (the code in the function is written once, rather than every time it is required).
How can you use abstract classes instead of interfaces?
In most cases, you will want to use abstract classes IN ADDITION to interfaces.
You should use an abstract class in the following circumstance:
In practice, abstract classes are a good way to collect common code into one place, to make maintenance easier.
For instance, say you have a class and interface structure like this:
Class A
Interface X
Class B extends A implements X
Class C extends A implements X
Both B and C will have the all the methods declared in X; if the implementation of those methods is the same (or can be made the same), then X is a good candidate for changing to an abstract method:
Class A
Abstract Class X extends A
Class B extends X
Class C extends X
Thus, you have removed the code duplication that was happening when using interfaces.
Note that doing the above is NOT a good idea if any class which implement interface X cannot be made a subclass of the new abstract class X.
What is the difference between a null pointer and a null macro?
I'll assume that you're asking your question for C type language programming. A NULL pointer is a pointer that's guarnteed to point to nothing. This may be 0 in a UNIX/Linux system or some other address in another system. Using the NULL macro to set/initialize your pointers will make your programs more portable among systems than using something like the 0.
#include
char *c = 0; // initialize to NULL--not portable
char *p = NULL; // initialize to NULL as defined in stdio is portable
First work out the algorithm for what you want to do and then encode that using the C++ language.
Alternative Answer
In this case, the algorithm will consist of a loop that counts from 1 to 1000, testing each value to see if it can be divided by 3 or 5 without leaving a remainder. If so, it gets added to a running total. When the loop ends, the running total is the final answer.
To determine a remainder in C++, we use the modulo operator (%). That is; 9%5 is 4 because 9 divided by 5 is 1 remainder 4, whereas 9%3 is zero. Therefore if either operation evaluates to zero, the value can be added to the running total.
int DoSum( void )
{
int total=0;
for(int x=1; x<1000; ++x)
if( !(x%3) !(x%5) )
total+=x;
return(total);
}
An alternative (and more efficient) algorithm will perform the same loop twice, first in steps of 3 and then in steps of 5. Since we're no longer testing for invalid values, we simply sum every value.
int DoSum( void )
{
int total=0;
for(int y=0; y<2; ++y)
{
int step=y?5:3; // if y is 0, step is 3, otherwise step is 5
for(int x=step; x<1000; x+=step)
total+=x;
}
return(total);
}
What is the purpose of a C plus plus constructor?
The constructor in C++ is a function that runs when an object is created. It is used to initialize the object.
Types of constructors include default constructors (no arguments), copy constructor (one argument of same type as object), conversion constructors (one argument of some other type), and other constructors (all other cases).
If you do not provide a default constructor, the object will not be initialized.
If you do not provide a copy constructor, the compiler will blindly copy the attributes of the old object into the new object whenever a copy is made, such as in a function call with the object as an argument. This may or may not be safe, especially if any of the attributes are pointers, because that creates the situation of two pointers to the same region of memory. In that case, if that region of memory is an object, then when the object is destroyed, so will the pointed to object, and that will leave the original copied object in an invalid state, with its pointers referencing deleted memory.
Write a program in c plus plus to display the palindrome numbers between 10 and 1000?
#include<iostream>
unsigned int reverse(unsigned int num, unsigned int base=10)
{
unsigned int rev=0;
while( num )
{
rev*=base;
rev+=num%base;
num/=base;
}
return( rev );
}
int main()
{
std::cout<<"Palindromic numbers from 10 to 1000\n"<<std::endl;
for( unsigned int num=10; num<=1000; ++num)
if( num==reverse(num))
std::cout<<num<<std::endl;
}
An algorithm to Reversing the order of elements on stack S using 1 additional stacks?
// stack to contain content
Stack sourceStack = new Stack();
// ... fill sourceStack with content
// stack to contain reversed content
Stack targetStack = new Stack();
while (!sourceStack.empty())
{
targetStack.push(sourceStack.pop());
}
// targetStack contains the reversed content of sourceStack
when the object is instantiated(created) and delared,it has to assigned with variables.constructor is used for this purpose. syntax: classname ojbectname=new classname(); here classname() is a constructor. eg: box bb=new box(5,10); when this object is instantiated,it can be directly accessed by the constructor(similar to method but without a void because the instance variables are directly used) box(5,10) { }