Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 37 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
37
Dung lượng
91,5 KB
Nội dung
Object OrientedProgrammingwith C++/ Session 2/ 1 of 37
More on Classes
Session 2
Object Oriented
Programming with C++/
Session 2/ 2 of 37
Session Objectives
Use the scope resolution operator
Use dynamic memory allocation with
•
New
•
Delete
Use pointers to objects
Define and use Constructors
Define and use Destructors
Define the "Const" keyword
Object Oriented
Programming with C++/
Session 2/ 3 of 37
Session Objectives (Contd.)
Define and use the "this" pointer
Describe how objects and functions are
arranged in memory
•
Static Data Members
•
Static member Functions
Describe type conversions using
•
Converting by assignment
•
Type casting
Object Oriented
Programming with C++/
Session 2/ 4 of 37
Scope resolution operator
Function can be defined outside the class
specifier using a scope resolution operator ::
(double colon symbol) with the function
definition. .
• General syntax:
return_type class_name
::member_functions(arg1, arg2,. . .,argn)
•
The type of member function arguments must exactly
match with the type declared in the class specifier.
Important for defining the member functions
outside the class declaration.
Object Oriented
Programming with C++/
Session 2/ 5 of 37
Scope resolution operator (Contd.)
The left-hand operator of :: must be the name of
the class.
Only the scope operator identifies the function as
a member of a particular class.
Is also used to refer to global variable names in
cases where a global variable and a local variable
share the same name.
•
The syntax used is: ::global_variable
More freedom in naming variables.
• If two variables have different purposes, their names
should reflect the difference.
Object Oriented
Programming with C++/
Session 2/ 6 of 37
Dynamic memory allocation
An object is created when its definition appears
in a program and it is destroyed when its name
goes out of scope or the program terminates.
Useful to create a new object that will exist only
as long as it is needed.
new creates such objects and the operator
delete can be used to destroy them later.
Objects allocated by new and delete are said
to be on the free store.
Object Oriented
Programming with C++/
Session 2/ 7 of 37
New
The new operator is used to create a memory
space for an object of a class
The general syntax of the new operator is:
data_type pointer_variable = new data_type;
For example,
int *p; //pointer to integer type
float *f; //pointer to a float type
p = new int;
//allocates memory for an integer
f = new float; //allocates memory for a float
If call to new is successful, it returns a pointer to
the space that is allocated.
Object Oriented
Programming with C++/
Session 2/ 8 of 37
New (Contd.)
Returns zero if the space is not available or if
some error is detected.
Same syntax for an object. For example,
Student *stu_ptr;
//pointer to an object of type Student
stu_ptr = new Student;
//points to new Student object
The new operator is similar to the malloc()
function used in C.
Object Oriented
Programming with C++/
Session 2/ 9 of 37
Delete
Object created by new exists until it is explicitly
destroyed by delete.
delete pointer_variable;
Example of new and delete.
int *ptr;
ptr = new int;
*ptr = 12;
cout << *ptr;
delete ptr;
•
Always a good practice to delete memory when you are through
with it.
•
Be careful that you do not use pointers to memory that has been
deleted.
Object Oriented
Programming with C++/
Session 2/ 10 of 37
Allocatng Arrays
Allocate blocks consisting of arrays of varying
length using a similar technique.
int *ptr;
ptr = new int[100];
delete [] ptr;
Any time you allocate an array of objects using
new, you must use [] in the delete statement.
Error to delete a variable that has been
malloc'ed and it is an error to free a variable
that was allocated with new.
[...]... Oriented Objects, data members and member functions in memory Object 1 Object 3 data 1 data 1 mem_function1() data 2 data 2 mem_function2() Object2 data 1 data 2ObjectOriented Static Data Members Useful when all objects of the same class must share a common item of information If a data item in a class is defined as static, then only one such item is created for the entire class, no matter how many objects... objects involved One is the pointer itself and the other is object pointed to Prefixing a declaration of a pointer with const makes the object, but not the pointer, a constant Example int num =10; const int *iptr = # *iptr = 25 ; //error num = 25 ; //valid, num is not constant int xyz = 20 0; iptr = &xyz; //iptr can point anywhere else *iptr = 305; //error ObjectOriented Const with pointers (Contd.) Also... A constant is an entity whose value does not change during the execution of a program The keyword const can be added to the declaration of an object to make that object a constant rather than a variable A constant cannot be assigned to, so it must be initialised const int num=100; num =20 0; //error num++; //error ObjectOriented Const with pointers When we use const with a pointer, there are two objects... scope-resolution operator as shown: alpha::display_count(); ObjectOriented Type Conversions Type conversion is done to convert a variable of a declared type to some other required type int count = 7; float average = 15.5; double totalsum = count * average; Converting data types can be done in two ways: • Converting by assignment • Type casting ObjectOriented Converting by assignment Typical way of converting... to an object of type date today_ptr = new date; //points to the new date object Since today_ptr is a pointer to an object use arrow operator (-> ) today_ptr->getdate(); ObjectOriented Constructors A constructor is a special member function for automatic initialisation of an object Has the same name as the class it belongs to Can declare and define constructors within the class, or declare them within... functions Object Oriented Objects and functions in memory Each object has its own copy of the data members of the class All the objects in a given class use the same member functions The member functions are created and placed in memory only once - when they are defined in the class specifier Data is therefore placed in memory when each object is defined, so there is a set for each object Object Oriented. .. Constructors (Contd.) class date{ int month, day, year; public: date() //default constructor {day=1; month=1; year=1999;} date(int x) //only day is specified {day=x; month=1; year=1999;} date(int x, int y, int z) //day month year {day=x; month=y; year=z;} }; Object Oriented Constructors (Contd.) As long as the constructors differ sufficiently in their argument types the compiler can select the correct one for... functions Object Oriented Constructors (Contd.) class username { public: username(); //constructor }; username::username() { } No return type is used for constructors Also invoked when local or temporary objects of a class are created Several constructors provide several ways of initialising a class object • A default constructor is a constructor that does not have any arguments Object Oriented Constructors... like any other pointer to an object Can be used to access the members of the object it points to with the use of the arrow operator this->age = 5; this->getdata(); ObjectOriented Use of this class Person{ private: int age; public: void display(); }; void Person :: display() { this->age = 25 ; cout .
Object Oriented Programming with C++/ Session 2/ 1 of 37
More on Classes
Session 2
Object Oriented
Programming with C++/
Session 2/ 2 of 37
Session Objectives.
initialised.
const int num=100;
num =20 0; //error
num++; //error
Object Oriented
Programming with C++/
Session 2/ 20 of 37
Const with pointers
When we use const with