+ All Categories
Home > Documents > Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Week 5 Lab 1 comments Hand in...

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Week 5 Lab 1 comments Hand in...

Date post: 14-Jan-2016
Category:
Upload: rudolph-booth
View: 214 times
Download: 0 times
Share this document with a friend
47
Monday, Feb 3, 2003 Kate Gregory with material from Deitel and Deitel Week 5 • Lab 1 comments • Hand in Lab 2 • Questions from Last Week • Classes continued • Lab 3
Transcript
Page 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.

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

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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)

{ // }

Page 8: 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.

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();

}

Page 9: 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.

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();

};

Page 10: 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.

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;

};

Page 11: 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.

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;

}

Page 12: 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.

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;

};

Page 13: 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.

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();

}

Page 14: 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.

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

Page 15: 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.

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

};

Page 16: 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.

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

Page 17: 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.

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?

Page 18: 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.

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?

Page 19: 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.

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

Page 20: 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.

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)

{

}

Page 21: 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.

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

Page 22: 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.

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

};

Page 23: 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.

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}

Page 24: 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.

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

Page 25: 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.

Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel

this

class Task

{

private:

Resource* person;

public:

void Assign(Resource* res);

// etc

};

Page 26: 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.

Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel

this

class Resource

{

private:

Task* job;

public:

void Introduce(Task* t);

// etc

};

Page 27: 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.

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

Page 28: 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.

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( ??? );}

Page 29: 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.

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 );}

Page 30: 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.

Monday, Feb 3, 2003 Kate Gregorywith 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

Page 31: 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.

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

Page 32: 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.

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);

Page 33: 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.

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;

Page 34: 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.

Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel

Using Object Pointers

e->SetSalary(100000000);

cout << e->GetSalary();

Page 35: 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.

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

Page 36: 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.

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

Page 37: 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.

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;

Page 38: 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.

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

};

Page 39: 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.

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

Page 40: 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.

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

Page 41: 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.

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;

Page 42: 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.

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

Page 43: 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.

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;

Page 44: 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.

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

Page 45: 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.

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;

Page 46: 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.

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

Page 47: 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.

Monday, Feb 3, 2003 Kate Gregorywith material from Deitel and Deitel

For Next class

• Read chapter 8

• Do Lab 3


Recommended