Computer Aided Engineering
Lecture 6
Advanced Class Operations
Dr C.F. Kwong
chiew-foong.kwong@nottingham.edu.cn
Department of Electrical and Electronic Engineering
Faculty of Science and Engineering
Room: PMB 310
Topics
Introducing ”This” operator
Assignment Operator
Static Members and Functions
Overloading Operators
The this Pointer
this: predefined pointer available to a class’s
member functions
Always points to the instance (object) of the class
whose function is being called
Is passed as a hidden argument to all non-static
member functions
Can be used to access members that may be hidden
by parameters with same name
this Pointer Example
class SomeClass
{
private:
int num;
public:
void setNum(int num)
{ this->num = num; }
...
};
Introducing “this”
const int_vector& int_vector::operator=(const int_vector& a){
if(this==&a) return(*this);
......
It would be silly to waste time doing x=x if x is big - so we need to detect this somehow.
However, this means that the instance of the class being copied into must know who they are!
this is a pointer to the object itself; Layout of an instance (e.g. a) in memory
20000000
For example, if the instance were laid out in memory number_of_elements 10
as shown, then this would return 2000000 20000004
values 4000000
Therefore if(this==&a)... can be translated as
“if my address is the same as that of the argument then the argument must be me!”
Similarly, return(*this); can be translated as
“return a reference to the object pointed to by this, i.e. me.”
Topics
Introducing ”This” operator
Assignment Operator
Static Members and Functions
Overloading Operators
Memberwise Assignment
Can use = to assign one object to another, or to
initialize an object with an object’s data
Copies member to member. e.g.,
instance2 = instance1; means:
copy all member values from instance1 and assign to
the corresponding member variables of instance2
Use at initialization:
Rectangle r2 = r1;
Assignment Operator
class int_vector{
public:
int_vector(int n); // Constructor
~int_vector(); // Destructor
int_vector (const int_vector& a); // Copy constructor
// Assignment
const int_vector& operator=(const int_vector& a);
main() {
int get_value(int i);
void set_value(int i, int val); int_vector A(10);
private: To A.set_value(0,1.23);
pe
int number_of_elements; rf A.set_value(1,2.45);
or
m
int *values; int_vector B(10);
};
B=A;
}
Assignment Operator
const int_vector& int_vector::operator=(const int_vector& a){
if(this==&a) return(*this);
if(number_of_elements !=a.number_of_elements) {
// The existing array is not the same size as that of a, so destroy it and create a new one
number_of_elements =a.number_of_elements;
delete[] values;
values=new int[number_of_elements];
}
// Copy the array elements
for(int i=0;i<number_of_elements;i++) values[i]=a.values[i];
return(*this);
}
Difference with Copy Constructor
float z(x); // Initialisation from an existing float
z=func(x);// Copies of x made to be passed to the function and also the
float return value is “copied” to z
We have dealt with the first line, (copy constructor) but there is a difference in the
second line.
z already exists when we copy the function’s return value into it. We don’t want to
create a new float with z=func(x); just change the values of the existing object, z.
This is an assignment not a copy construction.
The comments regarding default compiler behaviour still apply, so we need to
explicitly explain what we intend assignment (=) to do.
Difference with Copy Constructor
Note that in this example, we only need to create a new array if the existing one
is not the same size as that of the argument a (consider the run-time speed!)
The basic syntax of the assignment operator is always the same, both
argument and return value are const references to an instance.
Remember the difference between
copy_constructor: construct a new instance and initialise it by copying an existing instance
operator=: overwrite the data of one existing instance with a copy of the data of another
existing instance
// Here’s a little time saver, given how similar the implementations often are. Is it always “safe to do”?
class my_object() {
...
my_object(const my_object& an_object_to_be_copied) { // Copy constructor
*this= an_object_to_be_copied;
const my_object& operator=(const my_object& an_object_to_be_copied); // Operator=
};
Summary
In order that our class objects behave just like the intrinsic types such as float, 4 special
member functions are always needed; constructor, destructor, copy constructor,
assignment operator.
If we do not provide them then the compiler will make its own guess for them which is
unreliable.
All well designed classes must be explicitly given these 4 functions. The syntax of the
definitions is always the same.
class my_object() {
public:
my_object( argument list); // Constructor
~my_object(); // Destructor
my_object(const my_object& an_object_to_be_copied); // Copy constructor
const my_object& operator=(const my_object& an_object_to_be_copied);
// Assignment operator
};
Topics
Assignment Operator
Static Members and Functions
Overloading Operators
Instance and Static Members
instance variable: a member variable in a class. Each
object has its own copy.
static variable: one variable shared among all
objects of a class
static member function: can be used to access
static member variable; can be called before any
objects are defined
static member variable
Contents of Tree.h
1 // Tree class Static member declared here.
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 int getObjectCount() const
13 { return objectCount; } Static member defined here.
14 };
15
16 // Definition of the static member variable, written
17 // outside the class.
18 int Tree::objectCount = 0;
Three Instances of the Tree Class, But Only One
objectCount Variable
static member function
Declared with static before return type:
static int getObjectCount() const
{ return objectCount; }
Static member functions can only access static
member data
Can be called independent of objects:
int num = Tree::getObjectCount();
Modified Version of Tree.h
1 // Tree class
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 static int getObjectCount() const
13 { return objectCount; }
14 };
15
16 // Definition of the static member variable, written
17 // outside the class.
18 int Tree::objectCount = 0;
Now we can call the function like this:
cout << "There are " << Tree::getObjectCount()
<< " objects.\n";
Topics
Assignment Operator
Static Members and Functions
Overloading Operators
Operator Overloading
Operators such as =, +, and others can be redefined when used
with objects of a class
The name of the function for the overloaded operator is
operator followed by the operator symbol, e.g.,
operator+ to overload the + operator, and
operator= to overload the = operator
Prototype for the overloaded operator goes in the declaration of
the class that is overloading it
Overloaded operator function definition goes with other
member functions
Operator Overloading
Prototype:
void operator=(const SomeClass &rval)
parameter for
return function object on right
type name side of operator
Operator is called via object on left side
Invoking an Overloaded Operator
Operator can be invoked as a member function:
object1.operator=(object2);
It can also be used in more conventional manner:
object1 = object2;
Returning a Value
Overloaded operator can return a value
class Point2d
{
public:
double operator-(const point2d &right)
{ return sqrt(pow((x-right.x),2)
+ pow((y-right.y),2)); }
...
private:
int x, y;
};
Point2d point1(2,2), point2(4,4);
// Compute and display distance between 2 points.
cout << point2 – point1 << endl; // displays 2.82843
Example
So we can now overload any other operator we choose.
For example it would be nice to add complex numbers using a simple +, i.e.
complex a,b,c;
...
c=a+b;
Because this is how we naturally represent complex number addition and
therefore we should make sure this possible in our codes. OK, easy to do:
the operator in question is
complex operator+(const complex& x);
so in fact c=a+b; above is shorthand for c=a.operator+(b);
and we provide the implementation as follows,
Example
main(){
class complex {
... complex a,b,c;
complex operator+(const complex&);
...
private: c=a+b;
}
float re,im;
};
complex complex::operator+(const complex &f){
complex sum(f); // We need a new object to hold the sum
sum.re+=re;
sum.im+=im;
return(sum); // Return the sum
}
Straightforward! Now we can define the full suite of +,-,*,/ in exactly the
same way