Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class rational part2.

Similar presentations


Presentation on theme: "Class rational part2."— Presentation transcript:

1 class rational part2

2 Learning Goals Programs are composed from separate files.
Split up Lab6rational.cpp into separate files main.cpp – main program rational7.h – header declaration rational7.cpp – source implementation Consolidate 3 constructors using defaults. Use const parameters and const functions Use friend functions for sum, product, equals Use static members and functions. Dynamic arrays of objects.

3 Splitting class files rational7.h – contains the declaration of the class and all functions related to the class. rational7.cpp – contains all of the functions and member definitions related to the class. main.cpp – contains the program that uses the rational class.

4 Copy the Lab6rational.cpp
cp ../rational_class_part1/Lab6rational.cpp rational7.h cp ../rational_class_part1/Lab6rational.cpp rational7.cpp cp ../rational_class_part1/Lab6rational.cpp main.cpp To compile a program with multiple files: g++ main.cpp rational7.cpp –o rational Or g++ -c rational7.cpp g++ -c main.cpp g++ main.o rational7.o –o rational

5 rational7.h – header file
rational7.h should include any library files needed to define all of the types used #include <iostream> using namespace std; We also have to include the using namespace line, but that is not usually done. Usually, programmers will explicitly say void input(std::istream& in); void output(std::ostream& out);

6 rational7.h – header file
rationa7.h should protect itself from defining the class twice: #ifndef _RATIONAL #define _RATIONAL class rational { … }; #endif // _RATIONAL The first time it gets included, _RATIONAL is not defined. However, it is immediately defined after it is included, so the next time, it skips the declaration.

7 Include all dependencies
Since rational7.h uses istream and ostream, include iostream #include <iostream> using namespace std; Generally, we don’t add the using namespace std to class files, instead we fully qualify the name like this: void input(std::istream& ins); void output(std::ostream& out); Students can do it to make it easier.

8 rational7.cpp – source file
You must include the declaration of the rational class everywhere it is used. The declaration is in rational7.h #include <iostream> #include “rational7.h” using namespace std; rational7.cpp contains all of the member and non-member functions related to rational class.

9 Get Lab6 working as Lab7 The best approach is the get lab6 functionality working with lab7 multiple files. Then add the constructor consolidation which shouldn’t break anything. Then add const to existing functions that can be declared as const. Then start implementing new functionality. Sum friend Product friend

10 Then modify input and output functions
Lastly, implement simplify() as a member function. Recommend using gcd function given in the Mimir instructions. Advanced topics… You may implement operator+ in lieu of sum You may implement operator* in lieu of product You may implement operator== in lieu of equals You may not use a vector in place of dynamic array.

11 Static data members Static data members are outside of any object in the class. Static data members can have any access. Static data members do not need an object to exist. It gets created before any object of the class is created. static Type staticDatMember; Static data members declared in a class have to be defined in the source file: Type className::staticDataMember = value;

12 Static member functions
Static member functions access static members Static member functions do not access non-static data members unless they are parameters. Calling a static function looks like ClassName::staticFuncName(arg-list); Declaring a static function in the class looks like static return-type staticFuncName(parm-list); Definition in the source file looks like return-type ClassName::staticFuncName(parm-list) { // function logic }

13 Consolidate constructors
C++ allows default values for parameters. Using default values for parameters in a constructor can help reduce the number of functions without reducing the functionality. rational(int num=0, int den=1); In the implementation, the function has to behave as if the programmer passed in values for all parameters with complete error checking.

14 Changes to rational class (.h)
One constructor with default values numerator=0 and denominator=1. Add set member function that takes both numerator and denominator and assigns them to the invoking object data members. Make accessor functions const functions. Add parameters to input and output. void input(istream& in); void output(ostream& out);

15 Changes to rational class (.h)
Sum function becomes a friend function with const rational& as parameters Product function is a new function that is also a friend function with const rational& as parameters Add an equals friend function that compares 2 rationals. Parameters should be const rational&.

16 Changes to class rational (.h)
Add a public static function getObjectCounter that returns the static counter defined below. Add a private static counter. Static members are created without an object. They are available to ALL objects in the class. The declaration does not create the space for the variable. It needs to be defined in the implementation file.

17 rational implementation (.cpp)
One constructor that assumes all variables are arguments (not defaults). Add set member function Make accessor functions const functions. Changes to input function use a parameter ins instead of cin directly. Changes to output function use a parameter out instead of cout directly. .

18 rational implementation (.cpp)
Changes to sum making it a friend function. Friends are not member functions so no scope. Friends do not have a this pointer, so all data members are accessed with a ‘.’ operator. Follow the same pattern to make the product function as sum above.

19 rational implementation (.cpp)
Add an equals function as a friend function. Can’t just compare numerator to numerator and denominator to denominator. E.g. 1/2 == 2/4 should be true. 1/2 == 2/4 will be true because 0 == 0 int/int is an int Cast them to double(1)/2 == double(2)/4 Cast them to static_cast<double>(1)/2 == … Cross multiply and compare products 1 * 4 == 2 * 2 or n1 * d2 == n2 * d1

20 main.cpp - program Include the rational7.h in the main.cpp as well.
This is where the program is written. The program will have a do-while loop. // read an integer for an array in a size variable. rational *fractions = new rational[size]; // for-loop to set each fraction fractions[i].set(1,i+1); // another for-loop to perform sum, product

21 Declare local rational variables
Remember to declare local rational variables for the sum and product. The best way to accumulate sums and products is to implement something like r_sum = r_sum + next[i]; r_prod = r_prod * next[i]; How should the r_sum be initialized? What about product? Remember anything multiplied by 0 is 0.

22 Class rational part3 Must read in operators as strings.
Write a function in main using a vector<string> initialized to all of the operators vector<string> opstrs = {“+”,”-”,”*”,”/”,”==“ “<“,”>”,”<=“,”>=“,”!=“,”-1” }; // loop over the opstrs testing it against the input // operator. Return the index when it’s found and -1 // otherwise. // create a function called int getOperator(string op) // Use a switch statement of integers where // case 0 for +, case 1 for -, case 2 for * and so on.


Download ppt "Class rational part2."

Similar presentations


Ads by Google