C++ - Object-Oriented Programming - Part2 - UP
C++ - Object-Oriented Programming - Part2 - UP
C++ - Object-Oriented Programming - Part2 - UP
3
Content
4
Method and Property Members
5
Function vs. Method
6
Member functions/ Methods
7
Member functions/ Methods
class Point {
float _x, _y;
public:
void setXY(float x, float y);//declaration
float getX(){ return _x; } //definition
float getY(){ return _y; } //definition
float distanceTo(Point p); //declaration
};
void Point::setXY(float x, float y){
_x = x;
_y = y;
}
float Point::distanceTo(Point p){
float d = (p._x-_x)*(p._x-_x) + (p._y-_y)*(p._y-_y);
return sqrt(d);
}
int main()
{
Point p1, p2;
p1.setXY(10,10);
p2.setXY(20,20);
cout<<"D="<<p1.distanceTo(p2)<<endl;
return EXIT_SUCCESS;
}
8
Static members
• A static property: exists only once for all objects of a class (even if
no objects have been created yet)
• Must initialize out of the class
• Static method: used to handle static properties of the class
class C { C c1, c2, c3[10],
public: *c4 = new C(),
static int count; *c5 = new C[20];
C() { count++; } delete c4;
~C() { count--; }
static int getCount() cout << “Number of current C objects: "
{ return count; } << C::getCount() << endl;
... // cout << C::count << endl;
}; // cout << c1.count << endl;
int C::count = 0; // cout << c2.getCount() << endl;
9
Constant method
10
Friend function, Friend class
11
Function overloading
• Multiple functions in the same scope can have the same name
• but must be different in input parameters (number of parameters, type of
each parameter)
1. int compare(int n1, int n2);
2. int compare(float x1, float x2);
3. bool compare(float x1, float x2);
4. int compare(string& s1, string& s2);
5. int compare(const string& s1, const string& s2);
12
Method overloading in class
• The same
• class C {
public:
int compare(int x, int y);
int compare(float x, float y);
};
• [Note] Overlapping in the subclass will hide the methods of the parent class with
the same name
• class D : public C {
public:
int compare(string s1, string s2);
};
D d;
d.compare("1234", "abcd"); // OK
d.compare(10, 20); // error
d.C::compare(10, 20); // OK
13
Default parameter of function/method
14
Default parameter of function/method
15
Default parameter of function/method
16
Default parameter of function/method
17
Operator overloading
18
Concept
19
Concept
20
Overloading for an unary operator
21
Overloading for an unary operator
• Usage:
• Vector v1(1.2, 2.3, 4.5), v2;
v2 = -v1; // Can be used with both definitions above
22
Increment and Decrement operators (++ and --)
23
Type conversion operator
24
Binary operator overloading
25
Comparison operators
• Example:
• class Vector {
public:
bool operator ==(const Vector& v) const // in class
{ return x == v.x && y == v.y; }
friend bool operator !=(const Vector&, const Vector&);
};
• // out of class:
bool operator !=(const Vector& v1, const Vector& v2)
{ return !(v1==v2); } // reuse == operator
26
Assignment operators
27
Assignment operator =
28
new, new[], delete, delete[] operators
29
Other special operators
30
cout, cin and the input/output operator
The examples here are for illustrative purposes only. Actually the ostream and istream classes are not defined exactly like here. See
more in the section about STL.
31
Input/output operator overloading
32
Exercises
1. Định nghĩa đầy đủ các toán tử cho lớp Vector 3 thành phần: cộng, trừ, nhân
với số, tích vô hướng và có hướng
2. Định nghĩa các toán tử cho lớp String: + (cộng chuỗi hoặc ký tự), chuyển kiểu,
xuất/nhập, [ ] (lấy phần tử)
3. Viết một lớp Array cho mảng động với các toán tử: += (thêm phần tử, nối hai
mảng), [ ], chuyển kiểu
33
Exercises
34