1 / 18

Classes

Classes. Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program Example: a zoo, a gradebook. OOD Goals. Robustness Gracefully handle failures Adaptability Evolve as necessary Reusability Many programs use same piece of code.

ella
Download Presentation

Classes

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

  2. Object-Oriented Design • Method for designing computer programs • Consider “objects” interacting in the program • Example: a zoo, a gradebook

  3. OOD Goals • Robustness • Gracefully handle failures • Adaptability • Evolve as necessary • Reusability • Many programs use same piece of code

  4. OOD Principles • Abstraction • Abstract Data Types (ADTs) • Interfaces • Encapsulation • Information Hiding • Modularity • Easily plug together components

  5. What is a class? • Data and the functions (methods) that operate on that data – collectively called members • Example: bank account class • Provide structure for organizing programs • Almost like an advanced struct

  6. Syntax class classname { private: … public: … } • private only accessible within the class • public accessible from anywhere (like with a struct)

  7. Member Functions • Typically, data (variables) declared private • Functions operate on data • accessor functions – read data, provide access to data but do not change it • update functions – change data • examples from bank account, zoo??? • constructor – builds a new object

  8. Writing Classes • Typically, two files header and source • Header declares member variables, provides function prototypes and bodies of short functions • Source provides code for rest of functions • Allows separation of interface and implementation • Reduces compile time

  9. Bank Account Interface • BankAccount(double initial_balance); • void withdraw(double amount); • void deposit(double amount); • double checkBalance() const;

  10. Creating and Using Objects BankAccount b(500); //Type Name(constructor parameters); //how would you withdraw funds?

  11. Creating and Using Objects BankAccount b(500); Type Name(constructor parameters); //how would you withdraw funds? b.withdraw(300); object_name.method_name(param list);

  12. Constructor BankAccount::BankAccount(double init_balance) class_name(parameter list) • Special-case function called when a new object is created • Used to initialize member variables • Examples? • Default constructor takes no parameters

  13. Alternative Initialization BankAccount::BankAccount(double init_balance) { balance = init_balance; } BankAccount::BankAccount(double init_balance=0) : balance(init_balance) { }

  14. Destructor BankAccount::~BankAccount() ~class_name • Used if class allocates memory dynamically (uses new) • Delete all dynamically declared objects

  15. const • const member function will not change any class variables • double checkBalance() const {…}

  16. friend • Allow a class to access the private members of another class • Operator overloading • Closely related classes

  17. static • Static class member - a global variable with scope the same as a class member • 1 per class, not per object • Example - car serial number

  18. More Examples • An appointment book/calendar • A tic tac toe game • Airline example

More Related