classes in C++ Flashcards
using classes in C++
• inside class, members referred to by name
• outside class, full name of member used
(i.e. class-name::Member name)
what are overloaded functions?
functions with different implementations based on the number and/or types of arguments
dynamic memory allocation in C++
- new T create a new instance of type T and returns a pointer to it (new also calls any constructor)
- delete x calls any destructor then deallocates memory
pointers
- if x is an object, x.y refers to member y of x
- if x is a pointer, x->y refers to member y of object pointer to by x
- “this” is a pointer to the object for which the function was called (only legal in member function)
what is inheritance?
the reuse of code and variables in a derived class
C++ supports inheritance and multiple inheritance
C++ supports three access modifiers
1) public - anyone can access
2) private - only this class can access
3) protected - this class and its subclasses can access
what is polymorphism?
- ability to use a subclass where a superclass is expected
- functions of the subclass are invoked (rather than those of superclass)
- C++ supports it (only functions marked as virtual are polymorphic)
- a run-time thing
what are templates?
- another way to achieve reuse is through genericity
- achieved through parameterized types
- happens at compile time
- each use of a template with a different type parameter creates a new type at run-time
pros and cons of templates
- no runtime expense
- catch more errors at compile time (good)
- programs suffer from code bloat
two ways to give re-use
polymorphism: errors aren’t caught until run-time, slower code
templates: catch more errors at compile-time, suffers from code bloat