Download presentation
Presentation is loading. Please wait.
Published byRudolph Booth Modified over 9 years ago
1
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Week 5 Lab 1 comments Hand in Lab 2 Questions from Last Week Classes continued Lab 3
2
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Schedule
3
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Lab 1 comments Spacing in your code x=2*b+3; cout << "x is " << x << endl; Use your own sample values Many of you overcharged the parkers
4
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Lab 1 comments Read the question –Any negative value to quit is not the same as –1 to quit –Extra testing (e.g. maximum sales, maximum parking time) is not always helpful –Developers must create code that meets requirements, not their improvements on requirements
5
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel What is an Object?
6
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const You can insist that a variable’s value never be changed: const int a = 3; a = 4; //will not compile
7
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const arguments SomeClass::Foo1(Huge h) { // } SomeClass::Foo2(Huge& h) { // } SomeClass::Foo3(const Huge& h) { // }
8
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const member functions SomeClass::Foo3(const Huge& h) { h.x = 2; h.setwidth(3); h.display(); }
9
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const member functions class Huge { public: int x; Huge(); void setwidth(int w); void display(); };
10
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const member functions class Huge { public: int x; Huge(); void setwidth(int w) const; void display() const; };
11
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const member functions void Huge::display() const { cout << x; } void Huge::setwidth(int w) const { x = w; }
12
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const member functions class Huge { public: int x; Huge(); void setwidth(int w); void display() const; };
13
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const member functions SomeClass::Foo3(const Huge& h) { //h.x = 2; //h.setwidth(3); h.display(); }
14
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Const tips Make member functions const as a default –If they change a member variable, take the const away Include const in your design from the very beginning –Adding const after the fact is miserably hard Use const instead of #define
15
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Objects as Member Variables class Employee { private: float salary; char* name; Date startdate; // rest of class };
16
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Objects as Ordinary Member Variables void Employee::Display() { cout << “name is “ << name << endl; cout << “salary is “ << salary << endl ; cout << “startdate is “ << startdate.FormatLongDate() << endl; } No special treatment because they are objects or because they are member variables
17
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Initializing Objects Employee::Employee(char* n, float f) { name = n; salary = f; } How do I set the startdate to today?
18
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Initializing Objects Employee::Employee(char* n, float f) { name = n; salary = f; startdate.set(“today”); } What if no public set function has been written?
19
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Use the Constructor Employee::Employee(char* n, float f): startdate(“today”) { name = n; salary = f; } Use the public constructor Actually improves performance
20
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Use the Constructor Employee::Employee(char* n, float f): startdate(“today”), name(n), salary(f) { }
21
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel The friend keyword Making exceptions to the rules A function can become an honourary member of another class It can violate encapsulation, but by avoiding a public set or get it might be the lesser of two evils
22
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel friend class A { friend int B::foo(A arg); private: int a; // rest of class };
23
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel friend int B::foo(A arg) { arg.a = 3; // access to private a OK } int B::bar(A arg) { arg.a = 3; // access to private a not OK }
24
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel friend Tips Use very sparingly Grant access to one function at a time, not the whole class Put the friend statements right before all the private variables in the class definition
25
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel this class Task { private: Resource* person; public: void Assign(Resource* res); // etc };
26
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel this class Resource { private: Task* job; public: void Introduce(Task* t); // etc };
27
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel this Resource* r; // get a pointer to a resource and put it in r Task* t; // get a pointer to a task and put it in t r->Introduce(t); t->Assign(r); Programmer must remember to call both methods
28
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Enforce your Business Rules void Task::Assign(Resource* res) { person = res; } void Resource::Introduce(Task* job) { t = job; t->Assign( ??? ); }
29
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel this is a useful pointer void Task::Assign(Resource* res) { person = res; } void Resource::Introduce(Task* job) { t = job; t->Assign( this ); }
30
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel More on this The type varies: it’s a pointer to whatever kind of object you’re in If you’re not in a member function, there is no this It’s incredibly useful for operator overloads
31
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Dynamic Memory Allocation The old way of getting memory: int* i = (int*) malloc(sizeof (int)); –Compiler must be told everything The new way: int* i = new int(); –Compiler knows size, type, etc
32
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel new Calls the Constructor Employee* e = new Employee(); Employee* e = new Employee(“Kate”, 1000000.01);
33
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel new Allocates on the Heap Pointers to heap memory can be passed around Memory remains allocated until explicitly freed delete e;
34
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Using Object Pointers e->SetSalary(100000000); cout GetSalary();
35
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel delete delete calls the destructor Do not mix and match malloc/free with new/delete Good practice to set pointers to NULL after using delete
36
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Dynamic Array Allocation int* intarray = new int[10]; Employee* employeearray = new Employee[10]; No way to get parameters to constructors
37
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel delete[] Employee* e = new Employee(); Employee* emparray = new Employee[10]; // use them delete e; delete[] emparray;
38
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static variables class BankAccount { private: float balance; float interestrate; public: // gets and sets // etc };
39
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static variables BankAccount* accounts = new BankAccount[1000]; // work with them Uh-oh: time to change the interest rate
40
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Alternatives Global variable –Namespace issues –No encapsulation Accept the performance hit of thousands of copies Use a static variable
41
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static variables class BankAccount { private: float balance; static float interestrate; public: // gets and sets // etc }; // in another file float BankAccount::interestrate = 0.1;
42
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static variables accounts[0].interestrate = 0.05; Even if it were public this wouldn’t be nice
43
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static variables class BankAccount { private: float balance; static float interestrate; public: void setrate(float f) {interestrate = f;} }; // in another file float BankAccount::interestrate = 0.1;
44
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static variables accounts[0].setrate(0.05); Feels weird accounts[92].setrate(0.05); Both of these set the interest rate for everyone
45
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static functions class BankAccount { private: float balance; static float interestrate; public: static void setrate(float f) {interestrate = f;} }; // in another file float BankAccount::interestrate = 0.1;
46
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Static functions BankAccount::setrate(0.05); Sets the interest rate for everyone Feels better too
47
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel For Next class Read chapter 8 Do Lab 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.