300 likes | 381 Views
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
E N D
Week 4 • Questions from Last Week • Hand in Lab 2 • Classes 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 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 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 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! Kate Gregorywith material from Deitel and Deitel
Inside and Outside 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); }; 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; 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; 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 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 Kate Gregorywith material from Deitel and Deitel
Gets and Sets class Truck { private: float currentweight; public: float getcurrentweight(); void setcurrentweight(float w); }; 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 Kate Gregorywith material from Deitel and Deitel
Default Constructor • No parameters • Runs when instances are created without parameters class BankAccount { // ... BankAccount(); }; Kate Gregorywith material from Deitel and Deitel
Default Constructor //BankAccount.cpp #include BankAccount.h BankAccount::BankAccount() { Balance = 0; } 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 Kate Gregorywith material from Deitel and Deitel
Overloaded Constructors class BankAccount { // ... BankAccount(); BankAccount(float openingbalance); BankAccount(float a); // not allowed BankAccount(char* name); }; 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 Kate Gregorywith material from Deitel and Deitel
Don’t Do This BankAccount KateChequing(); • This is declaring a function called KateChequing! BankAccount GetAccount(int x); 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 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 Kate Gregorywith material from Deitel and Deitel
Leaving Scope // ... Program fragment { int i; BankAccount Kate(1000); } • Destructor runs as Kate leaves scope Kate Gregorywith material from Deitel and Deitel
Coding Destructors class BankAccount { // ... ~BankAccount(); }; //BankAccount.cpp #include BankAccount.h BankAccount::~BankAccount() { cout << “There goes your money”; } 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 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 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;} }; 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;} }; 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 Kate Gregorywith material from Deitel and Deitel
For Next class • Read chapter 7 • Get ready for Lab 3 to be handed out Kate Gregorywith material from Deitel and Deitel