PROMOTE MY BLOG: JUST CLICK BELOW BUTTON

Search Any Paper On This Blog

Thursday, March 3, 2011

CS201 Subjective questions 2

By ADEEL ABBAS, Bhakkar. AdeelAbbasbk@gmail.com

Friend function
A type of declaration used within a class to grant other classes or functions access to that class.
We use the keyword ‘friend’ before the prototype of the function.
friend return_type friend_function_name(int, char);

Friend Classes
It shows that OtherClass is a friend of ClassOne so it has access to the private data of ClassOne.

The destructors can be summarized as the following.
The destructors cannot be overloaded.
 The destructors take no arguments.
The destructors don’t return a value.
 So they don’t have a return type and no return statement in the body.
Date::~Date()
{
cout << "The object has destroyed" << endl;
 }



New Operator
Whenever new operator is used, no number of bytes or sizeof operator and no cast is applied to convert the pointer to the required type.

The memory allocated from free store or heap is a system resource and is not
returned back to the system unless explicitly freed using delete or free operators.

- If the memory in the free store is not sufficient enough to fulfill the request,
malloc() function returns NULL pointer. Similarly, the new function returns 0 in
case the request could not be fulfilled.


References as Return Values A function itself can return a reference. The syntax of declaration of such a function will be as under.
datatype& function_name (parameter list)


Operators to Overload
There are two types of operators to overload:
1. Unary
2. Binary
Unary operators are the ones that require only one operator to work. Unary operators are applied to the left of the operand. For example, ^, &, ~ and !.
 Binary operators require two operands on both sides of the operator. +, -, *, /, %, =, < and > are examples of binary operators.

The complete list of C++ operators that can be overloaded is as follows:
+ - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | | ++ - - -> * , [ ] ( ) new new[ ] delete delete[ ]
The following operators can’t be overloaded. . : :: .* ? sizeof



template
A combination of a template with a template argument list via the process of template instantiation.


Destructor of Matrix Class
Destructor’ is relatively simple. It becomes necessary after the use of new in the constructor. While creating objects, a programmer gets memory from the free store. So in the destructor, we have to return it. We will do it as:
delete [] elements;


Utility Functions of Matrix
The functions getRows() and getCols() are relatively simple. They do not change anything in the object but only read from the object. Therefore we have made this function constant by writing the const keyword in the end. It means that it does not change anything. The code of the getRows() functions is as follows:
 int Matrix :: getRows ( ) const
 {
 return numRows;
}


Input Functions
Input functions are also of two types like output functions. The first function takes input from the keyboard while the other takes input from the file. The function that takes input from the keyboard is written in a polite manner because humans are interacting with it. We will display at the screen”Input Matrix size: 3 rows by 3 to columns” and it will ask “Please enter 3 values separated by spaces for row no. 1” for each row.

Transpose Function
 The transpose of a matrix will interchange rows into columns. There are two alternative requirements. In the first case, we have a square matrix i.e. the number of rows is equal to number of columns. In this situation, we don’t need extra storage to do this. If the number of rows is not equal to the number of columns, then we have to deal it in a different way. We can use general case for both purposes but you will notice that it is slightly insufficient. Here is the code of the function.

No comments:

Post a Comment

PLEASE COMMENT ABOUT YOUR VISIT AND MY SITE

Note: Only a member of this blog may post a comment.