1 / 30

Week 4

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

Download Presentation

Week 4

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. Week 4 • Questions from Last Week • Hand in Lab 2 • Classes Kate Gregorywith material from Deitel and Deitel

  2. Kate Gregorywith material from Deitel and Deitel

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

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

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

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

  7. Inside and Outside Kate Gregorywith material from Deitel and Deitel

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

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

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

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

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

  13. Gets and Sets class Truck { private: float currentweight; public: float getcurrentweight(); void setcurrentweight(float w); }; Kate Gregorywith material from Deitel and Deitel

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

  15. Default Constructor • No parameters • Runs when instances are created without parameters class BankAccount { // ... BankAccount(); }; Kate Gregorywith material from Deitel and Deitel

  16. Default Constructor //BankAccount.cpp #include BankAccount.h BankAccount::BankAccount() { Balance = 0; } Kate Gregorywith material from Deitel and Deitel

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

  18. Overloaded Constructors class BankAccount { // ... BankAccount(); BankAccount(float openingbalance); BankAccount(float a); // not allowed BankAccount(char* name); }; Kate Gregorywith material from Deitel and Deitel

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

  20. Don’t Do This BankAccount KateChequing(); • This is declaring a function called KateChequing! BankAccount GetAccount(int x); Kate Gregorywith material from Deitel and Deitel

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

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

  23. Leaving Scope // ... Program fragment { int i; BankAccount Kate(1000); } • Destructor runs as Kate leaves scope Kate Gregorywith material from Deitel and Deitel

  24. Coding Destructors class BankAccount { // ... ~BankAccount(); }; //BankAccount.cpp #include BankAccount.h BankAccount::~BankAccount() { cout << “There goes your money”; } Kate Gregorywith material from Deitel and Deitel

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

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

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

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

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

  30. For Next class • Read chapter 7 • Get ready for Lab 3 to be handed out Kate Gregorywith material from Deitel and Deitel

More Related