1 / 8

ENERGY 211 / CME 211

ENERGY 211 / CME 211. Lecture 16 October 27, 2008. Classes. Classes can be used to implement types representing complex objects An object is an instance of a class, that Stores data that define its characteristics Is associated with a set of operations that work with its own data

delu
Download Presentation

ENERGY 211 / CME 211

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ENERGY 211 / CME 211 Lecture 16 October 27, 2008

  2. Classes • Classes can be used to implement types representing complex objects • An object is an instance of a class, that • Stores data that define its characteristics • Is associated with a set of operations that work with its own data • string, vector<> are classes • Classes support encapsulation, which allows implementation details to be hidden from outside code

  3. Declaring Classes • Form: classclass-name{ access-specifier1:decl-list1 access-specifier2:decl-list2 … }; • access-specifier is either public, private or protected • decl-list is a list of variable or function declarations of the class' members

  4. public and private • Members declared public: are accessible to functions outside the class • Example: size() is a public member of the string class • Operations on objects usually public • private members are hidden from outside functions • Member variables, helper functions used to implement operations are usually private

  5. Implementing Classes • To define member functions outside declaration: ret-typeclass-name::func-name(type1arg1,type2arg2, …)compound-stmt • Member variables are already declared and available, and store the data of the instance on which the function operates • Members that do not modify the underlying object should be declared const (use after argument list)

  6. Overloaded Operators • In classes that are value types, it is common practice to overload certain operators to support "expected" operations • >> to convert text into instance data • << to output a string representation • = to copy from one instance to another • ==, != to compare instances • <, >, <=, => if values can be ordered

  7. Testing Classes • When you implement a class, you should write a suite of functions that will test each member function • Use a main() function that will run each test, and produce output that reports success or failure • Often, implementation changes, and you should repeat tests to ensure nothing is broken (regression testing) • Things will be broken!

  8. Next Time More about simple classes • Special member functions • Constructors • Destructors • Copy constructors, overloading = • Implementation, header and application files

More Related