Overview Classes in What is a class ? C++98 and C++11 Constructors & . . . What if I don’t write . . . January 10, 2018 Why Prefer Init? Brian A. Malloy Static Class Variabless Principle of Least . . . Interface vs . . . Makefiles Overload Operators ◭◭ ◮◮ ◭ ◮ Slide 1 of 38 Go Back Full Screen Quit
1. Overview Overview What is a class ? • When we refer to C++98, we are referring Constructors & . . . to C++98 and C++03, since they differ What if I don’t write . . . only slightly. Why Prefer Init? • C++98 contained 3 types of constructors, Static Class Variabless but C++11 added a move constructor. Principle of Least . . . • The C++ class is one of the most difficult Interface vs . . . constructs to write correctly Makefiles • Some methods are written silently by the Overload Operators compiler ◭◭ ◮◮ • Some methods are required w/ pointers ◭ ◮ • These slides describe classes, including 3 of the 4 constructors. Slide 2 of 38 • We describe move semantics in separate slides Go Back Full Screen Quit
2. What is a class ? Overview What is a class ? • Unit of encapsulation: Constructors & . . . What if I don’t write . . . – Public operations Why Prefer Init? – Private implementation Static Class Variabless • Abstraction: Principle of Least . . . Interface vs . . . – string: abstracts char* of C Makefiles – student Overload Operators – sprite ◭◭ ◮◮ • C++ Classes: easy to write, hard to get ◭ ◮ right! Slide 3 of 38 • Need lots of examples Go Back Full Screen Quit
2.1. The actions of a class Overview What is a class ? • Constructors: initialize data attributes Constructors & . . . What if I don’t write . . . • Constructors: allocate memory when needed Why Prefer Init? • Destructor: De-allocate memory when nec- Static Class Variabless essary Principle of Least . . . Interface vs . . . Makefiles Overload Operators ◭◭ ◮◮ ◭ ◮ Slide 4 of 38 Go Back Full Screen Quit
2.2. C++ class vs C++ struct Overview What is a class ? • Default access is only difference Constructors & . . . What if I don’t write . . . • Generally, structs used for data Why Prefer Init? • Classes used for data and methods Static Class Variabless Principle of Least . . . Interface vs . . . Bad class Good Class Makefiles class Student { class Student { Overload Operators public: string name; ◭◭ ◮◮ string name; float gpa; ◭ ◮ float gpa; }; }; Slide 5 of 38 Go Back Full Screen Quit
2.3. Object : an instantiated class Overview What is a class ? • C++ objects can be stored on the stack: Constructors & . . . class A{}; What if I don’t write . . . int main() { Why Prefer Init? A a, b; Static Class Variabless }; Principle of Least . . . Interface vs . . . • Or on the heap: Makefiles int main() { Overload Operators A *a = new A; ◭◭ ◮◮ A *b = new B; ◭ ◮ }; Slide 6 of 38 • Compiler does stack; programmer does heap! Go Back Full Screen Quit
3. Constructors & Destructors Overview What is a class ? • No name and cannot be called directly Constructors & . . . • Init data through initialization lists What if I don’t write . . . • Constructor types are distinguished by their Why Prefer Init? parameters. Static Class Variabless • The four types of constructors are: Principle of Least . . . 1. Default Interface vs . . . 2. Conversion Makefiles 3. Copy Overload Operators 4. Move (which we describe in later slides) ◭◭ ◮◮ ◭ ◮ Slide 7 of 38 Go Back Full Screen Quit
Overview Constructor examples: What is a class ? class Student { Constructors & . . . public: What if I don’t write . . . Student(); // default: no params Student(char * n); // convert Why Prefer Init? Student(const Student&); // copy: param is Student Static Class Variabless Student(Student&&); // move Principle of Least . . . ~Student(); // destructor (no params) Interface vs . . . private: char* name; Makefiles }; Overload Operators ◭◭ ◮◮ ◭ ◮ Slide 8 of 38 Go Back Full Screen Quit
3.1. Default Constructor Overview What is a class ? 1 c l a s s s t r i n g { Constructors & . . . 2 public : What if I don’t write . . . 3 s t r i n g () : buf (new char [ 1 ] ) { buf [ 0 ] = ’ \ 0 ’; } Why Prefer Init? 4 private : 5 char ∗ buf ; Static Class Variabless 6 } ; Principle of Least . . . • No parameters to default constructor Interface vs . . . Makefiles • Uses an initialization list to create a “buffer” Overload Operators of length 1 characters: buf(new char[1]) ◭◭ ◮◮ • Places the null termination character into ◭ ◮ the newly created buffer. Slide 9 of 38 • cppreference : Constructs an empty string, Go Back with a length of zero characters. Full Screen Quit
3.2. Prefer initialization to assignment Overview What is a class ? • Initialization is more efficient for data mem- Constructors & . . . bers that are objects (demo later) What if I don’t write . . . • Only way to pass parameters to base class: Why Prefer Init? Static Class Variabless class Person { Principle of Least . . . public: Person(int a) : age(a) {} Interface vs . . . private: Makefiles int age; Overload Operators }; class Student : public Person { ◭◭ ◮◮ public: ◭ ◮ Student(int age, float g) : Person(age), gpa(g) {} private: Slide 10 of 38 float gpa; Go Back }; Full Screen Quit
3.3. Init performed in order of declare Overview What is a class ? • In Student , the constructor will initialize iq Constructors & . . . first, then age , because iq appears first in What if I don’t write . . . declaration (line 5). Why Prefer Init? • Initialization list not needed for built-in types. Static Class Variabless Principle of Least . . . Interface vs . . . 1 c l a s s Student { 2 public : Makefiles 3 Student ( int a ) : age ( a ) , iq ( age+100) {} Overload Operators 4 private : ◭◭ ◮◮ 5 int iq ; 6 int age ; ◭ ◮ 7 } ; Slide 11 of 38 Go Back Full Screen Quit
3.4. Conversion Constructor Overview What is a class ? 1 c l a s s s t r i n g { Constructors & . . . 2 public : What if I don’t write . . . 3 s t r i n g ( const char ∗ b) : 4 buf (new char [ s t r l e n (b)+1]) { Why Prefer Init? 5 strcpy ( buf , b ) ; Static Class Variabless 6 } Principle of Least . . . 7 private : Interface vs . . . 8 char ∗ buf ; 9 } ; Makefiles • Converts b , on line 3, into a string Overload Operators ◭◭ ◮◮ • strlen returns the size of the c-string, not ◭ ◮ including the null termination Slide 12 of 38 • On line 4 we allocate strlen(b)+1 bytes, Go Back where +1 allows for the null termination Full Screen Quit
3.5. Copy Constructor Overview What is a class ? 1 c l a s s s t r i n g { Constructors & . . . 2 public : What if I don’t write . . . 3 s t r i n g ( const s t r i n g& s ) : Why Prefer Init? 4 buf (new char [ s t r l e n ( s . buf )+1]) { 5 strcpy ( buf , s . buf ) ; Static Class Variabless 6 } Principle of Least . . . 7 private : Interface vs . . . 8 char ∗ buf ; 9 } ; Makefiles Overload Operators • Copy constructor uses the parameter s , line ◭◭ ◮◮ 3, to make a deep copy. ◭ ◮ • Notice the parameter transmission mode: Slide 13 of 38 const& Go Back Full Screen Quit
3.6. Destructor Overview What is a class ? 1 c l a s s s t r i n g { Constructors & . . . 2 public : What if I don’t write . . . 3 ˜ s t r i n g () { del ete [ ] buf ; } Why Prefer Init? 4 private : 5 char ∗ buf ; Static Class Variabless 6 } ; Principle of Least . . . • We used new char[] in the constructors to Interface vs . . . allocate an array Makefiles Overload Operators • We use delete [] on line 3 to indicate that ◭◭ ◮◮ we are deallocating an array. ◭ ◮ Slide 14 of 38 Go Back Full Screen Quit
4. What if I don’t write one Overview What is a class ? I write this : Constructors & . . . What if I don’t write . . . class Empty{}; Why Prefer Init? Compiler writes this : Static Class Variabless Principle of Least . . . Interface vs . . . class Empty { Makefiles public: Overload Operators Empty(); Empty(const Empty &); ◭◭ ◮◮ ~Empty(); ◭ ◮ Empty& operator=(const Empty &); Slide 15 of 38 }; Go Back Full Screen Quit
4.1. Here’s what they look like: Overview What is a class ? inline Empty::Empty() {} Constructors & . . . inline Empty::~Empty() {} What if I don’t write . . . Why Prefer Init? inline Empty * Empty::operator&() {return this;} Static Class Variabless Principle of Least . . . inline const Empty * Empty::operator&() const { Interface vs . . . return this; } Makefiles Overload Operators The copy constructor & assignment operator simply ◭◭ ◮◮ do a member wise copy, i.e., shallow. Note that ◭ ◮ the default copy/assign may induce leak/dble free Slide 16 of 38 Go Back Full Screen Quit
Recommend
More recommend