Chapter 4. Class and Object
Chapter 4. Class and Object
September 2015
Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I
Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I
Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I
Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I
Structure
Example
class
{
data members
function members
};
class Student
{
int id_num;
string last_name;
double grade_point_average;
};
Example:
David.id_num = 7645;
cout << David.id_num;
Example:
David.id_num = 7645;
cout << David.id_num;
Access modifier
By default, all members of a class are private, meaning they
cannot be accessed using any statements in any functions that are
not also part of the class.
Declare class data members to be public instead of private.
class Student
{
public:
int id_num;
string last_name;
double grade_point_average;
};
Using the keyword public means that the fields are now accessible
when they are used with a Student object in a main() function.
What is encapsulating
What is encapsulating
What is encapsulating
class Student
{
private:
int id_num;
string last_name;
double grade_point_average;
public:
void display_student_data();
};
Example
// implementation section:
void Student::displayStudentData()
{
cout << "Student #" << idNum << "s last name is " <<
lastName << endl;
cout << "The grade point average for this student is " <<
gradePointAverage << endl;
}
Why is static?
When objects are instantiated, each one gets its own block of
memory for its data members. For example, if you create an array
of 100 objects, 100 blocks of memory are set aside
Sometimes every instantiation of a class requires the same value.
For example
100 Student objects, all Students need their own ID and
grade point average, but not all Students need their own
copy of the athletic fee figure. If each Student object
contains a copy of the athletic fee, you repeat the same
information 100 times, wasting memory.
To avoid this, you can declare the athletic fee variable as static,
meaning that only one memory location is allocated, no matter
how many objects of the class you instantiate.
Why is static?
When objects are instantiated, each one gets its own block of
memory for its data members. For example, if you create an array
of 100 objects, 100 blocks of memory are set aside
Sometimes every instantiation of a class requires the same value.
For example
100 Student objects, all Students need their own ID and
grade point average, but not all Students need their own
copy of the athletic fee figure. If each Student object
contains a copy of the athletic fee, you repeat the same
information 100 times, wasting memory.
To avoid this, you can declare the athletic fee variable as static,
meaning that only one memory location is allocated, no matter
how many objects of the class you instantiate.
Why is static?
When objects are instantiated, each one gets its own block of
memory for its data members. For example, if you create an array
of 100 objects, 100 blocks of memory are set aside
Sometimes every instantiation of a class requires the same value.
For example
100 Student objects, all Students need their own ID and
grade point average, but not all Students need their own
copy of the athletic fee figure. If each Student object
contains a copy of the athletic fee, you repeat the same
information 100 times, wasting memory.
To avoid this, you can declare the athletic fee variable as static,
meaning that only one memory location is allocated, no matter
how many objects of the class you instantiate.
Why is static?
When objects are instantiated, each one gets its own block of
memory for its data members. For example, if you create an array
of 100 objects, 100 blocks of memory are set aside
Sometimes every instantiation of a class requires the same value.
For example
100 Student objects, all Students need their own ID and
grade point average, but not all Students need their own
copy of the athletic fee figure. If each Student object
contains a copy of the athletic fee, you repeat the same
information 100 times, wasting memory.
To avoid this, you can declare the athletic fee variable as static,
meaning that only one memory location is allocated, no matter
how many objects of the class you instantiate.
Why is static?
When objects are instantiated, each one gets its own block of
memory for its data members. For example, if you create an array
of 100 objects, 100 blocks of memory are set aside
Sometimes every instantiation of a class requires the same value.
For example
100 Student objects, all Students need their own ID and
grade point average, but not all Students need their own
copy of the athletic fee figure. If each Student object
contains a copy of the athletic fee, you repeat the same
information 100 times, wasting memory.
To avoid this, you can declare the athletic fee variable as static,
meaning that only one memory location is allocated, no matter
how many objects of the class you instantiate.
Defining
Structure
static data_type variable_name;
They belong to the class, and you can use them even if you
never instantiate an object.
They belong to the class, and you can use them even if you
never instantiate an object.
They belong to the class, and you can use them even if you
never instantiate an object.
They belong to the class, and you can use them even if you
never instantiate an object.
It would waste space if you stored the code for the member
functions separately for each object.
It would waste space if you stored the code for the member
functions separately for each object.
It would waste space if you stored the code for the member
functions separately for each object.
It would waste space if you stored the code for the member
functions separately for each object.
It would waste space if you stored the code for the member
functions separately for each object.
It would waste space if you stored the code for the member
functions separately for each object.
Constructor: Example
Example
using namespace std;
class Employee
{
private:
int idNum;
double hourlyRate;
public:
Employee();
void setIdNum(const int);
void setHourlyRate(const double);
int getIdNum();
double getHourlyRate();
};
Employee::Employee()
{
idNum = 9999;
Constructor: overloading
Example
A class can have multiple
constructors, with different
parameters.
This technique is called
overloading the constructor.
class Writer
{
private:
string firstName;
string middleName;
string lastName;
// other data members can go her
public:
Writer(string, string, string)
Writer(string, string);
string toString();
// other functions can go here
};
Destructor
Example
I
class array
{
private:
char* c;
public:
array()
{
c = new char[100];
}
~array()
{
delete c;
}