CIS 29 - Notes for Thursday, 1/11

Announcements and Reminders


Classes, Constructors, and  Desctructors

Study these examples

Pay attention to

static data members, constructor initializations list, copy constructor, overloaded assignment operator, overloaded insertion operators

Example 1-4 – Adding Matrices

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

typedef and using

The keyword, typedef, originally from C, is used to define a type.

C++ 11 introduced the keyword, using to act like typedef.

typeid operator

The typeid operator returns an identifier of a type, a variable or an expression.  The return of the typeid is a class type, called type_info.  You can use the name() member function of the type_info class to display a literal description of the type.

Example 1-8 – typedef, using, typeid

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

Example 2 – Range-based for loop

Defaulted and deleted 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;
   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.

class Base

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


class Derived : public Base

   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