200 likes | 299 Views
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
E N D
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
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
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
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
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
Object-Oriented Programming • 50% design • 20% syntax • 30% conventions
Object-Oriented Design:Rectangle • What information does a rectangle need to store (data)? • What things would you want to do with a rectangle?
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
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”
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
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
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
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
Why void vsbool in setters? • For setName, I allocated space, which can fail • setting an integer can never fail
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>
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
C++: bool variable type • true and false
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;
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