Class Basics
• Classes have two primary characteristics:
– they hold data – data members
– they have functions for manipulating that data – member functions or methods
• Classes have two major types of data members and methods:
– public - non class members (e.g., main program, members of other classes, etc.) can access directly
– private - non class members cannot access directly, only via public methods
– (another type, protected, is not discussed here)
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 1
Simple Class Example – A Date Class
//file date1.h - class Date holds month, //day and year as ints class Date
{ public:
Date();
//creates default date, month,day,year=0,0,0 Date(int m, int d, int y);
//creates date with month, day, year = m,d,y void set(int m, int d, int y);
//sets month,day, year = m,d,y void get(int& m, int& d, int& y);
// returns month, day,year values by reference void output();
// outputs date to default output private:
int month, // date’s month day, // date’s day year;}; //date’s year
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 2
Basic Philosophy
• Hide data from users
• Hide implementation from users
• Provide user access via public methods – controls data manipulation
– can change implementation without affecting users
Public Access Example – Simple Date Class
void Date::set(int m, int d, inty y) {
month = m;
day = d;
year = y;
}
void Date::get(int& m, int& d, int& y) {
m = month;
d = day;
y = year;
}
Constructors
• A class member function such that:
– automatically called when object declared – if constructor has arguments, must be
given
– constructor must have same name as class – constructors cannot return values
• Must have a default - no arguments (system or user supplied) but may have many (with different signatures)
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 5
Example of Multiple Constructors – The Date Class
Date::Date() {
month = day = year = 0;
}
Date::Date(int m, int d, int y) {
month = m;
day = d;
year = y;
}
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 6
Some Important public Member Functions
• Default Constructor
– use to provide appropriate default values for data members
– not good idea to use “default” default constructor
• Functions to Provide Access to set/Retrieve Private Data Members as Needed
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 7
const Member Functions
• If function does not modify data, declare const.
• E.G., For Date retrieval, Define:
void get(int& m, int& d, int& y) const;
• This results in:
– a compile error if get tries to modify data – allows this function to be called on an
object passed as a const reference
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 8
Const member functions (cont)
• Functions can return any meaningful type, including a class
• e.g.,
Date min_date(const Date& D1, const Date& D2){
//creates a Date that is the minimum //-- earlier -- of two
//dates D1,D2 --
//must use dates via const functions
• Note: Also uses copy constructor – more later
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 9
Copy Constructors
To make copies of objects – for instance, for value parameters and when initializing a new object with an existing object.
PDQ(const PDQ & newpdq);
Called when an object of type PDQ is passed as a value parameter, when the return value of a function is an object of type PDQ, and whenever an object is initialized in a declaration. Important not to use the default if class has dynamically allocated memory.
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 10
Destructors
To deallocate dynamically allocated memory within an object.
~PDQ();
The destructor is called whenever an object goes out of scope, or an object is destroyed by delete.
It is important not to use the default destructor if an object contains a pointer to dynamically allocated memory.
Assignment Operator
To assign one object to another.
const PDQ & operator=(const PDQ & rightSide);
{
.... copy rightSide return *this;
}
A default definition of = is always included. But does not work properly if class uses dynamically allocated memory. So, then it is necessary to define an assignment operator for the class.
Friend Functions
A friend function of a class is defined outside that class’s scope, yet has the right to access non-public members of the class.
Standalone functions or entire classes may be declared to be friends of another class.
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 13
Friend Functions (cont)
To give function setX access to private methods, members of classOne, put the following in the class declaration of classOne:
friend void setx(int);
setX is not a member of any class-standalone function.
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 14
Friend Functions (cont)
to declare all member functions of classTwo as friends of classOne, put the following in the definition of classOne:
friend classTwo;
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 15
Examples of New Usage
• Dynamically Create an Array of 5 Ints int *A_ptr;
A_ptr = new int[5];
• Dynamically Create a Single Double double *p;
p = new double;
• Dynamically Create an Array of 50 Dates Date *D_ptr;
D_ptr = new Date[50];
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 16
Arrow Operator
Date* DPtr;
DPTR = new Date;
(*DPtr).day = 3; // Ignoring Private Protection
DPtr->day = 3;
DPtr->get(m,d,y);
Program Devel/Intro Alg Lang CS501b/275 Spring 2005 17