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

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Operator overloading redefine the operations of operators
Exercise 2.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
C++ Classes & Data Abstraction
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Computer Science 1620 Programming & Problem Solving.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
First steps Jordi Cortadella Department of Computer Science.
CS442: ADVANCED PROGRAMMING USING JAVA Lab 6: Classes and objects Computer Science Department.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Access Functions and Friend Functions
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Classes.
Introduction to Classes
Chapter 5 Classes.
group work #hifiTeam
Extra.
Introduction to Classes
Pointers & Functions.
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
Lab 1 Introduction to C++.
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
Pointers & Functions.
Lecture 8 Object Oriented Programming (OOP)
Introduction to Algorithms and Programming COMP151
Objects as Function Arguments
Presentation transcript:

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

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

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

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))); }

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

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

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

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))); }

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.

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

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

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

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

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

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

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

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