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.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Monday, Feb 10, 2003Kate Gregory with material from Deitel and Deitel Week 6 Lab 2 is marked Hand in Lab 3 Questions from Last Week Operator Overloading.
Monday, Mar 24, 2003Kate Gregory with material from Deitel and Deitel Week 11 Exceptions.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
More C++ Features True object initialisation
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Chapter 9 Classes: A Deeper Look, Part I Part II.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 CSC241: Object Oriented Programming Lecture No 05.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Yan Shi CS/SE 2630 Lecture Notes
Learning Objectives Pointers as dada members
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Dynamically Allocated Memory
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Constructors and Destructors
Dynamic Memory A whole heap of fun….
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Templates CMSC 202, Version 4/02.
Presentation transcript:

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

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Schedule

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

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

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel What is an Object?

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

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) { // }

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

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

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

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

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

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

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

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

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

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?

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?

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

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

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

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

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 }

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

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel this class Task { private: Resource* person; public: void Assign(Resource* res); // etc };

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel this class Resource { private: Task* job; public: void Introduce(Task* t); // etc };

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

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

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

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

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

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel new Calls the Constructor Employee* e = new Employee(); Employee* e = new Employee(“Kate”, );

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;

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Using Object Pointers e->SetSalary( ); cout GetSalary();

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

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

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;

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

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

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

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;

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

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;

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

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;

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

Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel For Next class Read chapter 8 Do Lab 3