Introduction to classes and structs

Objectives

  • Use classes and structs
  • Describe the differences between structs and classes

structs and classes

These topics are covered well in the text and structs should be familiar to you from previous C/C++ programming. If you are familiar with classes, but not structs, then all you have to know is that classes and structs are the same thing, except that the members of a class are private by default, while the members of a struct are public by default. Structs are generally used only when only data is contained. The topics listed below are just a brief outline of what will be discussed in class.

  • Assume that statements about structs below also apply to classes
  • Declaring a struct: struct structName { members };
  • Structs usually just have explicit data members, but may also have function members
  • Declaring a variable using a struct type: structName structVar;
  • Accessing members of a struct: structVar.memberName
  • Struct variables can be passed to functions either by value or by reference
  • See demostruct1.cpp example
  • You can create an array of structs
  • Structs can be nested
  • See demostruct2.cpp example
  • Classes are just like structs except for the default access permission
  • Functions can also be added as members of a struct or class
  • public access: all other code can see
  • private access: only seen within the struct itself
  • protected access: seen within the struct itself and derived classes (subclasses)
  • See democlass1.cpp example
  • Good practice to separate specification (header file) from implementation
  • Convention is to only use struct when treating as C-style records and not including functions as members
  • Built-in operators: member access, assignment (shallow copy only)
  • Automatically provided functions (if compiler does not support move semantics)
    • Default constructor (if no other constructor is explicitly declared)
    • Copy constructor
    • Copy assignment operator
    • Destructor
  • Automatically provided functions (if compiler supports move semantics) [Note: We do not cover move semantics in the CIS 250 course.]
    • Default constructor (if no other constructor is explicitly declared)
    • Copy constructor (if no move constructor and move assignment operator are explicitly declared)
    • Move constructor (if no copy constructor, move assignment operator, copy assignment operator, and destructor are explicitly declared)
    • Copy assignment operator (if no move constructor and move assignment operator are explicitly declared)
    • Move assignment operator (if no copy constructor, copy assignment operator, move constructor, and destructor are explicitly declared)
    • Destructor
  • Assignment works with objects that do not dynamically allocate memory, but most other operators do not unless code is specifically written to implement/overload them
  • A const function has the keyword const at the end of its signature; that specifies that the function may not modify the values of its data members
  • Accessor methods allow public access to private data member values
  • Mutator methods allow public access to modify private data member values
  • Predicate methods return true or false
  • Constructors
    • A default constructor is automatically provided
    • A default constructor has no parameters
    • If you define any constructor, then the default constructor is not automatically provided
    • Constructors are often overloaded
    • A copy constructor is automatically provided, but it only does a shallow copy which is not suitable for objects that dynamically allocate memory
    • Constructors have the same name as the class
    • Constructors have no return data type
    • Constructors are automatically invoked when objects are created, but can not be explicitly called
    • Constructors are usually used to initialize "instance" data members
    • Invoking a default constructor: className varName;
    • Invoking a non-default constructor: className varName(param1, param2);
    • Creating an array of objects: className arrayName[sizeOfArray];
    • Constructors may have default parameter values
  • Destructor
    • Has same name as class, but with a leading tilde (~)
    • There is only one destructor function per class
    • A destructor has no return data type
    • A destructor has no parameters
    • Usually used to deallocate resources such as memory that the object has allocated dynamically
    • Destructors are automatically invoked when objects go out of scope
  • All members of a class are either static or instance
  • Static members are specified using the keyword static
  • Static functions do not have access to instance data members and may be called directly using the class name even before any objects of the class have been created
  • There are individual copies of each instance data member for each object created
  • No instance data members are created before an object has been created from a class
  • Static data members are associated with the class rather than instances (objects) of the class; for that reason, there is only a single copy of each static data member which is shared by the class and all objects created from the class
  • Static data members exist before any objects have been created from a class