Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.

Similar presentations


Presentation on theme: "1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan."— Presentation transcript:

1 1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan

2 2 Overview  Warm-up/review  A class interface for bankAccount  Structure of a class interface  Member data and functions  Working with objects  Preview of implementing member functions  Types of class member functions

3 3 Review of Strings  Last time, we finished with a problem:  string returnedExpression(string inputString)  // PRE: inputString is a line of valid C++ code // (<=80 chars) containing the “return” // keyword and ending with a semicolon  // POST: FCTVAL == the expression that follows // the return keyword, not including the // semicolon  Recall the string member functions:  stringObject.length()  stringObject.find(string)  stringObject.substr(startLocation, length)  How do we use these functions to construct a solution?

4 4 Solution  string returnedExpression(string inputString)  // PRE: inputString is a line of valid C++ code (<=80 chars) containing // the “return” keyword and ending with a semicolon  // POST: FCTVAL == the expression that follows the return keyword, // not including the semicolon  First, find where “return” is located  int returnLoc = inputString.find(“return”);  Next, find how long the string is  int exprLength = inputString.length();  Find how long the expression is:  exprLength -= 1; // don’t want semicolon  exprLength -= returnLoc; // or stuff before return  exprLength -= 7; // or “return ”

5 5 Solution  string returnedExpression(string inputString)  // PRE: inputString is a line of valid C++ code (<=80 chars) containing // the “return” keyword and ending with a semicolon  // POST: FCTVAL == the expression that follows the return keyword, // not including the semicolon  Finally, construct and return the expression as a substring:  return inputString.substr(returnLoc+7, exprLength);

6 6 Abstraction  Another motivation for OOP  The concept of being able to use something difficult without knowing the details of how it works  Focus on WHAT it does, not HOW it does it  Everyday life examples?

7 7 Object Oriented Terms  Class  Object  Member  Encapsulation  Information Hiding  Message  Abstraction

8 8 Back to the Bank Account Class  Data:  name  balance  Operations:  create an account  withdraw  deposit  check balance

9 9 Bank Account Objects  Objects are instances of the class  One class, many objects name balance acct1 $500.00 Marge name balance acct2 $123.45 Bart name balance acct3 $20.00 Homer

10 10 Class Interface  Starting point for working with classes  Defines the class  Defines the WHAT, not the HOW

11 11 So what does a class interface look like? #include // string class definition using namespace std;// so std:: isn’t needed class bankAccount { public: bankAccount(); // POST: default bankAccount object constructed with name == “?” and balance == 0 void withdraw(int amount); // PRE: amount in dollars and amount > $0 // POST: amount has been subtracted from balance void deposit(int amount); // PRE: amount in dollars and amount > $0 // POST: amount had been added on to balance double getBalance(); // POST: FCTVAL == balance private: string name;// full name of account holder double balance;// how much in the acct, in dollars }; necessary headers ‘class’ keyword to signal a class definition name of the class declarations of the member functions of the class (public section) declarations of the member data of the class (private section) note the semicolon!!!

12 12 What do public and private mean?  Public  can be accessed by any function in any class  Private  can only be accessed by functions who are members of the same class  Observations  public member functions  private data  why?  abstraction and information hiding  protect the data from accidental changes

13 13 Some more observations  Data are declared, but not initialized  Functions are declared, but not implemented  PRE- and POST- conditions are essential  Agreement between client and implementer  How to use  How to implement

14 14 So how do we use it?  For now, we will be the client or user.  Create a bankAccount using a special function called a constructor.  The constructor we have: bankAccount();  Called in odd way…  in the declaration: bankAccount myAcct;

15 15 So how do we use it?  To “do stuff” to or with our object, we need to send it messages.  Use the dot notation we learned for strings.  objectName. operation(parameters?)  Why?  need to say WHICH object to send the message to  Example  Given: bankAccount myAcct;  To deposit $50, myAcct.deposit(50);

16 16 Time for you to think…  Create two accounts.  Deposit $100 in the first and $75 in the second.  Then withdraw $50 from both.

17 17 Time for you to think…  Create two accounts.  bankAccount acct1;  bankAccount acct2;  Deposit $100 in the first and $75 in the second.  acct1.deposit(100);  acct2.deposit(75);  Then withdraw $50 from both.  acct1.withdraw(50);  acct2.withdraw(50); name balance acct1 $0.00 ? name balance acct2 $0.00 ? name balance acct1 $100.00 ? name balance acct2 $75.00 ? name balance acct1 $50.00 ? name balance acct2 $25.00 ?

18 18 More thinking… (Yeah, I know it’s 8 a.m. and you’d rather be sleeping…)  How can we print out how much money we have in each account?  balance is private  must send a message!  cout << acct1.getBalance(); // displays 50  cout << acct2.getBalance(); // displays 25  Our bankAccount interface isn’t perfect…  How could it be improved? name balance acct1 $50.00 ? name balance acct2 $25.00 ?

19 19 Oooh… even more thinking…  Just like with primitive data types (int, double, etc.), we can create arrays of objects.  ex: bankAccount employees[100];  Exercise: It’s payday. Put $500 in each employee’s account.  for(int i = 0; i < 99; i++) { employees[i].deposit(500); }

20 20 A Preview of Implementation  Implementation of class member functions is similar to implementing nonmember functions  Function headers vs. function prototypes  The main difference:  Member functions need to know what class they belong to (their scope)  Scope resolution operator ( :: )

21 21 A Preview of Implementation  Implementation of two bankAccount functions:  bankAccount::bankAccount() // POST: default bankAccount object constructed with // name == “?” and balance == 0 { name = “?”; balance = 0; }  void bankAccount::deposit(int amount) // PRE: amount in dollars and amount > $0 // POST: amount had been added on to balance { balance = balance + amount; } private member data scope resolution

22 22 Types of Functions  Class member functions are classified into three categories:  constructors  create objects (allocate memory, set initial state)  modifiers  change the state of objects  accessors  make information about the state of the object available outside the class

23 23 Exercise  Classify the functions of bankAccount  constructors  bankAccount();  modifiers  void withdraw(int amount);  void deposit(int amount);  accessors  double getBalance();

24 24 Where have we been?  Class interface  stores declarations of private data members  stores declarations of public member functions  constructors, modifiers, accessors  Objects are instances of classes  Use dot notation to send messages to objects to change them or get information about them.

25 25 Where do we go from here?  Creating our own classes  Interface  In-depth look at each kind of function  Implementation  More careful look at implementation  Look at how to implement each kind of function

26 26 Practice  For next lecture: think of one more constructor, modifier, and accessor that would be good additions to the bankAccount class.  Write a class interface for a (library) book class.  Reminder: Homework 5 is due Monday.


Download ppt "1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan."

Similar presentations


Ads by Google