Computer Science II -- Week 3
Introduction
Note that the links from this page are to handouts that will be distributed the night of class. Only print out those handouts if you could not attend class.
Main topics this week:
Objects have structure, for example a House:
# of windows
# of doors
# of bathrooms
Objects have state, for example a specific House:
15 windows
3 doors
2 bathrooms
Objects have an interface, for example with a House:
turnOnFurnace
turnOffFurnace
Encapsulation
Classes are how C++ supports objects.
public and private access:
class Foo
{
public:
int x;private:
int y;
};void main ()
{
Foo temp;temp.x = 5;
temp.y = 6;
}
Encapsulation: hiding the implementation. Why is this a good thing?
class Foo
{
public:
int getX ();
void setX (int);private:
int x;
};
Default arguments
class Foo
{
public:
int getX ();
void setX (int = 5);private:
int x;
};
const member functions
class Foo
{
public:
int getX () const;
void setX (int = 5);private:
int x;
};
constructors:
class Foo
{
public:
Foo ();
};void main ()
{
Foo x;
}
What is the purpose of a constructor?
What happens if the constructor is private?
Multiple constructors: default, copy, custom
class Foo
{
public:
Foo ();
Foo (const Foo &);
Foo (int);
};void main ()
{
Foo x;
Foo y = 5;
Foo z = x;
}
If you don't provide a copy constructor, the compiler provides one for you. When is the compiler provided copy constructor okay? When should you write your own?
destructors:
class Foo
{
public:
Foo ();
~Foo ();
};
When is a destructor called?
#1
void function1 ()
{
Foo x;}
#2
void function2 ()
{
Foo *ptr = new Foo ();delete ptr;
}#3
void function3 ()
{
Foo *ptr = new Foo ();
}
What is wrong with example #3?
Purpose of a destructor? When is a destructor necessary?
Handout 1 can be found here. What is wrong with the Foo class presented in the handout?
What happens if you do not provide a default constructor or a copy constructor? Example in memory of class in handout of not providing a copy constructor.
The .h file for handout 2 can be found here, and the .cpp file can be found here.
In handout 2, note:
- Use of .h and .cpp files. What goes into the .h file?
- Different types of constructors. Which is which?
- Destructor. Why is the destructor necessary?
- const member functions
- Use of classname:: in the .cpp file
Initialization of data members. This uses handout 3, which can be found here. What happens in what order when a TwoFoos class is created?
Initializer list. Tells the compiler what constructors to call for data members:
TwoFoos (int firstInt, int secondInt)
: first (firstInt), second (secondInt)
{
}
Why use an initializer list?
In what order does initialization happen?
Static data members:
Test.h
class Test
{
public:
static int x;
};Test.cpp
// Must provide storage location for the static data member
int Test::x = 0;
Show memory picture of static data member.
void main ()
{
Test j;
Test k;j.x = 5;
cout << k.x << endl;
}
Static methods:
class Test
{
public:
static int getX ();
static void setX (int);int getY ();
void setY (int);private:
static int x;
int y;
};
Here's a handout (.cpp and .h)giving an example of using static data members and methods. We will discuss this handout in class.
In the handout, note the private constructors. What does this mean?
Note the static method and data member. In the .cpp, at line 22, note the declaration of the static data member.