1 / 20

Lec5: Objects

Lec5: Objects. Success in Computer Science. There is no “CS gene” Time, practice, and efficient effort leads to success In class: Pay attention in class, T ry all examples – don’t wait for answer A sk questions, dare to be wrong Assignments: Start early Work a little each day / evening

fritzi
Download Presentation

Lec5: Objects

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. Lec5: Objects

  2. Success in Computer Science • There is no “CS gene” • Time, practice, and efficient effort leads to success • In class: • Pay attention in class, • Try all examples – don’t wait for answer • Ask questions, dare to be wrong • Assignments: • Start early • Work a little each day / evening • Look at code examples and make sure you understand them • Refer back to your CS 16 textbook and notes • Come to office hours or e-mail when you are stuck with no progress for an hour

  3. Objects • Reuse code • The same object can be used by many programs • Objects can be extended to make new objects • Cut down on programmer bugs • keep implementation details private

  4. Abstract Data Type3 levels • Application (or user) level: • A high-level or real-life concept • What it looks like to the application developer or user • The code that uses the class

  5. Abstract Data Type3 levels • Logical (or abstract) level: • The specification or interface • Abstract view of the data values (the domain) and the set of operations to manipulate them • the details of the interface necessary to use that common every-day concept • The public parts of the .h file that defines the class

  6. Abstract Data Type3 levels • Implementation level • Variables and their types to represent data • Algorithms used to implement methods • The private aspects of the .h file • The .cpp file that implements the class

  7. Object-Oriented Programming • 50% design • 20% syntax • 30% conventions

  8. Object-Oriented Design:Rectangle • What information does a rectangle need to store (data)? • What things would you want to do with a rectangle?

  9. Object-Oriented Design:Rectangle • What information does a rectangle need to store (data)? • height, width, color, filled, position • What things would you want to do with a rectangle? • calculate area, calculate perimeter, calculate diagonal length, compare two rectangles

  10. C vs C++: Differences on handout • Getters & Setters in the C++ code • C uses malloc, C++ uses new • C++: classname:: • C++: bool variable type • C++: using namespace std; • C++ uses different libraries for the #include • C++ uses a constructor • Classes have methods, but structs do not • C++ code uses const, my C code did not • C++ uses “private” and “public”

  11. C++ uses “private” and “public” • Classes have a private portion and a public portion • Design: Only a need to know basis • Keep everything private that you can • Usually, you want data to be private • You provide public methods • You can also write private methods – these are called by public methods, but not by anything outside of the class

  12. Classes provide “setters” and “getters” • Getters (Accessors): return each of the variable values that you want others to be able to see (read) • Setters (Mutators): allow others to change variables in the object (write) • Class – int – the specification of a type - recipe • Objects – x, the variable – instantiation of the class - cookie

  13. const keyword • const char *getString(const Rectangle *x, int *w) const; • 1) the return value may not be changed • 2) the input parameter may not be changed during this call • 3) nothing in the object may be changed during this call

  14. C uses malloc, C++ uses newC++ has a constructor • int *x = (int *)malloc(sizeof(int)); • int *x = new int; • int *iarray = (int *)malloc(sizeof(int)*5); • int *iarray = new int[5] • int x = 5; • int *x = (int *)malloc… ; *x = 5; • Rectangle *rect; • rect = new Rectangle(“Hi!”,5, 7); • Rectangle r(“Bonnie”,5,7); • r.setWidth(10); • char *n = r.getName(); • n[5] = ‘a’; • r.setName(“Howdy”); • C – you need to malloc and initialize separately • C++, when you call new on an object, the constructor is automatically called so that you can initialize your new object

  15. Why void vsbool in setters? • For setName, I allocated space, which can fail • setting an integer can never fail

  16. C++: using namespace std;C++ uses different libraries for the #include • C++: using namespace std; C: #include <stdlib.h> • C++: #include <iostream>: C: #include <stdio.h> • To use C libraries within a C++ program: • #include <cstdlib> or #include <cstdio> or #include <cstring>

  17. C++: classname:: • Scope – what code can “see” a variable • Lifetime – how long the variable exists • Local variables • scope: Within the function or code block • lifetime: Within the function or code block • Instance variable • variables declared in a class / object • private instance variable: • scope: within any methods in the class • lifetime: as long as the object lives • public instance variable • scope: anything that can “see” the object • lifetime: as long as the object lives

  18. C++: bool variable type • true and false

  19. delete function • malloc has free • new has delete • There are two ways to call new • int *x = new int; • int *y = new int[5]; • delete x; • delete[] y;

  20. input parameter • All functions in C had an extra parameter – rectangle * • In C++, that parameter is also there, but it is always named “this”, and it doesn’t appear on the line. • Because methods are called with variable->methodname rather than just functionname, the compiler already knows what variable it is

More Related