the c class
play

The C++ Class What is a class ? Constructors & . . . January 4, - PowerPoint PPT Presentation

The C++ Class What is a class ? Constructors & . . . January 4, 2016 Overload Operators Interface vs . . . Brian A. Malloy Naming Convention Makefiles Problems Template Classes Slide 1 of 35 Go Back Full


  1. The C++ Class What is a class ? Constructors & . . . January 4, 2016 Overload Operators Interface vs . . . Brian A. Malloy Naming Convention Makefiles Problems Template Classes ◭◭ ◮◮ ◭ ◮ Slide 1 of 35 Go Back Full Screen Quit

  2. 1. What is a class ? What is a class ? Constructors & . . . • Unit of encapsulation: Overload Operators – Public operations Interface vs . . . – Private implementation Naming Convention Makefiles • Abstraction: Problems – string: abstracts char* of C Template Classes – student ◭◭ ◮◮ – sprite ◭ ◮ • C++ Classes: easy to write, difficult to get Slide 2 of 35 right! Go Back • Lots of examples Full Screen Quit

  3. 1.1. The actions of a class What is a class ? • Initialize it’s data attributes Constructors & . . . Overload Operators • Allocate memory when needed Interface vs . . . Naming Convention • De-allocate memory when necessary Makefiles Problems Template Classes ◭◭ ◮◮ ◭ ◮ Slide 3 of 35 Go Back Full Screen Quit

  4. 1.2. C++ class vs C++ struct What is a class ? • Default access is only difference Constructors & . . . Overload Operators Interface vs . . . Naming Convention Bad class Good Class Makefiles class Student { class Student { Problems public: string name; Template Classes string name; float gpa; float gpa; }; ◭◭ ◮◮ }; ◭ ◮ Slide 4 of 35 Go Back Full Screen Quit

  5. 1.3. Object : an instantiated class What is a class ? • C++ objects can be stored on the stack: Constructors & . . . Overload Operators class A{}; Interface vs . . . int main() { Naming Convention A a, b; Makefiles }; Problems • Or on the heap: Template Classes ◭◭ ◮◮ int main() { A *a = new A; ◭ ◮ A *b = new B; Slide 5 of 35 }; Go Back • Compiler does stack; programmer does heap! Full Screen Quit

  6. 2. Constructors & Destructor What is a class ? • Constructors: Constructors & . . . Overload Operators – init data & allocate memory Interface vs . . . – Init data through initialization lists Naming Convention • Destructors deallocate memory Makefiles • The three types of constructors are: Problems 1. Default Template Classes 2. Conversion ◭◭ ◮◮ 3. Copy ◭ ◮ class Student { public: Slide 6 of 35 Student(); Student(char * n); Go Back Student(const Student&); ~Student(); Full Screen }; Quit

  7. 2.1. Prefer initialization to assignment What is a class ? • Initialization is more efficient for data mem- Constructors & . . . bers that are objects Overload Operators Interface vs . . . • Only way to pass parameters to base class Naming Convention class Person { Makefiles public: Problems Person(int a) : age(a) {} Template Classes private: int age; ◭◭ ◮◮ }; class Student : public Person { ◭ ◮ public: Student(int age, float g) : Person(age), gpa(g) {} Slide 7 of 35 private: Go Back float gpa; }; Full Screen Quit

  8. 2.2. Init performed in order of declare What is a class ? Constructors & . . . class Student { Overload Operators public: Interface vs . . . Student(int a) : age(a), iq(age+100) {} Naming Convention private: Makefiles int iq; int age; Problems }; Template Classes ◭◭ ◮◮ ◭ ◮ Slide 8 of 35 Go Back Full Screen Quit

  9. 2.3. Principle of Least Privilige What is a class ? • Make “everything” const ! Constructors & . . . Overload Operators • Can reduce debugging Interface vs . . . Naming Convention • Provides documentation Makefiles • Can prevent a member function from mod- Problems ifying data attributes Template Classes ◭◭ ◮◮ • Allow a function enough data access to ac- complish its task and no more! ◭ ◮ • Most beginners take them all out . . . probably Slide 9 of 35 need more! Go Back Full Screen Quit

  10. 2.4. Least Privilege example What is a class ? class string { Constructors & . . . public: Overload Operators string(const char* n) : buf(new char[strlen(n)+1]) { Interface vs . . . strcpy(buf, n); Naming Convention } Makefiles const char* get() const { return buf; } Problems private: Template Classes char *buf; }; ◭◭ ◮◮ std::ostream& operator<<(std::ostream& out, const string& s) { ◭ ◮ return out << s.get(); } Slide 10 of 35 int main() { string x("Hello"); Go Back std::cout << x.get() << std::endl; } Full Screen Quit

  11. 2.5. What operations does a class need? What is a class ? 1. All classes should have default constructor Constructors & . . . 2. Heap based data: canonical form : Overload Operators Interface vs . . . (a) Copy constructor Naming Convention (b) Destructor (c) Overloaded assignment Makefiles Problems Template Classes class string { public: ◭◭ ◮◮ string(); string(const string&); ◭ ◮ ~string(); string operator=(const string&); Slide 11 of 35 private: char *buf; Go Back }; ostream& operator<<(ostream&, const string&); Full Screen Quit

  12. 2.6. Why canonical form? What is a class ? Constructors & . . . Overload Operators Interface vs . . . Naming Convention Makefiles Problems Template Classes ◭◭ ◮◮ ◭ ◮ Slide 12 of 35 Go Back Full Screen Quit

  13. 2.7. Why canonical form? What is a class ? Constructors & . . . Overload Operators Interface vs . . . Naming Convention Makefiles Problems Template Classes ◭◭ ◮◮ ◭ ◮ Slide 13 of 35 Go Back Full Screen Quit

  14. 2.8. What can go wrong? What is a class ? 1 #include <iostream> Constructors & . . . 2 #include <cstring> Overload Operators 3 using std::cout; using std::endl; Interface vs . . . 4 Naming Convention 5 class string { Makefiles 6 public: Problems 7 string() : buf(new char[1]) { buf[0] = NULL; } 8 string(const char * s) : buf(new char[strlen(s)+1]) { Template Classes 9 strcpy(buf, s); ◭◭ ◮◮ 10 } 11 ~string() { delete [] buf; } ◭ ◮ 12 const char* getBuf() const { return buf; } 13 private: Slide 14 of 35 14 char * buf; 15 }; Go Back Looks like a well written class, but it is an accident waiting to happen! Full Screen Quit

  15. 2.9. Unseen Functions What is a class ? Write this : Constructors & . . . Overload Operators class Empty{}; Interface vs . . . Naming Convention Get this : Makefiles Problems class Empty { Template Classes public: Empty(); ◭◭ ◮◮ Empty(const Empty &); ~Empty(); ◭ ◮ Empty& operator=(const Empty &); Slide 15 of 35 Empty * operator&(); const Empty * operator&() const; Go Back }; Full Screen Quit

  16. 2.10. Here’s what they look like: What is a class ? Constructors & . . . inline Empty::Empty() {} Overload Operators inline Empty::~Empty() {} Interface vs . . . Naming Convention inline Empty * Empty::operator&() {return this;} Makefiles Problems inline const Empty * Empty::operator&() const { Template Classes return this; } ◭◭ ◮◮ ◭ ◮ The copy constructor & assignment operator simply do a member wise copy, i.e., shallow. Note that Slide 16 of 35 the default assignment may induce a memory leak. Go Back Full Screen Quit

  17. 2.11. What’s wrong with this class? What is a class ? Constructors & . . . class Student { Overload Operators public: Interface vs . . . Student(const char * n) : name(n) { } Naming Convention const getName() const { return name; } Makefiles void setName(char *n) { name = n; } Problems private: Template Classes char *name; ◭◭ ◮◮ }; ◭ ◮ Slide 17 of 35 Go Back Full Screen Quit

  18. 2.12. Practice: What’s the output? What is a class ? Constructors & . . . class String { Overload Operators public: Interface vs . . . String() { cout << "default" << endl; } Naming Convention String(char * n) { cout << "convert" << endl; } Makefiles String(const String&) { cout << "copy" << endl; } Problems ~String() { cout << "destructor" << endl; } Template Classes private: char * buf; ◭◭ ◮◮ }; ◭ ◮ int main() { String a("cat"), b = a; Slide 18 of 35 String * ptr = new String("dog"); return 0; Go Back } Full Screen Quit

  19. 2.13. Practice: write class Student What is a class ? void fun(Student stu) { Constructors & . . . std::cout << stu.getName() << std::endl; Overload Operators } Interface vs . . . Naming Convention int main() { Makefiles Student a, b(Darth Maul, 3.5), c = b; Problems Student * d = new Student(Anakin, 4.0); Template Classes cout << *d << endl; fun(a); ◭◭ ◮◮ return 0; } ◭ ◮ Slide 19 of 35 Go Back Full Screen Quit

  20. 3. Overload Operators What is a class ? class string { Constructors & . . . public: Overload Operators string(); Interface vs . . . string(const char*); Naming Convention string(const string&); Makefiles ~string(); Problems string operator+(const string&); Template Classes string& operator=(const string&); char& operator[](int index); ◭◭ ◮◮ const char& operator[] const (int index); private: ◭ ◮ char *buf; }; Slide 20 of 35 ostream& operator<<(ostream&, const string&); string operator+(const char*, const string&); Go Back Full Screen Quit

Recommend


More recommend