week 4
DESCRIPTION
Week 4. Questions from Last Week Hand in Lab 2 Classes. Data Abstraction. An extension of the concept of a structure Gathers together a number of related variables e.g. Bank Account: Owner Balance transactions. Defining your own Types. Built-in types have allowed operations - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/1.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Week 4
• Questions from Last Week
• Hand in Lab 2
• Classes
![Page 2: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/2.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Date Week Topic ChapterHand Out Due Back Test
6-Jan-03 1Administrivia / Overview / Intro to C++ / Control Structures 1
13-Jan-03 2 Functions / Arrays, Pointers, Strings 2,3,4,5Lab 1 / Lab 2
20-Jan-03 3 Classes, Data Abstraction 6 Lab 1 5%27-Jan-03 4 More on Classes 7 Lab 3 Lab 2 5%3-Feb-03 5 No Lecture
10-Feb-03 6 Operator Overloading 8 Lab 4 Lab 3 5%17-Feb-03 Reading Break24-Feb-03 7 Inheritance 9 Lab 4 5% Midterm 25%3-Mar-03 8 Virtual Functions and Polymorphism 10 Lab 5
10-Mar-03 9 Stream IO 11 Lab 6 Lab 5 5%17-Mar-03 10 Templates 12,13 Lab 7 Lab 6 5%24-Mar-03 11 Exceptions31-Mar-03 12 File IO 14 Lab 7 5%
??? Exam Final 40%
![Page 3: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/3.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Data Abstraction
• An extension of the concept of a structure
• Gathers together a number of related variables
• e.g. Bank Account:– Owner– Balance– transactions
![Page 4: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/4.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Defining your own Types
• Built-in types have allowed operations– Can’t multiply strings– Can’t use ++ on a float– Can’t pass an integer to strlen()
• In the same vein, you choose the operations for your own types– Bank account can have deposit and withdraw
![Page 5: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/5.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
An Object is a Clump
• Information, Variables, Data, State, Attributes
• Behaviour, Methods, Functions, Operations, Services
• All belong together
![Page 6: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/6.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Defining a Class
//BankAccount.h
class BankAccount
{
float Balance;
void Deposit(float amount);
bool Withdraw (float amount);
};• Don’t forget that final semi colon!
![Page 7: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/7.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Inside and Outside
PrivateA ttributes
andm ethods
Public A ttributesand M ethods
System
M yO bject
![Page 8: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/8.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Defining a Class
//BankAccount.h class BankAccount{private: float Balance;public: void Deposit(float amount); bool Withdraw (float amount);};
![Page 9: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/9.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Creating Objects
• An object is an instance of a class
• Class is BankAccount: object is my account, another object is someone else’s
#include BankAccount.h
// ...
BankAccount KateChequing;
BankAccount BillSaving;
![Page 10: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/10.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Private means no access
• All code can access public functions
KateChequing.Deposit(50);
• But not private variables
KateChequing.Balance = 100;
![Page 11: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/11.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Implementing a Class
//BankAccount.cpp #include “BankAccount.h”void BankAccount::Deposit(float amount)
{ Balance += amount;}
• :: is called Scope Resolution Operator
![Page 12: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/12.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Encapsulation
• Holding all the rules together• Not forcing users of a class to know what the
inside means• Retaining the freedom to change the inside of a
class– Variable name– Type (int, float)– Units or other “meaning”
• Information hiding – design information
![Page 13: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/13.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Gets and Sets
class Truck
{
private:
float currentweight;
public:
float getcurrentweight();
void setcurrentweight(float w);
};
![Page 14: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/14.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Constructors
• Sometimes your only motivation for a set is to give something an initial value
• A constructor can be used to initialize the member variables of an object as it is created
• Name is always the name of the class• Never a return type• Parameters vary
![Page 15: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/15.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Default Constructor
• No parameters• Runs when instances are created without
parameters
class BankAccount
{
// ...
BankAccount();
};
![Page 16: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/16.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Default Constructor
//BankAccount.cpp
#include BankAccount.h
BankAccount::BankAccount()
{
Balance = 0;
}
![Page 17: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/17.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Overloaded Functions
• In C, two functions cannot have the same name
• In C++, they can, as long as something else differs:– Class– Parameters
• Return type or placeholder names don’t matter
![Page 18: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/18.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Overloaded Constructors
class BankAccount
{
// ...
BankAccount();
BankAccount(float openingbalance);
BankAccount(float a); // not allowed
BankAccount(char* name);
};
![Page 19: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/19.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Passing Parameters to Constructors
BankAccount KateChequing(50.0);• Uses the constructor that takes a float
BankAccount BillSaving(“Bill”);• Uses the constructor that takes a char*
BankAccount KateChequing(1000);• Uses the constructor that takes a float, and
converts the int to float first
![Page 20: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/20.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Don’t Do This
BankAccount KateChequing();• This is declaring a function called
KateChequing!
BankAccount GetAccount(int x);
![Page 21: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/21.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Initializer Syntax
//BankAccount.cpp
#include BankAccount.h
BankAccount::BankAccount() :
Balance(0)
{
}• Slightly more efficient and readable
• Becomes important once inheritance enters the picture
• Also useful when a member variable is actually an object
![Page 22: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/22.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Destructors
• Opposite of constructors
• Name is ~ plus name of class– Joke based on boolean syntax
• Never take arguments
• No return type
• Rarely explicitly called
![Page 23: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/23.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Leaving Scope
// ... Program fragment
{
int i;
BankAccount Kate(1000);
}
• Destructor runs as Kate leaves scope
![Page 24: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/24.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Coding Destructors
class BankAccount{// ... ~BankAccount(); };
//BankAccount.cpp #include BankAccount.hBankAccount::~BankAccount(){ cout << “There goes your money”;}
![Page 25: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/25.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Reuse Thoughts• Objects should be self contained• Constructors should set good default values• If there is no default value for something, every
constructor should demand it, so that it cannot remain uninitialized
• You should add all the methods others are likely to use
• Don’t add gets and sets without thinking• The object should handle its own error checking
– Avoid side-effects such as error messages
![Page 26: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/26.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Inline functions
• Dramatically improve performance
• Compiler actually expands the code like a macro
• You retain the protection of encapsulation at no performance cost
![Page 27: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/27.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Inline Gets and Sets
class Truck{private: float currentweight;public: float getcurrentweight() { return currentweight; } void setcurrentweight(float w) { currentweight = w;}};
![Page 28: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/28.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Error checking
class Truck{private: float currentweight;public: float getcurrentweight() { return currentweight; } void setcurrentweight(float w) { if (w > 0) currentweight = w;}};
![Page 29: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/29.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
Private Functions
• Serve some useful purpose
• You don’t want the whole system to be able to call them
• You aren’t willing to commit they will always exist
• Can only be called from inside the object itself
![Page 30: Week 4](https://reader035.vdocument.in/reader035/viewer/2022062718/56812ca8550346895d91523c/html5/thumbnails/30.jpg)
Monday, Jan 27, 2003 Kate Gregorywith material from Deitel and Deitel
For Next class
• Read chapter 7
• Get ready for Lab 3 to be handed out