{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 16 - Defining New Types

Lecture 16 - Defining New Types - Defining New Types...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Defining New Types Lecture 22 Hartmut Kaiser [email protected] http://www.cct.lsu.edu/˜ hkaiser/fall_2011/csc1254.html
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Programming Principle of the Day Hide Implementation Details Hiding implementation details allows change to the implementation of a code component while minimally affecting any other modules that use that component. Encapsulation is key A language mechanism for restricting access to some of the object's components A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data http://en.wikipedia.org/wiki/Information_Hiding http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) 11/10/2011 Lecture 22 CSC 1254, Fall 2011, Defining New Types 2
Background image of page 2
Defining New Types C++ has two kinds of types built-in types int, double, char, etc. class types (user defined types) string, vector, istream Rely entirely on built-in types and other class types and generally available language facilities Design of C++ rests on the idea of creating user defined types usable similar to built-in types Requires substantial language support Taste and judgment in class design 11/10/2011 Lecture 22 CSC 1254, Fall 2011, Defining New Types 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
student_info Type Revisted We wrote the student_info type and related functions However, these were not well suited for other programmers to use Any newly created student_info object required first to read data into it Otherwise the values would have been uninitialized No way to check whether held data was valid Well, without looking at member values Requires internal knowledge of student_info Assumption was that after grades were read they wouldn’t change Interface to student_info was scattered (separate, unrelated functions) 11/10/2011 Lecture 22 CSC 1254, Fall 2011, Defining New Types 4
Background image of page 4
Class (User Defined) Types Class types allow to group several data items into one entity That’s what we used: struct student_info { std::string name; double midterm, final; std::vector< double > homework; }; Four data elements: data members A string, two double’s, and a vector<double> 11/10/2011 Lecture 22 CSC 1254, Fall 2011, Defining New Types 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Class (User Defined) Types Programmers may and must handle those data items directly They may because nothing is preventing them They must because there is no other way It would be better to hide implementation details of how things are stored Manipulate members through functions only This forms the interface of our class 11/10/2011 Lecture 22 CSC 1254, Fall 2011, Defining New Types 6
Background image of page 6
Class (User Defined) Types Important general remarks
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}