Lecture 16 - Defining New Types

Lecture 16 - Defining New Types - DefiningNewTypes...

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 DocumentRight 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  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 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 DocumentRight 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 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 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 DocumentRight 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 DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/29/2011 for the course CSC 1254 taught by Professor Blanks,l during the Fall '08 term at LSU.

Page1 / 26

Lecture 16 - Defining New Types - DefiningNewTypes...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online