INTRO TO COMPUTER SCIENCE II
CONST, STATIC, DESTRUCTORS
CS162
Last time…
Constructors
Last time…Constructors
3 Types: Implicit, Default, Accepts parameters
Special class member function that is called automatically when the
object is created
Must have same name as class
Not allowed to return anything
Should always write a default constructor
Should only be used for initialization
Also, best practice is to initialize all member variables
Passing Objects
Can be passed the same way as any other variable Value void print_info(Point p1){}
Pointer
Reference
void print_info(Point* p1){} void print_info(Point& p1){}
Traditionally pass by reference or address
Generally more efficient
Pass by value makes 2 copies -> requires the copy constructor at least once Can be used when using the value for internal computation, but not changing it for the
main program
Pass by reference only uses the one variable (no copies) and can’t be NULL Can be problematic since changes to references/pointer persist
Special Pointers – NULL
NULL Pointer is a constant with a value of zero
Special macro inherited from C
Good practice to assign the pointer NULL to a pointer variable in case you don’t have the exact address to be assigned
Avoid accidental misuse of an unitialized pointer Garbage values can make debug difficult
In C++11, use nullptr instead of NULL
Special Pointers – this
Implicit *this
What the compiler uses when calling class functions associated with objects
//what you see
Point p1;
p1.set_location(4, 8);
//what the compiler sees
Point p1;
set_location(&p1, 4, 8);
//what you see
void set_location(int new_x, int new_y){
x = new_x;
y = new_y;
}
//what the compiler sees
void set_location(Point* const this, int new_x, int new_y){
this->x = new_x;
this->y = new_y;
}
Special Pointers – this
You can explicitly use *this as well Allows you to chain functions together
//in point.cpp
Point& Point::move_left(int delta){
x -= delta; } return*this;
Point& Point::move_up(int delta){
y += delta;
} return*this;
//in prog.cpp
int main(){
Point p1(4,8);
p1.move_left(3).move_up(7);
p1.print_point();
return 0; }
Const Objects
Create an object that cannot be changed
Instantiated class objects can be made constant
const Point p1(50,50);
const Point p2; //calls default constructor
Can’t modify any member variables after constructed Either directly (if public) or via setter member function
Const References & Functions
To prevent changes to an object being passed, put const in the parameter listing bool isGreater(const Point&a, const Point&b);
If a function isn’t supposed to change anything, put a const at the end void print() const;
void Point::print() const {/*definition code goes here*/} Will cause an error if the code in print() changes anything
If you use const for one parameter of a particular type, then you should use it for every other parameter of that type that is not changed by the function call
Const can’t be a member variable of a class
Static Variables
A variable shared by all objects of the same class EX: static int count;
Allowed to be private
Permits objects of the same class to communicate
Must be initialized outside of class definition EX: int Point::count = 0;
The person writing the class does this
Static variables can’t be initialized twice
Static Functions
Can have static functions
They cannot use non-static things They are not attached to an object
AKA, no this* pointer Example
static int getCount();
int Point::getCount() { //note: no static keyword
count++; return count;
}
Function call: Point::getCount();
Destructors
Special class member function that is called automatically when the object is destroyed
“Opposite” of the constructor
Syntax
Must have same name as class
Must have “~” at the beginning
Can’t take arguments, can’t return anything
class Point { private:
int x;
int y; public:
Point(); Point(int,int); ~Point();
void move_left(int);
};
Point::~Point(){
} cout << "Destructor called!" << endl;
int main() { Point p1;
Point *p2 = new Point(4,10); //print P1, P2
delete p2;
return 0;
}
Destructors
Called when object goes out of scope When the function ends
When the program ends
A block containing local variables ends A delete operator is called
Automatically created if one is not supplied
But won’t handle dynamic memory!
Generally used to clean up dynamic memory usage, file I/O handles, database connections, etc.
}
class Point { private:
int x;
int y; public:
Point(); Point(int,int); ~Point();
}; void move_left(int); Point::~Point(){
cout << "Destructor called!" << endl;
}
int main() { Point p1;
Point *p2 = new Point(4,10); //print P1, P2
delete p2;
return 0;