cs31 lecture 17

printableconst printable p printableconst do nothing

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: ructor Always ClassName(const ClassName& o)
 { … } ClassName(const 130 Example struct Printable { std::string content; std::string Printable()
 { Printable()
 content = “undefined”; content } ~Printable()
 { ~Printable()
 /// do nothing. / } Printable(const Printable& p)
 { Printable(const /// do nothing. / } }; 131 Good design habits x A user should not be allowed to read/write the attributes of user a class class x The developer should provide methods to read/write the The attributes attributes Accessor (to access the data)
 Mutator (to alter the data)
 x The usage of references and const is desired (but not The required)
 required)
 132 Example struct Printable { std::string content; std::string Printable()
 { Printable()
 content = “undefined”; content } ~Printable()
 { ~Printable()
 /// do nothing. / } Printable(const Printable& p)
 { Printable(const /// do nothing. / } std::string getContent()
 { return content; } //returns a COPY of content std::string void setContent(std::string c)
 { content = c; } void }; 133 References and Copy x Be careful: by default, a copy is performed Duplication of objects x In many cases, we do not want a copy, but a way to In manipulate the same object with a different name/type manipulate Use references x Reminder: An object is physically stored in memory A reference is a sort of pointer (“address”)
 to this memory slot reference where the objec...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online