1 / 17

Riyadh Philanthropic Society For Science Prince Sultan College For Woman

Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Defining Classes). continue. Exercise Complete the five missing parts in the following program: #include<iostream> #include<cmath>

hoai
Download Presentation

Riyadh Philanthropic Society For Science Prince Sultan College For Woman

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. continue • 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 1

  3. continue • 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; • } Lab: Defining Classes 2

  4. 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

  5. continue • 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 • }; Lab: Defining Classes 4

  6. continue • 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; • } Lab: Defining Classes 5

  7. continue • 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; • } Lab: Defining Classes 6

  8. 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

  9. 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

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

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

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

  13. 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

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

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

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

  17. 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

More Related