Engineering Science 1036A/B Lecture Notes - Lecture 24: Memory Management

51 views12 pages
Class
Class: a mechanism that allows a programmer to define new data types by
following the object orientated programming principles
Class provides the option to store collections of related data items and
functions that may have the same/different data types
Arrays are only useful data structures for storing a collection of the same
data type
A class can be used to:
Add functionality to an existing data type
Create a new data type
A class definition combines data and functionality
Analogy
Class Declaration Syntax
class any_valid_name {
Data members and function members are declared here
We can not initialize an data member here (except for the static
const data members
Results in a syntax error
§
Member (data or function) access privilege label:
Data member declarations;
Function member declarations;
};
Example
3 different data members (string, int, float) and function member
(printinfo) are collected together to form a new data type called Student
Class Definition (in-line)
Class Definition: defining the member function of a class
When a class contains function members, we need to define those
functions before we use the class
The member functions can be defined within the class declaration segment
Called in-line declaration
Example
Objects in Classes
An object is a variable of a defined class type
Also referred as "instance of a class"
Example
If "Student" is a class then objects can be declared as:
Student Adam;
Student Eve;
The objects Eve and Adam contains all the characteristics (data and
functions) declared in the class Student, which can be accessed via
member access operator (dot operator)
ostream is a class and cout is an instance (object) of it and istream is a class
and cin is an instance (object) of it
Analogy: Class and Object
The relationship between class-name and object is equivalent to the
relationship between data-type and variable-name
Declaring an object of Student type data follows similar approach of declaring
a variable of int type
An object can be used in:
An array of objects
A function
Both return-type and as a function parameter in the formal
parameter list
§
A function call
In pointer, reference and dynamic memory applications
Can use the assignment (=) operator
Accessing Data Members of a Class
After an object (variable) of a class data type id declared, its data embers can be
accessed using the dot/member-access operator (.)
The member access operator is a period placed between the object's name and
a member name
Q.ID refers to the data member ID for the object Q
Example
Example: Class in a program
Class definition Outside Class
Member functions can be defined outside the class declaration segment
scope resolution operator (::)is required
return_data_type class_name :: function_name (parameter
list){
definition;
}
Class and Object
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in
Class
Class: a mechanism that allows a programmer to define new data types by
following the object orientated programming principles
Class provides the option to store collections of related data items and
functions that may have the same/different data types
Arrays are only useful data structures for storing a collection of the same
data type
A class can be used to:
Add functionality to an existing data type
Create a new data type
A class definition combines data and functionality
Analogy
Class Declaration Syntax
class any_valid_name {
Data members and function members are declared here
We can not initialize an data member here (except for the static
const data members
Results in a syntax error
§
Member (data or function) access privilege label:
Data member declarations;
Function member declarations;
};
Example
3 different data members (string, int, float) and function member
(printinfo) are collected together to form a new data type called Student
Class Definition (in-line)
Class Definition: defining the member function of a class
When a class contains function members, we need to define those
functions before we use the class
The member functions can be defined within the class declaration segment
Called in-line declaration
Example
Objects in Classes
An object is a variable of a defined class type
Also referred as "instance of a class"
Example
If "Student" is a class then objects can be declared as:
Student Adam;
Student Eve;
The objects Eve and Adam contains all the characteristics (data and
functions) declared in the class Student, which can be accessed via
member access operator (dot operator)
ostream is a class and cout is an instance (object) of it and istream is a class
and cin is an instance (object) of it
Analogy: Class and Object
The relationship between class-name and object is equivalent to the
relationship between data-type and variable-name
Declaring an object of Student type data follows similar approach of declaring
a variable of int type
An object can be used in:
An array of objects
A function
Both return-type and as a function parameter in the formal
parameter list
§
A function call
In pointer, reference and dynamic memory applications
Can use the assignment (=) operator
Accessing Data Members of a Class
After an object (variable) of a class data type id declared, its data embers can be
accessed using the dot/member-access operator (.)
The member access operator is a period placed between the object's name and
a member name
Q.ID refers to the data member ID for the object Q
Example
Example: Class in a program
Class definition Outside Class
Member functions can be defined outside the class declaration segment
scope resolution operator (::)is required
return_data_type class_name :: function_name (parameter
list){
definition;
}
Class and Object
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in
Class
Class: a mechanism that allows a programmer to define new data types by
following the object orientated programming principles
Class provides the option to store collections of related data items and
functions that may have the same/different data types
Arrays are only useful data structures for storing a collection of the same
data type
A class can be used to:
Add functionality to an existing data type
Create a new data type
A class definition combines data and functionality
Analogy
Class Declaration Syntax
class any_valid_name {
Data members and function members are declared here
We can not initialize an data member here (except for the static
const data members
Results in a syntax error
Member (data or function) access privilege label:
Data member declarations;
Function member declarations;
};
Example
3 different data members (string, int, float) and function member
(printinfo) are collected together to form a new data type called Student
Class Definition (in-line)
Class Definition: defining the member function of a class
When a class contains function members, we need to define those
functions before we use the class
The member functions can be defined within the class declaration segment
Called in-line declaration
Example
Objects in Classes
An object is a variable of a defined class type
Also referred as "instance of a class"
Example
If "Student" is a class then objects can be declared as:
Student Adam;
Student Eve;
The objects Eve and Adam contains all the characteristics (data and
functions) declared in the class Student, which can be accessed via
member access operator (dot operator)
ostream is a class and cout is an instance (object) of it and istream is a class
and cin is an instance (object) of it
Analogy: Class and Object
The relationship between class-name and object is equivalent to the
relationship between data-type and variable-name
Declaring an object of Student type data follows similar approach of declaring
a variable of int type
An object can be used in:
An array of objects
A function
Both return-type and as a function parameter in the formal
parameter list
§
A function call
In pointer, reference and dynamic memory applications
Can use the assignment (=) operator
Accessing Data Members of a Class
After an object (variable) of a class data type id declared, its data embers can be
accessed using the dot/member-access operator (.)
The member access operator is a period placed between the object's name and
a member name
Q.ID refers to the data member ID for the object Q
Example
Example: Class in a program
Class definition Outside Class
Member functions can be defined outside the class declaration segment
scope resolution operator (::)is required
return_data_type class_name :: function_name (parameter
list){
definition;
}
Class and Object
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Class: a mechanism that allows a programmer to define new data types by following the object orientated programming principles. Class provides the option to store collections of related data items and functions that may have the same/different data types. Arrays are only useful data structures for storing a collection of the same data type. Class declaration syntax class any_valid_name { class any_valid_name { Data members and function members are declared here. We can not initialize an data member here (except for the static const data members. 3 different data members (string, int, float) and function member (printinfo) are collected together to form a new data type called student. Class definition: defining the member function of a class. When a class contains function members, we need to define those functions before we use the class. The member functions can be defined within the class declaration segment. An object is a variable of a defined class type.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents