By ADEEL ABBAS, Bhakkar. AdeelAbbasbk@gmail.com
Plus-equal (+=) Operator Function
Now we will discuss the += operator. Whenever a programmer writes a += b, he will come across a different scenario as compared to the one witnessed in the case of the addition operator. In a way, now ‘a’ itself is being changed. So if a is going to change, we can return a reference to a. The conformability check remains the same as in ordinary addition. That means both matrices must have the same number of rows and columns. There is no need of creating a temporary matrix. Here we can return reference to left-hand side matrix. Here one finds that there is reuse and efficiency in the code. The += operator is defined as under.
const Matrix & Matrix::operator += (Matrix &m)
{
*this = *this + m;
return *this;
}
Minus Operator (-) Function
The same discussion of plus (+) operator applies to the minus (-) operator as both are identical. We see the difference between these operators when we do a + d. In case of addition, it will be the same as that of d + a. However, while dealing with minus case, the result of a - d will be obviously different from the result of d - a.
Multiplication Operator (*) Function
The most complicated operator out of all these arithmetic manipulators for matrices is the multiplication (*) operator. How do we multiply two matrices together?
The code of the function for * operator is defined as below.
Matrix Matrix::operator* ( const Matrix& m)
{
Matrix temp(numRows,m.numCols);
if(numCols == m.numRows)
Rules for Programming
We need simply three constructs to solve any problem.
1) Things should be executed sequentially. That means the statements are executed in a sequence i.e. the second statement follows the first and so on.
2) We need to have a decision. The decision means if something is true, the program executes it. Otherwise, it tries doing something else. So there is simple decision or if-else decision.
3) The third construct is loop, which is a repetition structure that performs the same task repeatedly with different values.
Write a declaration statement for an array of 10 elements of type float.
Include an initialization statement of the first four elements to 1.0, 2.0,
3.0 and 4.0.
Answer:
float floatArry[10] = {1.0,2.0,3.0,4.0};
Write the general syntax for the declaration of pre-increment and post increment
member operator function.
Answer:
Classname operator ++(); ---- pre increment
Classname operator ++(int) ---- post increment
Give the general syntax of class template.
Answer:
template
class myclass { ---} ;
What is a truth Table?
Answer:
There are some areas where the decision structures become very
complicated. Sometimes, we find it difficult to evaluate a complicated
logical expression. Sometimes the logic becomes extremely complicated so that even writing it as a simple syntax statement in
any language. It becomes complicated to determine what will be
evaluated in what way. We know the concept of truth table. The truth
tables are very important. These are still a tool available for analyzing
logical expressions.
What will be the output of following code, if user input a
number 123?
int input ;
cin >> oct >> input;
cout << hex << input ;
Answer:
53
Rational: it will take 123 as octal and print it in hex form which is 53.
What is principle of friendship in the context of functions and
classes?
Answer:
Class can declare a friend function and someone from outside the class
cannot declare itself friend of a class.
A friend function can access the private variables of class just like a
member function
How many arguments a Unary Operator take? Can we make a
binary operator as unary operator?
Answer:
Unary operator takes only one argument like i++ or i— (Post
increment or post decrement operators for integers) or ++i,--i (Pre
increment or pre decrement operators for integers) ,we can not make
Unary operator as binary or binary as Unary operator.
Which arithmetic operators cannot have a floating point
operand?
Answer:
Modulus operator:
This operator can only be used with integer operands ONLY
What are manipulators? Give one example.
Answer:
The manipulators are like something that can be inserted into stream,
effecting a change in the behavior. For example, if we have a floating
point number, say pi (л), and have written it as float pi = 3.1415926 ;
Now there is need of printing the value of pi up to two decimal places
i.e. 3.14. This is a formatting functionality. For this, we have
a manipulator that tells about width and number of decimal points of a
number being printed.
No comments:
Post a Comment
PLEASE COMMENT ABOUT YOUR VISIT AND MY SITE
Note: Only a member of this blog may post a comment.