Presentation is loading. Please wait.

Presentation is loading. Please wait.

Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:

Similar presentations


Presentation on theme: "Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:"— Presentation transcript:

1 Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Defining Classes)

2 Lab: Defining Classes1 Exercise Complete the five missing parts in the following program: #include 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

3 Lab: Defining Classes2 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

4 Lab: Defining Classes3 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))); }

5 Lab: Defining Classes4 Solution #include 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

6 Lab: Defining Classes5 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

7 Lab: Defining Classes6 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

8 Lab: Defining Classes7 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))); }

9 Lab: Defining Classes8 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.

10 Lab: Defining Classes Solution #include using namespace std; class Rectangle { public: Rectangle(double the_length, double the_width); double get_length(); double get_width(); private: double length; double width; }; 9 continue

11 Lab: Defining Classes 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; } 10 continue

12 Lab: Defining Classes Solution double Rectangle::get_length() { return length; } double Rectangle::get_width() { return width; } 11

13 Lab: Defining Classes 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. 12

14 Lab: Defining Classes Solution #include 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; }; 13 continue

15 Lab: Defining Classes 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; } 14 continue

16 Lab: Defining Classes Solution Rectangle::Rectangle(double the_length, double the_width) { length = the_length; width = the_width; } Rectangle::Rectangle( ) { length = 0; width = 0; } continue 15

17 Lab: Defining Classes 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; } 16


Download ppt "Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:"

Similar presentations


Ads by Google