Classes
Classes
Overview
Header Files
Accessing Class Members
Class Scope
Constructors/Destructors
Member initializer list
this Pointer
Friend
Static
new/delete operators
Header Files
Preprocessor Guards
Prevent header files from being
included more than once
#ifndef FILENAME_H
#define FILENAME_H
. . .
#endif
#include <string>
using std::string;
class Person {
public:
private:
string _name;
};
#endif
Person.cpp
#include "Person.h"
string Person::getName()
{
return _name;
}
main.cpp
#include <iostream>
using namespace std;
#include "Person.h"
int main()
{
Person p;
Person &pRef = p;
Person *pPtr = &p;
return 0;
}
Accessing a Class Member
Dot (.) operator with an object:
objectName.memberFunction();
ClassName(optional_arg_list);
Destructor
Called when an object is destroyed
Only one per class
~ClassName();
};
const member non-const
function member function
const can invoke error
object member function
non-const can invoke can invoke
object member function member function
Person Example
Which Person member functions should
be const?
getName should be const
class Person {
...
string getName() const;
Person.cpp:
Person Example
Person object has a string object
Member Initializer List
Specifies initial values or constructor
arguments for data members of the class
Specified in the constructor function:
ClassName::ClassName(argList)
: dataMember1(initialValue),
dataMember2(arg1, arg2)
{ ... }
}
Order of Construction
Object data members are constructed in
the order they are declared in the class
definition
Ordering in initializer list does not
impact order of construction
Arguments to object data member’s
constructor is specified in member
initializer list of the enclosing object
object.func(a1, a2);
obj.func1().func2().func3();
Person.cpp:
In function main:
Create a Person object mrBox with
name “Mr Box”
Create a Store object petStore with
owner mrBox
Store.h
#ifndef STORE_H
#define STORE_H
#include "Person.h"
class Store {
public:
Store(const Person owner);
private:
Person _owner;
};
#endif
Store.cpp
#include "Store.h"
#include "Person.h"
#include "Store.h"
int main()
{
...
...
return 0;
}
Friends
Grants access to private members to
global functions or other classes
class ClassA {
// friend class
friend class ClassB;
};
class Person {
friend class Store;
...
};
Class Example
setStoreOwner global function accesses
private data member _owner of class Store:
class Store {
friend void setStoreOwner(Store &s,
const Person &o);
...
};
static Class Members
Exist independently of any object of the
class:
class ClassName {
};
class ClassName
{
static type staticData;
};
type ClassName::staticData;
Store Example
Add a _taxRate static data attribute of
type double to class Store.
Store.h
class Store {
...
private:
Person _owner;
static double _taxRate;
};
Store.cpp
#include "Store.h"
double Store::_taxRate;
Static Member Function
Declare static inside the class definition:
class ClassName
{
static returnType func();
};
Store.h
class Store {
public:
...
static void setTaxRate(const double rate);
Store.cpp
object.staticFunction();
ClassName::staticFunction();
In function main:
petStore.setTaxRate(.10);
Store::setTaxRate(.08);
new and delete Operators
Dynamic memory management operators
Allow programmer to specify when to
create (allocate) and destroy (free)
objects
Storage class rules do not apply to
objects created with operator new
delete sPtr;
Dynamically Managing Arrays
new and delete can dynamically allocate
and free arrays
Specify size of arrays at runtime
instead of at compile time
Dynamically allocate an array:
delete [] ptr;
delete [] pPtr;