Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Week 5 Lab 1 comments Hand in...
-
Upload
rudolph-booth -
Category
Documents
-
view
214 -
download
0
Transcript of Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Week 5 Lab 1 comments Hand in...
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Week 5
• Lab 1 comments
• Hand in Lab 2
• Questions from Last Week
• Classes continued
• Lab 3
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Date Week Topic ChapterHand Out Due Back Test
6-Jan-03 1Administrivia / Overview / Intro to C++ / Control Structures 1
13-Jan-03 2 Functions / Arrays, Pointers, Strings 2,3,4,5Lab 1 / Lab 2
20-Jan-03 3 Classes, Data Abstraction 6 Lab 1 5%27-Jan-03 4 More on Classes 7 Lab 3 Lab 2 5%
3-Feb-03 5 No Lecture10-Feb-03 6 Operator Overloading 8 Lab 4 Lab 3 5%17-Feb-03 Reading Break24-Feb-03 7 Inheritance 9 Lab 4 5% Midterm 25%3-Mar-03 8 Virtual Functions and Polymorphism 10 Lab 5
10-Mar-03 9 Stream IO 11 Lab 6 Lab 5 5%17-Mar-03 10 Templates 12,13 Lab 7 Lab 6 5%24-Mar-03 11 Exceptions31-Mar-03 12 File IO 14 Lab 7 5%
??? Exam Final 40%
Schedule
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Lab 1 comments
• Spacing in your code
x=2*b+3;
x = 2 * b + 3;
cout << "x is " << x << endl; • Use your own sample values
• Many of you overcharged the parkers
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
What is an Object?
PrivateA ttributes
andm ethods
Public A ttributesand M ethods
System
M yO bject
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Const arguments
SomeClass::Foo1(Huge h)
{ // }
SomeClass::Foo2(Huge& h)
{ // }
SomeClass::Foo3(const Huge& h)
{ // }
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Const member functions
SomeClass::Foo3(const Huge& h)
{
h.x = 2;
h.setwidth(3);
h.display();
}
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Const member functions
class Huge
{
public:
int x;
Huge();
void setwidth(int w);
void display();
};
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Const member functions
class Huge
{
public:
int x;
Huge();
void setwidth(int w) const;
void display() const;
};
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Const member functions
void Huge::display() const
{
cout << x;
}
void Huge::setwidth(int w) const
{
x = w;
}
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Const member functions
class Huge
{
public:
int x;
Huge();
void setwidth(int w);
void display() const;
};
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Const member functions
SomeClass::Foo3(const Huge& h)
{
//h.x = 2;
//h.setwidth(3);
h.display();
}
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Objects as Member Variables
class Employee
{
private:
float salary;
char* name;
Date startdate;
// rest of class
};
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith 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?
Monday, Feb 3, 2003 Kate Gregorywith 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?
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Use the Constructor
Employee::Employee(char* n, float f): startdate(“today”), name(n), salary(f)
{
}
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
friend
class A
{
friend int B::foo(A arg);
private:
int a;
// rest of class
};
Monday, Feb 3, 2003 Kate Gregorywith 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}
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
this
class Task
{
private:
Resource* person;
public:
void Assign(Resource* res);
// etc
};
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
this
class Resource
{
private:
Task* job;
public:
void Introduce(Task* t);
// etc
};
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith 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( ??? );}
Monday, Feb 3, 2003 Kate Gregorywith 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 );}
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
• 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
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
new Calls the Constructor
Employee* e = new Employee();
Employee* e = new Employee(“Kate”, 1000000.01);
Monday, Feb 3, 2003 Kate Gregorywith 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;
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Using Object Pointers
e->SetSalary(100000000);
cout << e->GetSalary();
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
delete[]
Employee* e = new Employee();
Employee* emparray = new Employee[10];
// use them
delete e;
delete[] emparray;
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Static variables
class BankAccount
{
private:
float balance;
float interestrate;
public:
// gets and sets
// etc
};
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Static variables
BankAccount* accounts = new BankAccount[1000];
// work with them
• Uh-oh: time to change the interest rate
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Alternatives
• Global variable– Namespace issues
– No encapsulation
• Accept the performance hit of thousands of copies• Use a static variable
Monday, Feb 3, 2003 Kate Gregorywith 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;
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Static variables
accounts[0].interestrate = 0.05;
• Even if it were public this wouldn’t be nice
Monday, Feb 3, 2003 Kate Gregorywith 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;
Monday, Feb 3, 2003 Kate Gregorywith 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
Monday, Feb 3, 2003 Kate Gregorywith 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;
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
Static functions
BankAccount::setrate(0.05);
• Sets the interest rate for everyone• Feels better too
Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel
For Next class
• Read chapter 8
• Do Lab 3