Answer:
1. Function overloading is capability of several functions of the same name to be defined, as long as the return type or the argument types of the two functions are different, to perform similar tasks. We can have as many different versions of a function as you need. For example: overloaded printArray function:
Void printArray (const int* array, int count) // function print array for int data type
Void printArray (const double* array, int count) // print array for double data type
Function template is capability to perform similar or identical operation on different types of data.
For example: function template printArray below
Void printArray(const T *array, int count)
In test file, we can call use this function to print several types of array based on what type of array that we pass to the function.
Thus, the major different is the function template can handle different data types without separate code for each of them. For a similar operation on several kinds of data types, a programmer need not write different versions by overloading a function. It is enough if he writes a C++ template based function. This will take care of all the data types. In other hand, we need to write separate function in function overloading to handle different type of data.
2. We need C calling convention besides standard calling convention when importing a variable defined in C and the object is not part of a DLL.
3. Function pointer is a type of pointer that when dereferenced, it calls a function with arguments placed after the dereferenced pointer just like a normal function. Function pointers are of the type the function returns. They are used to eliminate giant switch statements.
4. We need to implement a copy constructor if we use a class that has pointer variables and has some dynamic memory allocations. In other words, a copy constructor should be provided for any class for which we want deep copy semantics.
We also need to implement our own assignment operator= for any class that contains pointer or you want the assignment operator to be in private member of the class. In addition, assignment operator= is needed when we are doing object-to-object assignment. The key difference between assignment and copy construction is that assignment possibly involves the destruction of embedded resources. We need to overload the assignment operator whenever we want to guarantee that memory (or perhaps other resources) aren't orphaned improperly.
5. A constant member function is a function which may not modify data members. In other hand, non-constant member function can modify data members. We need constant member functions in order to make sure a member function will not modify its data members.
Example:
class Backpack {
public:
const Book& getContents() const; // example of a constant member function
void setContents(const Book& bk); /* example of a non constant member function (it changes myText variable)*/
private:
Book myText;
};
6. C++ class achieves polymorphism using inheritance technique and an abstract class as the base class. All functions in base class are pure virtual functions which the real implementation is written individually in its sub-classes by creating the same function names in sub-classes and base class.
C++ achieves encapsulation by creating a class and declaring which members are private, protected, or public. In addition, specified non-member functions may be given access to private or protected parts of classes. These functions are specified using the friend keyword, and may be globally defined functions or members of other classes. Whole classes may be declared as friends of another class, in which case every member of the friend class can access the private members of the specified class.
7. Pure virtual function is a virtual function that is explicitly marked as having no implementation by using a special syntax (example: class B{virtual void apurevirtualfunction() = 0;}; ). The function prototype then serves as a stub that provides only the signature of the eventual implementing method. The actual definition or definitions will be provided later as overridden functions in derived classes.
An abstract class is one that is designed only as a parent class and from which child classes may be derived, and which is not itself suitable for instantiation. Abstract classes are often used to represent abstract concepts or entities. The incomplete features of the abstract class are then shared by a group of sibling sub-classes which add different variations of the missing pieces. An abstract class is defined as a class having at least one pure virtual method. They can not be instantiated and will generate an error if an attempt is made. They are meant to function as stubs, allowing the programmer to identify what modules of functions (behaviour or methods) are needed without having to actually implement them. This is in line with OOP's philosophy of allowing the programmer to concentrate on how an object should behave without going into the actual detail.
Example:
// example of abstract class
class Abstract {
public:
virtual void MyVirtualMethod() = 0; // this is pure virtual function
};
class Concrete : public Abstract
{
public:
void MyVirtualMethod()
{
//do something
}
};
8. Can a class static member function access the class non-static member function?
No, a static member function can access only static member data, static member functions and data and functions outside the class.
Can a class non-static member function access the class static member function?
Yes, a non-static member function can access all of the above including the static data member.
9. If a collection is rarely modified, but often retrieved, what type of container(s) should we choose?
Vector class container can be a good choice because it offers random access to elements.
If a collection contains dynamic items, i.e. inserted/removed often, and retrieved often, too, what type of containers should we choose?
Deque class container can be a good choice because it also supports efficient insertions and deletions at both ends. It also offers random access to elements.
10. Type of iterator that STL list implement is bidirectional since list has double-linked list data structure.
Type of iterator that STL vector implement is random access (ability to directly access any element of the container).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment