agenda learning objectives
play

Agenda / Learning Objectives: 1. Run the following command and - PDF document

Agenda / Learning Objectives: 1. Run the following command and extract lab14.tar in your venus account (note the dot): cp ~ctse/cs211/lab14.tar . ; tar xvf lab14.tar 2. Understand the logic from lines 81 to 83 in Ch8Prog6.cpp. 3. Read the tips


  1. Agenda / Learning Objectives: 1. Run the following command and extract lab14.tar in your venus account (note the dot): cp ~ctse/cs211/lab14.tar . ; tar xvf lab14.tar 2. Understand the logic from lines 81 to 83 in Ch8Prog6.cpp. 3. Read the tips and pitfalls starting on page 2. 4. Complete the following exercise from the textbook. Review class lectures with Chapter 8 question 2: Rational number class Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so on we mean the everyday fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values of type int , one for the numerator and one for the denominator. Call the class Rational . Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int ; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber /1. Include a default constructor that initializes an object to 0 (that is, to 0/1). Overload the input and output operators >> and << . Numbers are to be input and output in the form 1/2 , 15/32 , 300/401 , and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so − 1/2 , 15/−32 , and −300/−401 are also possible inputs. Overload all the following operators so that they correctly apply to the type Rational : == , < , <= , > , >= , + , − , * , and / . Write a test program to test your class. Hints : Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b . You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be represented the same as − 1/2 .

  2. From teacher’s note for Absolute C++: Pitfalls Overloading &&, || and the Comma Operator . It is safest not to overload these operators as the overloaded || and && operators perform complete evaluation instead of short-circuit evaluation while the overloaded comma operator does not guarantee left to right evaluation. Member Operators and Automatic Type Conversion . When you overload a binary operator as a member operator, the two arguments are no longer symmetric. One is a calling object and only the second argument is a true argument. Overloaded member operators behave more nicely; see the tip on Member vs. Friend Overloading. Compilers without Friends . On some compilers, friend functions simply do not work as they are supposed to. If this happens then you must use accessor functions to define nonmember functions and overloaded operators or you must overload operators as members. All of the latest compilers do seem to support friends. Key Points Overloading Basics for Operators. With a small syntax change we can overload the default operators. Recall that few operations (+, /, -, ==, <, etc.) apply to class objects directly. We do have assignment, =, that results in member wise copy. We have seen that frequently this should be called member-UN-wise copy, since under circumstances frequently encountered, this results in a disaster. To prevent mischief, C++ requires that at least one argument of an overloaded operator be a class object. Operators are overloaded using an “operator function” having its name made up of the keyword operator followed by the operator symbol to be overloaded. The operator function must be a member of a class (in which case the calling object is an argument for the operator) or the operator function must have a class object as at least one of its arguments. Most operators can be overloaded, only a few cannot. Specifically, the operators . , .* , ?: , and :: may not be overloaded. There are a few operators that must be overloaded as non-static member functions, namely = , -> , () , [] . When an operator is overloaded, only operators that exist can be overloaded: No new tokens can be formed. For example, you cannot create an exponentiation operator ** as in Fortran. Only the behavior of operators can be changed by overloading. The arity of an operator cannot be changed. Arity (the number of arguments an operator takes, that is, whether the operator is binary or unary) and the precedence of an operator cannot be changed by overloading. For example, the “not” operator, !, is a unary prefix operator and cannot be made into a binary operator by overloading. Overloading as Member Functions . When a binary operator is overloaded as a member function, the operator’s left argument is always the calling object. The operator function must have one argument in this case. Having the operator function as member is convenient for access to class internals, but it prevents a measure of flexibility in symmetric treatment of overloading. When a binary operator is overloaded as a stand-alone function, the operator function must have two arguments. The left-hand operand of the expression is the first argument for the operator function and the right-hand operand of the expression is the second argument for the operator function.

  3. Overloading Unary Operators . When a unary operator is overloaded as a member function, the only operand is the calling object. This is true regardless of whether the operator is prefix or postfix. The compiler will take care of that detail. (But see the discussion of overloading the postfix ++ operator, later.) When a unary operator is overloaded as a stand-alone function, the argument of the operator is the argument for the operator function. For example, class A; //A is defined somewhere else A a; !a . . . //compiler translates this into operator!(a) //for a stand-alone overloaded operator Overloading Function Application (). Overloading this operator implements function call syntax for class objects. It can have default arguments. If we overload using class T { public: . . . void operator()(T t); . . . }; Where T1 and T2 are classes, then the function calls syntax, T v, w; u(v); is translated into a function call u.operator()(v); Given a call in a setting where there are several overloaded operator functions that may match, the “best match” rules for function overload resolution are used to find the operator function to be used. Friend Functions and Automatic Type Conversion . A friend function has access to all members of a class, regardless of access control (public, protected or private). A constructor with one argument provides automatic type conversion. A friend of a class is not a member. The access keywords public, private or protected have no effect on friend a declaration, so where in the class you place the declaration is immaterial. Constructors for Automatic Type Conversion. If you use a different type than the expected type in a function call, the compiler will look for a conversion from the type provided to the expected type. Constructors having one argument are candidates for this conversion. This conversion is applied to arguments for overloaded operator functions as well as to other functions. Friend Classes. Sometimes, there is a need for all functions of one class to be friends of another class. Declaring class F to be a friend of a class C makes each member functions of be F a friend of class C. There is often a choice between declaring a class into a friend of another class and making the class a member of the second class. Clearly, classes should be made friends only when the two classes represent closely related entities in the problem being solved. References. A reference is another name, that is, an alias, for an object. A reference is very nearly an automatically dereferenced, constant pointer, but pointers and references are not interchangeable. The main use for references is passing parameters to functions, function return values, and for overloaded operators. If T is a type, the expression T& x; defines x to be a reference to a type T object, and requires initialization. We do not care for use of stand-alone references, nevertheless, we write the following as an illustration. int i;

Recommend


More recommend