chapter 4. class and object
TRANSCRIPT
-
8/17/2019 Chapter 4. Class and Object
1/41
Chapter 4. CLASS and OBJECT
Chapter 4. CLASS and OBJECT
Minh Quang Nguyen
Hanoi National University of Education
September 2015
-
8/17/2019 Chapter 4. Class and Object
2/41
Chapter 4. CLASS and OBJECT
Class and Class members
Encapsulating class components
Implementing functions in class
Static class members
this Pointer
Constructor
Q & A
-
8/17/2019 Chapter 4. Class and Object
3/41
Chapter 4. CLASS and OBJECT
Class and Class members
Introduction
Classes are the central feature of C++ that supportsobject-oriented programming and are often called user-definedtypes
A class is a category of objects; it is a new data type youcreate that is more complex than the basic data types
A class is used to specify the form of an object and itcombines data representation and methods for manipulating
that data into one neat package. The data and functions within a class are called members of
the class.
-
8/17/2019 Chapter 4. Class and Object
4/41
Chapter 4. CLASS and OBJECT
Class and Class members
Introduction
Classes are the central feature of C++ that supportsobject-oriented programming and are often called user-definedtypes
A class is a category of objects; it is a new data type youcreate that is more complex than the basic data types
A class is used to specify the form of an object and itcombines data representation and methods for manipulating
that data into one neat package. The data and functions within a class are called members of
the class.
-
8/17/2019 Chapter 4. Class and Object
5/41
Chapter 4. CLASS and OBJECT
Class and Class members
Introduction
Classes are the central feature of C++ that supportsobject-oriented programming and are often called user-definedtypes
A class is a category of objects; it is a new data type youcreate that is more complex than the basic data types
A class is used to specify the form of an object and itcombines data representation and methods for manipulating
that data into one neat package. The data and functions within a class are called members of
the class.
-
8/17/2019 Chapter 4. Class and Object
6/41
Chapter 4. CLASS and OBJECT
Class and Class members
Introduction
Classes are the central feature of C++ that supportsobject-oriented programming and are often called user-definedtypes
A class is a category of objects; it is a new data type youcreate that is more complex than the basic data types
A class is used to specify the form of an object and itcombines data representation and methods for manipulating
that data into one neat package. The data and functions within a class are called members of
the class.
-
8/17/2019 Chapter 4. Class and Object
7/41
Chapter 4. CLASS and OBJECT
Class and Class members
Structure
class
{
data members
function members
};
Example
class Student
{
int id_num;
string last_name;
double grade_point_average;};
-
8/17/2019 Chapter 4. Class and Object
8/41
Chapter 4. CLASS and OBJECT
Class and Class members
Object declaration and use
To declare Student object, the Student name is used just as youuse scalar type names.
Student David;
To refer to the specific attributes of the object:
object_name.atttribute;
Example:
David.id_num = 7645;
cout
-
8/17/2019 Chapter 4. Class and Object
9/41
Chapter 4. CLASS and OBJECT
Class and Class members
Object declaration and use
To declare Student object, the Student name is used just as youuse scalar type names.
Student David;
To refer to the specific attributes of the object:
object_name.atttribute;
Example:
David.id_num = 7645;
cout
-
8/17/2019 Chapter 4. Class and Object
10/41
Chapter 4. CLASS and OBJECT
Class and Class members
Access modifier
By default, all members of a class are private, meaning theycannot be accessed using any statements in any functions that arenot 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 accessiblewhen they are used with a Student object in a main() function.
Ch 4 CLASS d OBJECT
-
8/17/2019 Chapter 4. Class and Object
11/41
Chapter 4. CLASS and OBJECT
Encapsulating class components
What is encapsulating
When you create a class name for a group of associated variables,you contain, or encapsulate, the individual components.Programmers sometimes refer to encapsulation as an example of using a ”black box”.A black box is a device that you can use, but cannot look inside tosee how it works.
In contrast, the interface intercedes between you and the morecomplicated inner workings of the radio
Ch te 4 CLASS d OBJECT
-
8/17/2019 Chapter 4. Class and Object
12/41
Chapter 4. CLASS and OBJECT
Encapsulating class components
What is encapsulating
When you create a class name for a group of associated variables,you contain, or encapsulate, the individual components.Programmers sometimes refer to encapsulation as an example of using a ”black box”.A black box is a device that you can use, but cannot look inside tosee how it works.
In contrast, the interface intercedes between you and the morecomplicated inner workings of the radio
Chapter 4 CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
13/41
Chapter 4. CLASS and OBJECT
Encapsulating class components
What is encapsulating
When you create a class name for a group of associated variables,you contain, or encapsulate, the individual components.Programmers sometimes refer to encapsulation as an example of using a ”black box”.A black box is a device that you can use, but cannot look inside tosee how it works.
In contrast, the interface intercedes between you and the morecomplicated inner workings of the radio
Chapter 4 CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
14/41
Chapter 4. CLASS and OBJECT
Encapsulating class components
Designing a class: A&Q
Before designing a new class, some questions and its answers mustbe stated. For example, when designing class Student
Q: What shall we call it?
A: Student.
Q: What are its attributes? A: It has an integer ID number, a string last name, and a
double grade point average.
Q: What methods are needed by Student?
A: A method to assign values to a member of this class (forexample, one Student’s ID number is 3232, her last name is”Walters”, and her grade point average is 3.45).
Q: Any other methods?
A: A method to display data in a member of this class (for
example, display one Student’s data).
Chapter 4 CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
15/41
Chapter 4. CLASS and OBJECT
Encapsulating class components
Designing a class: an Example
For most object-oriented classes, then, you declare both fields andfunctions
You declare a field using a data type and an identifier.
You declare a function by writing its prototype, which serves
as the interface to the function
class Student
{
private:
int id_num;string last_name;
double grade_point_average;
public:
void display_student_data();
};
Chapter 4. CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
16/41
Chapter 4. CLASS and OBJECT
Implementing functions in class
Implementation section: what is it?
The first part is a declaration section, which contains the classname, variables (attributes), and function prototypes. The secondpart created is an implementation section, which contains thefunctions themselves. Implementation is in class interface Implementation is outside class interface
class Student
{
private:
int id_num;public:
int get_id_num()
{
return id_num;
}
};
Chapter 4. CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
17/41
p
Implementing functions in class
Implementation section: example
Example
// implementation section:
void Student::displayStudentData(){
cout
-
8/17/2019 Chapter 4. Class and Object
18/41
Static class members
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 arrayof 100 objects, 100 blocks of memory are set asideSometimes every instantiation of a class requires the same value.For example100 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 sameinformation 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 matterhow many objects of the class you instantiate.
Chapter 4. CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
19/41
Static class members
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 arrayof 100 objects, 100 blocks of memory are set asideSometimes every instantiation of a class requires the same value.
For example100 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 sameinformation 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 matterhow many objects of the class you instantiate.
Chapter 4. CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
20/41
Static class members
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 arrayof 100 objects, 100 blocks of memory are set asideSometimes every instantiation of a class requires the same value.
For example100 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 sameinformation 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 matterhow many objects of the class you instantiate.
Chapter 4. CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
21/41
Static class members
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 arrayof 100 objects, 100 blocks of memory are set asideSometimes every instantiation of a class requires the same value.
For example100 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 sameinformation 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 matterhow many objects of the class you instantiate.
Chapter 4. CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
22/41
Static class members
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 arrayof 100 objects, 100 blocks of memory are set asideSometimes every instantiation of a class requires the same value.
For example100 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 sameinformation 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 matterhow many objects of the class you instantiate.
Chapter 4. CLASS and OBJECT
-
8/17/2019 Chapter 4. Class and Object
23/41
Static class members
Defining
Structure
static data_type variable_name;
A static data member is defined (given a value) in a single
statement outside the class definition.
class Student
{
private:
int idNum;static double athleticFee;
public:
void setIdNum(int);
int getIdNum();
double getAthleticFee();
};
Chapter 4. CLASS and OBJECT
S l b
-
8/17/2019 Chapter 4. Class and Object
24/41
Static class members
Some features of static data member
A static class member exists, even when you have not
instantiated any objects of the class. They belong to the class, and you can use them even if you
never instantiate an object.
For a non-class member function (such as main()) to use the
static field directly, it cannot be private.
Chapter 4. CLASS and OBJECT
S i l b
-
8/17/2019 Chapter 4. Class and Object
25/41
Static class members
Some features of static data member
A static class member exists, even when you have not
instantiated any objects of the class. They belong to the class, and you can use them even if you
never instantiate an object.
For a non-class member function (such as main()) to use the
static field directly, it cannot be private.
Chapter 4. CLASS and OBJECT
St ti l ss e be s
-
8/17/2019 Chapter 4. Class and Object
26/41
Static class members
Some features of static data member
A static class member exists, even when you have not
instantiated any objects of the class. They belong to the class, and you can use them even if you
never instantiate an object.
For a non-class member function (such as main()) to use the
static field directly, it cannot be private.
Chapter 4. CLASS and OBJECT
Static class members
-
8/17/2019 Chapter 4. Class and Object
27/41
Static class members
Some features of static data member
A static class member exists, even when you have not
instantiated any objects of the class. They belong to the class, and you can use them even if you
never instantiate an object.
For a non-class member function (such as main()) to use the
static field directly, it cannot be private.
Chapter 4. CLASS and OBJECT
Static class members
-
8/17/2019 Chapter 4. Class and Object
28/41
Static class members
static function member
If static data member were private, and you wanted to access
it without using an object, you would have to use a publicfunction to access the value.
Additionally, the function would have to be a static function.
A static function is one you can use with or without a declared
object
Chapter 4. CLASS and OBJECT
Static class members
-
8/17/2019 Chapter 4. Class and Object
29/41
Static class members
static function member
If static data member were private, and you wanted to access
it without using an object, you would have to use a publicfunction to access the value.
Additionally, the function would have to be a static function.
A static function is one you can use with or without a declared
object
-
8/17/2019 Chapter 4. Class and Object
30/41
Chapter 4. CLASS and OBJECT
this Pointer
-
8/17/2019 Chapter 4. Class and Object
31/41
It would waste space if you stored the code for the member
functions separately for each object. C++ does not store member functions separately for each
instance of a class. Instead, one copy of each memberfunction is stored, and each instance of a class uses the same
function code→ When calling a non-static member function, it needs to knowwhich object to use.Within a member function, the address of the calling object isstored in a special pointer called the ”this” pointer. The thispointer holds the memory address of the current object that isusing the function;
Example
Chapter 4. CLASS and OBJECT
this Pointer
-
8/17/2019 Chapter 4. Class and Object
32/41
It would waste space if you stored the code for the member
functions separately for each object. C++ does not store member functions separately for each
instance of a class. Instead, one copy of each memberfunction is stored, and each instance of a class uses the same
function code→ When calling a non-static member function, it needs to knowwhich object to use.Within a member function, the address of the calling object isstored in a special pointer called the ”this” pointer. The thispointer holds the memory address of the current object that isusing the function;
Example
Chapter 4. CLASS and OBJECT
this Pointer
-
8/17/2019 Chapter 4. Class and Object
33/41
It would waste space if you stored the code for the member
functions separately for each object. C++ does not store member functions separately for each
instance of a class. Instead, one copy of each memberfunction is stored, and each instance of a class uses the same
function code→ When calling a non-static member function, it needs to knowwhich object to use.Within a member function, the address of the calling object isstored in a special pointer called the ”this” pointer. The thispointer holds the memory address of the current object that isusing the function;
Example
Chapter 4. CLASS and OBJECT
this Pointer
-
8/17/2019 Chapter 4. Class and Object
34/41
It would waste space if you stored the code for the member
functions separately for each object. C++ does not store member functions separately for each
instance of a class. Instead, one copy of each memberfunction is stored, and each instance of a class uses the same
function code→ When calling a non-static member function, it needs to knowwhich object to use.Within a member function, the address of the calling object isstored in a special pointer called the ”this” pointer. The thispointer holds the memory address of the current object that isusing the function;
Example
Chapter 4. CLASS and OBJECT
this Pointer
-
8/17/2019 Chapter 4. Class and Object
35/41
It would waste space if you stored the code for the member
functions separately for each object. C++ does not store member functions separately for each
instance of a class. Instead, one copy of each memberfunction is stored, and each instance of a class uses the same
function code→ When calling a non-static member function, it needs to knowwhich object to use.Within a member function, the address of the calling object isstored in a special pointer called the ”this” pointer. The thispointer holds the memory address of the current object that isusing the function;
Example
Chapter 4. CLASS and OBJECT
this Pointer
-
8/17/2019 Chapter 4. Class and Object
36/41
It would waste space if you stored the code for the member
functions separately for each object. C++ does not store member functions separately for each
instance of a class. Instead, one copy of each memberfunction is stored, and each instance of a class uses the same
function code→ When calling a non-static member function, it needs to knowwhich object to use.Within a member function, the address of the calling object isstored in a special pointer called the ”this” pointer. The thispointer holds the memory address of the current object that isusing the function;
Example
Chapter 4. CLASS and OBJECT
Constructor
-
8/17/2019 Chapter 4. Class and Object
37/41
Constructor function is a function which is automaticallycalled when you create a new object.
important Constructor name is identical to class name.
Constructor(s) do not return values. If not declaration for constructor, a default constructor is
create.
If there is a declaration of a constructor, there would be no
default constructor.
Chapter 4. CLASS and OBJECT
Constructor
-
8/17/2019 Chapter 4. Class and Object
38/41
Constructor: Example
Exampleusing 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;
Chapter 4. CLASS and OBJECT
Constructor
-
8/17/2019 Chapter 4. Class and Object
39/41
Constructor: overloading
A class can have multipleconstructors, with differentparameters.
This technique is calledoverloading the constructor.
Example
class Writer
{private:
string firstName;
string middleName;
string lastName;
// other data members can go hepublic:
Writer(string, string, string
Writer(string, string);
string toString();
// other functions can go here
};
Chapter 4. CLASS and OBJECT
Constructor
-
8/17/2019 Chapter 4. Class and Object
40/41
Destructor
Only use when heapmemory is allocated for
objects. (what is heapmemory?)
Usually use for free thememory location allocated.
Example
class array
{private:
char* c;
public:
array()
{ c = new char[100];
}
~array()
{
delete c;
}
Chapter 4. CLASS and OBJECT
Q & A
-
8/17/2019 Chapter 4. Class and Object
41/41
QUESTION and ANSWER