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;
}
What are the data types use in c plus plus?
Binary data is intrinsically numeric, therefore every type (including user-defined types) can be represented as a number. Even an executable or a file is nothing more than an extremely large number, in binary, made up of a specific sequence of bits grouped in various ways (bytes, words, dwords, etc), each of which is a number in its own right.
What these numbers actually represent is purely a matter of interpretation. While a char type is typically used to represent an alphanumeric character or symbol, it is really just a character code (a number in the range 0 to 255) that can be mapped to a symbolic character in the ASCII character set. A char is therefore no different to a byte and can represent an actual number just as easily as it can a character.
The primitive types are: unsigned short int, short int, unsigned long int, long int, int, unsigned int, char, wchar_t, bool, float, double and long double, each of which is intrinsically numeric. All user-defined types, including classes and structures, are derived from some combination of these primitive types.
What is a macro in c plus plus?
A macro is preprocessor definition that is processed prior to compilation. All occurances of a macro within your C++ source code are replaced with the macro definition, much like an automated search-and-replace-all operation. If the macro's definition is a function with one or more arguments, then the function is inline expanded within your code, replacing the defined arguments with the arguments that were passed to the macro. However, macro functions are not type safe and cannot be debugged because they only exist in your source code; the compiler only sees the intermediate code emitted by the preprocessor, at which point all macro definitions will no longer exist. To address this problem, C++ also supports the concept of template functions, which not only eliminates any unwanted inline expansion (resulting in smaller code), but also ensures that all calls to the function are type safe and can be debugged in the normal way. That said, macro functions, when used appropriately, can greatly simplify your code and can achieve things that would be difficult if not impossible to achieve with C++ alone. The ability to use code fragments via a macro is one such possibility. However, when combined with preprocessor directives such as #ifdef DEBUG, macros can also be used to provide useful and powerful debugging routines that only exist in debug code, compiling to no code in release builds. This cannot be achieved with C++ alone.
Disadvantages of friend function in c plus plus?
C is not object oriented programming language thus there are no friend functions in C.
In C++, which is object oriented, the advantage of a friend is that it allows code that cannot otherwise be regarded as being a member of the class to gain private access to the class representation, where public access to the representation would be considered undesirable. Although many see friendship as an undermining of encapsulation, the reality is it strengthens encapsulation by limiting exposure to the representation to only those functions that actually require that access. Public access is fully accessible, of course, but is not always desirable. Interfaces must be kept as simple as possible, but exposing an interface that is only of use to a few non-member functions is less desirable than simply making those functions friends of the class.
It is important to understand the relationships between functions and classes. All functions can be divided into one of three distinct categories with respect to any one class: non-member functions; static member functions or; nonstatic member functions.
Nonstatic member functions of a class have all three of the following properties:
1. The function has private access to the class representation.
2. The function is scoped to the class.
3. The function must be invoked upon an object of the class (has a 'this' pointer).
Static member functions only have the first two properties while friend functions only have the first property. External (non-friend) functions do not have any of these properties.
The only disadvantage to a friend function is when that friend is outwith the control of the class designer. Since friends have private access they must obey the class invariants, just as any member of the class must. Although they are not members of the class per se, they must be implemented just as if they were. In other words, they must not invalidate the representation. When the class designer has full control over the friends of that class then this is not a major problem. But when third-party programmers have access to the friend implementations, encapsulation can no longer be guaranteed by the class designer -- it is seriously undermined. But when used appropriately, friends actively re-enforce encapsulation and are no more an undermining of encapsulation than is public inheritance.
What is file mode in c plus plus?
There are 6 main types of file opening mode:
* "r". Open file for reading and file must exist; * "w" Open file for writing. If file does not exist it is created or if life already exist it's content is erased. * "a" Open file for appending. It adds all information at the end of the file leaving old data untouched. If file does not exist it is created. * "r+" Open file for reading and writing and file must exist. * "w+" Open file for writing and reading. If file does not exist it is created or if life already exist it's content is erased. * "a+" Open file for appending and reading. Again all new data is written at the end of the file old data leaving untouched. If file does not exist it is created. (You can read old data by moving pointer in file using fseek or rewind functions from stdio.h. But all writing operations will be done at the end of the file no matter how you change pointer) It is assumed by default that file will be standard ASCII text file in order to open file as binary file, you need to add "b" indicator:
FILE *myFile = fopen("myfile.txt", "wb");
/ * following two has identical meaning */
FILE *myFile = fopen("myfile.txt", "w+b");
FILE *myFile = fopen("myfile.txt", "wb+");
What is the major difference between c and c plus plus?
C is an imperative (procedural), structured paradigm language whereas C++ is multi-paradigm: procedural, functional, object-oriented and generic. Both are high-level, abstract languages. While C's design provides constructs that map efficiently to machine code instructions, C++ is more abstract, relying heavily upon object-oriented principals. However, both are equally capable of producing highly-efficient machine code programs. C++ derives almost directly from C thus everything you can do in C you can do in C++ with relatively minor alterations to the source. C++ was originally called C with Classes and that pretty much sums up the main difference between the two languages. However, there are many subtle differences.
One key difference between C and C++ is in the struct data type. In C, a struct can only contain public data members (with no methods). In C++, a struct is similar to a class, combining data and the methods that operate upon that data into a single entity (an object). The only difference between a C++ struct and a C++ class is that class members are private by default whereas struct members are public by default.
Another key difference is that because C++ is object oriented, there is much less reliance upon the programmer to manage memory. Each object takes care of its own memory allocations (including embedded objects), thus the programmer simply creates and destroys objects as needed. Thus C++ is much easier to work with, especially with regards to highly-complex hierarchical structures, but is every bit as efficient as C.
Both languages are highly popular and there are few architectures that do not implement suitable compilers for both. Thus they are both highly portable. However, the object oriented approach to programming gives C++ a major advantage over C in terms of code re-usability, scalability and robustness.
Why do you use standard template library in C plus plus?
Strictly speaking there is no need for function templates -- you could do it all by hand if you really wanted to. However, anything that aids in the reduction of duplicate source code is "a good thing" in my book. Code duplication increases code maintenance overheads, there's always the chance you'll forget to propagate changes consistently, and writing variations of a function that may or may not be used is simply a waste of time and effort.
To understand the need for function templates it's best to look at how they actually work. Let's consider the following two function declarations:
int GetMax(int a, int b){return(a>b?a:b);};
float GetMax(float a, float b){return(a>b?a:b);};
We can see that both functions have exactly the same body:
{return(a>b?a:b);};
These examples are fairly simple, but whether the function is complex or not, every time we need to pass a new data type to the GetMax() function we must declare a new definition of that function in order to handle that specific data type. Or we could write out every conceivable version of the GetMax() function in full, whether we intend to actually use it or not! That's certainly an option, but in a more complex function we may later decide to alter the implementation, in which case we must duplicate those changes across ever occurrence of the function. That's a lot of unnecessary work for just one function, and may introduce errors if we're not careful with our propagation of changes.
Thankfully, the compiler can do all this work for us. We need only define the function once, and once only, and the compiler will generate all the necessary code based upon the type of data we actually pass the function. This ensures we don't create copies of the function we will never use, and greatly reduces the maintenance overhead should we ever need to alter the implementation of the function.
We do this by using a function template.
template
T GetMax(T a, T b){return(a>b?a:b);};
Note that this is not a function, per se. It is a template for a function, whereby the variable T denotes a generic data type. Whenever we make a call to GetMax(), the compiler will automatically generate the necessary code for the actual function we need, replacing the generic data type T with the actual data type we pass into the function, such as int, or float.
int a=1, b=2;
int i=GetMax(i,j); // Generates the int variation of the function.
float x=0.3, y=1.2;
float f=GetMax(x,y); // Generates the float variation of the function.
Now that we have a function template, we are no longer restricted to ints and floats any more. We can now use GetMax() with any data type we wish, without having to write specific functions for those types. The compiler does it all for us, automatically.
DWORD p=0x000000FF, q=0x00000000;
DWORD d=GetMax(p,q); // Generates a new variation of the function.
Write down a program structure of c plus plus?
C++ programs are structured with data types and functions, much like C before it. However, unlike C, data types and functions can be combined to create entities that encapsulate a set of data and provide an interface to operate upon that data. These data types are known as classes, from which objects can be instantiated. Classes may also contain static data and methods which are local to the class rather than to a specific instance of the class. Although C++ is a general purpose, object-oriented programming language, it is also backwardly compatible with C and programs can be written using 4 different programming styles: procedural programming, data abstraction, object-oriented programming and generic programming. Most C++ programs are written using a combination of these styles.
What is explicit copy constructor?
An explicit constructor is one that is declared explicit and thus prevents implicit construction of an object. To understand explicit construction you must first understand implicit construction.
Consider the following code that uses a simple class, Square, to compute the square of any given integer:
#include
using namespace std;
class Square
{
private:
int m_number;
public:
Square(int number):m_number(number*number){}
friend void display(Square square);
};
void display(Square square)
{
cout << "Number = " << square.m_number << endl;
}
int main()
{
Square ten(10); // Explicit construction
Square twenty=20; // Implicit construction
display(ten);
display(twenty);
display(30); // What does this mean?
return(0);
}
Output:
Number = 100
Number = 400
Number = 900
Although there's nothing intrinsically wrong with the line display(30), it's not exactly clear to the reader what this line does. The display() function clearly expects an instance of a Square object, yet we're passing a literal constant.
The clue is the implicit constructor call, Square twenty = 20. Although we declared no assignment operator that accepts a literal constant, the statement implies we call the constructor, Square(int). In other words, the line is equivalent to explicitly calling Square twenty(20).
This is really no different to the way primitives work. That is, int x=5 is the same as calling int x(5), but the former is a more natural form of assignment. However, this behaviour is not always desirable. While Square twenty=5 may be fine, the unwanted side effect is that it also allows us to make ambiguous calls, like display(30).
Although the line display(30) forces the display()function to construct a local Square object using implicit construction, it's not obvious to the reader that an object is created (and subsequently destroyed), and as a result of this it isn't obvious to the reader why display(30) would produce the result Number = 900.
Note that while this usage may well be obvious to the developer while writing the code, when the developer revisits the code later, it may not be quite so obvious. And if the developer has trouble reading their own code, you can imagine how difficult it would be for a third party to understand the same code.
To avoid this problem, we must declare the constructor to be explicit:
explicit Square( int number ):m_number(number*number){}
As soon as we do this, the lines Square twenty = 20 and display(30) become invalid. To rectify this, we must replace them with calls to the explicit constructor instead:
int main()
{
Square ten(10); // Explicit constructor
Square twenty(20); // Explicit constructor
display(ten);
display(twenty);
display(Square(30)); // Explicit constructor
return(0);
}
Now it is much clearer to the reader what's going on. Although we've lost the ability to implicitly construct a Squareobject, we've assured our code remains readable at all times by enlisting the help of the compiler to ensure that we never unintentionally construct a Square object via implication.
What are the rules for inline functions in c plus plus?
When you mark function as inline compiler puts the whole body of function in those places it is called, similar idea as in macros. If you do not mark function as inlinecompiler inside still decides which functions should be inline and which not. Inline function is less performance costly especially if function is called very often. Why it is lest performance costly? Because to invoke function you need to prepare parameters, put them to stack, make jump and etc. and all those steps are eliminated if function is inline.
Example (very basic):
inline int sum(int a, int b) {
return a + b;
}
int c, d;
c = sum(2, 3); /* compiler will change to 2 + 3 */
d = sum(2, 5); /* this one will be changed to 2 + 5 */
Full inline functions are allowed in ANSI/ISO C99.