CIS 29 - Notes for Thursday, 1/7

Announcements and Reminders

  • Attendance using Zoom waiting room
  • Office Hours:  Tuesday and Thursday, 12:45 - 1:45 pm
  • Last night's online session notes and recording posted
  • Assignment 1 is due Tuesday
  • Another option for Group Project - educational game

Recording

Review - Maybe You Haven’t Covered This

Conversion Operator

A conversion operator is a member function that is used to convert a class type to another type

Example 1-5 - Conversion of a user-defined type to a primitive 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 as 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 loop

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 constructor.

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 a 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

The generic size function

The generic size function was introduced in C++17.  It returns the size of an array (number of elements) or the size of a C++ container;

Example 6 - The size function

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?