CIS 250 - 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
- default constructor
- destructor
- copy assignment operator
- copy constructor
- 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