answersLogoWhite

0


Best Answer

The ternary conditional operator:

x = a ? b : c;

This is the literal equivalent of the following if statement:

if (a) {

x = b;

} else {

x = c;

}

Note that a, b and c are all expressions (or compound expressions). Expression a must be a Boolean expression (evaluating true or false) while expressions b and c must be implicitly convertible to x's data type.

A real-world example:

// Return the larger of any two integers

int max (int a, int b) {

return a>b ? a : b;

}

User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

elif

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which operator can be used in some situations to simplify nested selection structures?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

C plus plus class of rational numbers in which multiplication and division occur?

The following program implements a user-defined class for rational numbers, including operator overloads for all arithmetic operations (+, -, * and /). Note that the class does not handle 0/n fractions properly because the reciprocal of 0/n is n/0 which is an invalid fraction with undefined value. The problem is that 0/n fractions are perfectly valid (1/2 - 1/2 = 0/2) so you need to cater for them (they simplify to zero), but you cannot divide by them. It is left as an exercise for the reader to provide handlers to cater for this type of exception. #include<iostream> #include<string> #include<sstream> // GCF: greatest common factor (AKA: GCD, greatest common divisor) int gcf (int a, int b) { // Recursive Euclid algorithm. return (b != 0 ) ? gcf (b, a % b) : a; } // LCM: least common multiple int lcm(int a, int b) { return (b / gcf (a, b)) * a; } // Class to store rational numbers (fractions). class rational { friend std::ostream& operator<< (std::ostream&, const rational&); private: int m_p; int m_q; public: rational (const int = 1, const int = 1); rational (const rational&); rational (const std::string& str); rational (const char* c_str); rational& operator= (const rational&); rational& operator= (const std::string&); rational& operator= (const char*); rational& operator-= (const rational&); rational& operator*= (const rational&); rational& operator/= (const rational&); rational& operator+= (const rational&); rational operator- (const rational&); rational operator* (const rational&); rational operator/ (const rational&); rational operator+ (const rational&); rational reciprocal() const; rational& simplify (); rational& unsimplify (int multiple); bool operator slash_pos) { std::string p = str.substr (0, slash_pos++); std::string q = str.substr (slash_pos, str.size()-slash_pos); std::stringstream ssp (p); std::stringstream ssq (q); int ip; int iq; if (ssp >> ip && ssq >> iq && iq != 0) { m_p = ip; m_q = iq; return *this; } } } return *this; } // Equality operator. bool rational::operator== (const rational& other) const { // Simplify both fractions before comparing. rational copy (*this); rational temp (other); copy.simplify(); temp.simplify(); return copy.m_p==temp.m_p && copy.m_q==temp.m_q; } // Return the reciprocal by value (swap numerator/denominator) rational rational::reciprocal() const { return rational (m_q, m_p); } // Simplifies the fraction. rational& rational::simplify() { // Simplify the sign. if (m_q<0) { m_p *= -1; m_q *= -1; } int factor = gcf (this->m_p, this->m_q); this->m_p /= factor; this->m_q /= factor; return *this; } // Usimplifies the fraction (use given denominator) rational& rational::unsimplify (int denominator) { this->m_p *= denominator / m_q; this->m_q = denominator; return *this; } // Test drive the rational class. int main() { // Test arithmetic. rational a = "1/2"; rational b (2, 5); // e.g., "2/5" std::cout << a << " + " << b << " = " << a + b << std::endl; std::cout << a << " - " << b << " = " << a - b << std::endl; std::cout << a << " * " << b << " = " << a * b << std::endl; std::cout << a << " / " << b << " = " << a / b << std::endl; // Test simplifier on negatives and integers. rational c (2, -5); // -2/5 rational d (-2, -5); // 2/5 rational e (3, -1); // -3 std::cout << c << " simplified is "; std::cout << c.simplify() << std::endl; std::cout << d << " simplified is "; std::cout << d.simplify() << std::endl; std::cout << e << " simplified is "; std::cout << e.simplify() << std::endl; }


What is the term for scientific discoveries that simplify work?

Inventions


What are abstraction level of programming of multimedia?

Physical level: The lowest level of abstraction describes how a system actually stores data. The physical level describes complex low-level data structures in detail.Logical level: The next higher level of abstraction describes what data the database stores, and what relationships exist among those data. The logical level thus describes an entire database in terms of a small number of relatively simple structures. Although implementation of the simple structures at the logical level may involve complex physical level structures, the user of the logical level does not need to be aware of this complexity. Database administrators, who must decide what information to keep in a database, use the logical level of abstraction.View level: The highest level of abstraction describes only part of the entire database. Even though the logical level uses simpler structures, complexity remains because of the variety of information stored in a large database. Many users of a database system do not need all this information; instead, they need to access only a part of the database. The view level of abstraction exists to simplify their interaction with the system. The system may provide many views for the same database.


How you simplify a network to thevnin's theorem?

Difficult to explain without using a circuit diagram to illustrate use as an example. Refer to any textbook to find your answer.


How to overloade operater in c plus plus?

The assignment operator overload is a typical example of operator overloading. Every class of object has an assignment operator by default, which simply assigns the value of the members of an existing class to the members of another existing class. The two classes may even be the same class. However, the default implementation is not suitable for classes that contain pointers to memory that cannot be shared with other classes. Assigning the value of one pointer to another simply copies the pointer's value (a shallow copy) but not what it points at (a deep copy). Thus the assignment operator must be overloaded to perform a deep-copy, as per the following example: struct X { // destructor ~X(){ delete( m_pointer ); } // default constructor X(): m_pointer( new int(100) ){} // copy constructor X( const X& x): m_pointer( x.m_pointer ? new int( *x.m_pointer) : NULL ) {} // assignment operator overload X& operator= ( const X& x ); int* m_pointer; }; X& X::operator= ( const X& x ) { if( this != &x ) // test for self-reference {delete( m_pointer );m_pointer = x.m_pointer ? new int(*x.m_pointer) : NULL;} return( *this ); } In the above example, the assignment operator overload first checks to ensure that the incoming object is not a self-reference. If it is not, then we release the current allocation and create a new allocation, copying the incoming object's memory by value. If we had not checked for a self-reference, then we could easily end up deleting the very memory we wished to copy, such as in the following example: X x; X* y = &x; x = *y; // self-assignment In the above example, x and *y are obviously references to the same object, but it may not be quite so obvious if the assignment occurred elsewhere in our code. Therefore the object's assignment operator overload must cater for this eventuality and veto the assignment -- thus leaving the original object unchanged. The assignment operator can also be overloaded in order to prevent assignment. You would do this if the object were intended to act as a singleton, for instance. In this case we simply need to declare the operator as private to the class but do not need to provide any implementation. Alternatively, we can simply return a reference to the current instance: X& operator= ( const X& x ) { return( *this ); } No other operator has a default implementation when applied to classes, therefore if we want to make use of other operators upon our classes, such as +, ++ or +=, then we must overload them in the class declaration. Again, each overload must test for self-references and act accordingly. However, while there's nothing to prevent you from providing a completely unique implementation of these operators, common sense dictates that all such implementations must be intuitive and predictable. While it may be fun to implement the plus operator as a minus operation, it has no practical value in the real world -- it is neither intuitive nor predictable. The stream insertion (<<) and extraction (>>) operators are another typical example of operator overloading. These cannot be implemented within the class itself because the l-value of these operators must be a stream, while internal class operator overloads use the current instance of the class as the l-value. Therefore these operators must be implemented outside of the class. Many programmers implement these "external" operators as friend functions. While some do have valid reasons for doing so, if the public class interface provides everything required of these operators then there is no need to declare the operator as a friend function. Although some programmers continually claim friendship undermines encapsulation this is not the case at all. Friends simply extend the class interface, nothing more. However, if the underlying class implementation is altered, all friend functions may need to be updated to cater for these changes -- which increases the maintenance cost. Therefore, if operator overloads can be implemented with friend access, then your class becomes that much easier to maintain. Here's a typical example of an external operator implemented as a friend function: struc X { friend ostream& operator<<( const X& x ); private: int m_data; }; ostream& operator<<( ostream& os, const X& x ) { os << x.m_data; return( os ); } Now here's the same operator overload implemented without a friend function: struc X { int GetData() const { return( m_data ); } private: int m_data; }; ostream& operator<<( ostream& os, const X& x ) { os << x.GetData(); return( os ); } While there's clearly the need for an extra function call, that call is completely eliminated by virtue of the fact X::GetData() will be inline expanded by the compiler. Thus the latter will not affect performance in any way. However, the main advantage of the non-friend function comes when you later decide to alter the implementation of the class: struc X { int GetData() const { return(( int ) m_data ); } private: double m_data; }; Here we've changed the member variable to a double, but the GetData() accessor method still returns an int. The accessor is therefore an abstraction, thus the insertion operator overload is left unaffected by the internal change to the class. However, had we made this same change in the earlier example employing the friend function, there is no abstraction and the extraction operator will insert a double into the output stream. If this were undesirable then we'd have to alter the friend function as well. Not only have we increased the maintenance, we've completely undermined the abstraction of our class. Note that we have not undermined the encapsulation (as some would claim), only the abstraction has been undermined. Thus if you want to retain the abstraction, do not use a friend function. Friend functions should only be used when abstraction is not an issue, and the function genuinely requires access to the private members of the class.