CIS 29 - Notes for Thursday, 1/10

Announcements and Reminders

  • Check attendance sheet
  • Office Hours:  Tuesday and Thursday, 11:30 - 12:00 noon in the ATC main computer lab
  • Review Session today 11:30-12:30 in the ATC main computer lab
  • Assignment 1 is due Tuesday

Review

Maybe You Haven’t Covered This

Example 1-5 - Conversion of a user-defined type to a primitive type

Example 1-6 - More Conversions of a user-defined type

Explicit Constructors

The keyword explicit is used to specify that a constructor may only be used for object instantiation and not for automatic conversion.  Here’s an example that demonstrates the effect.

Example 1-7 – Explicit constructors


Some C++ 11 Features

auto type

Using the auto keyword, a variable’s type may be automatic assigned.  The new usage of the auto keyword negates the former ansi-C storage class meaning.

the decltype operator

The decltype operator is similar to auto, it returns the type of an expression.

Example 2-1 – auto type and decltype

the constexpr specifier

The constexpr specifier declares that a function or variable is const at compile time. 

constexpr float pi = 3.14; 

constexpr float areaOfCircle(float radius)
{
    return pi * radius * radius;
}
 
constexpr float area1 = areaOfCircle(1);
 
const float two = 2.f;
constexpr float area2 = areaOfCircle(two);
 
float three = 3.f;
constexpr float area32 = areaOfCircle(three);  // ERROR

nullptr

nullptr is a pointer constant with conversions to any pointer type.  It is used as a replacement for the macro, NULL or a 0 pointer.
 
char*ptr = nullptr;
 
void somefunk(type* ptr = nullptr);
 
if (p == nullptr) …

Uniform initialization/Brace/List initialization

int I{7};  // instead of int I = 7;
 
int zero{};  // same same int zero = 0;
 
string s{“apple pie”};
 
SomeClass object{19};    // instead of SomeClass object(19);

AnotherClass obj{thing,23,2.5,’a’};   // instead of  AnotherClass obj(thing,23,2.5,’a’);  

Range-based for loop4

Example 2 – Range-based for loop

Default and delete constructors

The default specifier with the default constructor causes the compiler to generate it.  The delete specifier is used to disable a contructor.

class ABC
{
   int a,b,c;
Public:
   ABC() = default;           // same as ABC(){}
   ABC(int, int, int);
   ABC(const ABC&) = delete;  // disable copy constructor
   …
};

The override specifier

The keyword override specifier is a way to ensure that a virtual function in a derived class overrides the analogous function in the base class.  It can be used to tell the reader that the function overrides the equivalent function in the base class.  Keep in mind that the two functions might be located in different files.

class Base
{

public:
   virtual void funk1(int);
   virtual void funk2(float);
   virtual void funk3(string);

};

class Derived : public Base
{

public:
   virtual void funk1(int);    // overrides funk1 in Base class
                         // funk2 is not overridden
   virtual void funk3(string) override; // funk3 is overridden
   virtual void funk4(char) override;    // ERROR   

};

Rvalue references

Rvalue references permits a reference to bind to an rvalue – a temporary or a literal.  This is useful for the move constructor or the move assignment operator, avoiding the expense of copying an object for this purpose.

Example 3 – Rvalue References

Move Semantics
With the use of rvalue references in C++11, the move constructor and the move assignment operator was added as a replacement for the copy constructor and the overloaded assignment operator.

Example 4 – Move Semantics


Overloading the new and delete operators

The new and delete operators may be overloaded globally or in a class

The new and delete operators are overloaded

Assignment 1 Hints

Note: The string class is not allowed in this assignment.  Use only C-strings for text data.

How is the Dictionary stored?  What does resize mean?