Sunteți pe pagina 1din 41

Chapter 4.

CLASS and OBJECT

Chapter 4. CLASS and OBJECT


Minh Quang Nguyen
Hanoi National University of Education

September 2015

Chapter 4. CLASS and OBJECT

Class and Class members


Encapsulating class components
Implementing functions in class
Static class members
this Pointer
Constructor
Q&A

Chapter 4. CLASS and OBJECT


Class and Class members

Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I

A class is a category of objects; it is a new data type you


create that is more complex than the basic data types

A class is used to specify the form of an object and it


combines 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.

Chapter 4. CLASS and OBJECT


Class and Class members

Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I

A class is a category of objects; it is a new data type you


create that is more complex than the basic data types

A class is used to specify the form of an object and it


combines 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.

Chapter 4. CLASS and OBJECT


Class and Class members

Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I

A class is a category of objects; it is a new data type you


create that is more complex than the basic data types

A class is used to specify the form of an object and it


combines 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.

Chapter 4. CLASS and OBJECT


Class and Class members

Introduction
Classes are the central feature of C++ that supports
object-oriented programming and are often called user-defined
types
I

A class is a category of objects; it is a new data type you


create that is more complex than the basic data types

A class is used to specify the form of an object and it


combines 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.

Chapter 4. CLASS and OBJECT


Class and Class members

Structure

Example

class
{
data members
function members
};

class Student
{
int id_num;
string last_name;
double grade_point_average;
};

Chapter 4. CLASS and OBJECT


Class and Class members

Object declaration and use


To declare Student object, the Student name is used just as you
use scalar type names.
Student David;

To refer to the specific attributes of the object:


object_name.atttribute;

Example:
David.id_num = 7645;
cout << David.id_num;

Cannot access private class member declared in class Student

Chapter 4. CLASS and OBJECT


Class and Class members

Object declaration and use


To declare Student object, the Student name is used just as you
use scalar type names.
Student David;

To refer to the specific attributes of the object:


object_name.atttribute;

Example:
David.id_num = 7645;
cout << David.id_num;

Cannot access private class member declared in class Student

Chapter 4. CLASS and OBJECT


Class and Class members

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.

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 to
see how it works.
In contrast, the interface intercedes between you and the more
complicated inner workings of the radio

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 to
see how it works.
In contrast, the interface intercedes between you and the more
complicated inner workings of the radio

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 to
see how it works.
In contrast, the interface intercedes between you and the more
complicated inner workings of the radio

Chapter 4. CLASS and OBJECT


Encapsulating class components

Designing a class: A&Q


Before designing a new class, some questions and its answers must
be stated. For example, when designing class Student
I Q: What shall we call it?
I A: Student.
I Q: What are its attributes?
I A: It has an integer ID number, a string last name, and a
double grade point average.
I Q: What methods are needed by Student?
I A: A method to assign values to a member of this class (for
example, one Students ID number is 3232, her last name is
Walters, and her grade point average is 3.45).
I Q: Any other methods?
I A: A method to display data in a member of this class (for
example, display one Students data).

Chapter 4. CLASS and OBJECT


Encapsulating class components

Designing a class: an Example


For most object-oriented classes, then, you declare both fields and
functions
I

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


Implementing functions in class

Implementation section: what is it?


The first part is a declaration section, which contains the class
name, variables (attributes), and function prototypes. The second
part created is an implementation section, which contains the
functions themselves.
I Implementation is in class interface
I Implementation is outside class interface
class Student
{
private:
int id_num;
public:
int get_id_num()
{
return id_num;
}
};

Chapter 4. CLASS and OBJECT


Implementing functions in class

Implementation section: example

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;
}

Chapter 4. CLASS and OBJECT


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 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.

Chapter 4. CLASS and OBJECT


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 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.

Chapter 4. CLASS and OBJECT


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 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.

Chapter 4. CLASS and OBJECT


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 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.

Chapter 4. CLASS and OBJECT


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 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.

Chapter 4. CLASS and OBJECT


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


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

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

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

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

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 public
function 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

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 public
function 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

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 public
function 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


this Pointer

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 member
function is stored, and each instance of a class uses the same
function code

When calling a non-static member function, it needs to know


which object to use.
Within a member function, the address of the calling object is
stored in a special pointer called the this pointer. The this
pointer holds the memory address of the current object that is
using the function;
Example

Chapter 4. CLASS and OBJECT


this Pointer

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 member
function is stored, and each instance of a class uses the same
function code

When calling a non-static member function, it needs to know


which object to use.
Within a member function, the address of the calling object is
stored in a special pointer called the this pointer. The this
pointer holds the memory address of the current object that is
using the function;
Example

Chapter 4. CLASS and OBJECT


this Pointer

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 member
function is stored, and each instance of a class uses the same
function code

When calling a non-static member function, it needs to know


which object to use.
Within a member function, the address of the calling object is
stored in a special pointer called the this pointer. The this
pointer holds the memory address of the current object that is
using the function;
Example

Chapter 4. CLASS and OBJECT


this Pointer

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 member
function is stored, and each instance of a class uses the same
function code

When calling a non-static member function, it needs to know


which object to use.
Within a member function, the address of the calling object is
stored in a special pointer called the this pointer. The this
pointer holds the memory address of the current object that is
using the function;
Example

Chapter 4. CLASS and OBJECT


this Pointer

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 member
function is stored, and each instance of a class uses the same
function code

When calling a non-static member function, it needs to know


which object to use.
Within a member function, the address of the calling object is
stored in a special pointer called the this pointer. The this
pointer holds the memory address of the current object that is
using the function;
Example

Chapter 4. CLASS and OBJECT


this Pointer

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 member
function is stored, and each instance of a class uses the same
function code

When calling a non-static member function, it needs to know


which object to use.
Within a member function, the address of the calling object is
stored in a special pointer called the this pointer. The this
pointer holds the memory address of the current object that is
using the function;
Example

Chapter 4. CLASS and OBJECT


Constructor

Constructor function is a function which is automatically


called 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

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;

Chapter 4. CLASS and OBJECT


Constructor

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
};

Chapter 4. CLASS and OBJECT


Constructor

Destructor
Example
I

Only use when heap


memory is allocated for
objects. (what is heap
memory?)
Usually use for free the
memory location allocated.

class array
{
private:
char* c;
public:
array()
{
c = new char[100];
}
~array()
{
delete c;
}

Chapter 4. CLASS and OBJECT


Q&A

QUESTION and ANSWER

S-ar putea să vă placă și