Lecture 16 - Defining New Types

Lecture 16 - Defining New Types - DefiningNewTypes...

Info icon This 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  
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  11/10/ 2011  Lecture  22 CSC 1254,  Fall 2011,  Defining  New Types 2
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
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 11/10/ 2011  Lecture  22 CSC 1254,  Fall 2011,  Defining  New Types 4
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
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
Image of page 6
Class (User Defined) Types
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern