Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Defining Classes) Exercise Complete the five missing parts in the following program: #include<iostream> #include<cmath> using namespace std; class Loan { public: Loan(double the_amount, double the_rate, int the_term); Loan( ); void display( ); double payment( ); private: double amount; // $ amount of the loan double rate; // annual interest rate int term; // number of months, length of the loan }; Lab: Defining Classes continue 1 Exercise int main( ) { //1. Declare two loan objects: loan1 (initialized to given values) and // loan2 (initialized to default values) cout << "Display loan1: \n"; loan1.display(); //2. Display all information about loan2 cout << "Monthly payment of loan1 = " << loan1.payment() << endl; loan2 = loan1; cout << "Monthly payment of loan2 = " << loan2.payment() << endl; return 0; } continue Lab: Defining Classes 2 Exercise Loan::Loan(double the_amount, double the_rate, int the_term) { //3. Initialize the loan amount to the_amount, the loan rate to the_rate // and the loan term to the_term } //4. Write the definition of the default constructor and initialize all member // variables to zero void Loan::display() { //5. Display all information about the loan, i.e. amount, rate, and term } double Loan::payment() { double fraction_rate = rate / 1200; return (amount * fraction_rate * (pow((fraction_rate + 1),term) / (pow((fraction_rate+1),term) - 1))); } Lab: Defining Classes 3 Solution #include<iostream> #include<cmath> using namespace std; class Loan { public: Loan(double the_amount, double the_rate, int the_term); Loan( ); void display( ); double payment( ); private: double amount; // $ amount of the loan double rate; // annual interest rate int term; // number of months, length of the loan }; continue Lab: Defining Classes 4 Solution int main( ) { Loan loan1(1000,5,10), loan2; cout << "Display loan1: \n"; loan1.display(); cout << "Display loan2: \n"; loan2.display(); cout << "Monthly payment of loan1 = " << loan1.payment() << endl; loan2 = loan1; cout << "Monthly payment of loan2 = " << loan2.payment() << endl; return 0; } continue Lab: Defining Classes 5 Solution Loan::Loan(double the_amount, double the_rate, int the_term) { amount = the_amount; rate = the_rate; term = the_term; } Loan::Loan() { amount = 0; rate = 0; term = 0; } continue Lab: Defining Classes 6 Solution void Loan::display() { cout << "Amount = " << amount << endl << "Rate = " << rate << endl << "Term = " << term << endl; } double Loan::payment() { double fraction_rate = rate / 1200; return (amount * fraction_rate * (pow((fraction_rate + 1),term) / (pow((fraction_rate+1),term) - 1))); } Lab: Defining Classes 7 Exercise Write a class called Rectangle that represents a rectangle as a pair of double values - the rectangle length and width. Your class should include the following member functions: • A constructor with two arguments that sets the length and width to the values specified by its arguments. • A get_length function that gets the length of the rectangle. • A get_width function that gets the width of the rectangle. Note: test the class. Lab: Defining Classes 8 Solution #include<iostream> using namespace std; class Rectangle { public: Rectangle(double the_length, double the_width); double get_length(); double get_width(); private: double length; double width; }; continue Lab: Defining Classes 9 Solution int main( ) { Rectangle rectangle1(1,2); cout << "Length of rectangle1 = " << rectangle1.get_length() << endl << "Width of rectangle1 = " << rectangle1.get_width() << endl; return 0; } Rectangle::Rectangle(double the_length, double the_width) { length = the_length; width = the_width; } continue Lab: Defining Classes 10 Solution double Rectangle::get_length() { return length; } double Rectangle::get_width() { return width; } Lab: Defining Classes 11 Exercise Add the following member functions to the Rectangle class: • A default constructor that sets the length and width to 0.0. • An area function that returns the area of the rectangle. (length times width) • A perimeter function that returns the perimeter of the rectangle. (2 times length plus 2 times width) Note: test the new functions. Lab: Defining Classes 12 Solution #include<iostream> using namespace std; class Rectangle { public: Rectangle(double the_length, double the_width); Rectangle( ); double get_length(); double get_width(); double area(); double perimeter(); private: double length; double width; }; continue Lab: Defining Classes 13 Solution int main( ) { Rectangle rectangle1(1,2), rectangle2; cout << "Length of rectangle1 = " << rectangle1.get_length() << endl << "Width of rectangle1 = " << rectangle1.get_width() << endl; cout << "Length of rectangle2 = " << rectangle2.get_length() << endl << "Width of rectangle2 = " << rectangle2.get_width() << endl; double area = rectangle1.area(); double perimeter = rectangle1.perimeter(); cout << "Area of rectangle1 = " << area << endl << "Perimeter of rectangle1 = " << perimeter << endl; return 0; } continue Lab: Defining Classes 14 Solution Rectangle::Rectangle(double the_length, double the_width) { length = the_length; width = the_width; } Rectangle::Rectangle( ) { length = 0; width = 0; } continue Lab: Defining Classes 15 Solution double Rectangle::get_length() { return length; } double Rectangle::get_width() { return width; } double Rectangle::area() { return length * width; } double Rectangle::perimeter() { return 2 * length + 2 * width; } Lab: Defining Classes 16