2-C++ OO Programming
2-C++ OO Programming
2-C++ OO Programming
class Fraction
{ // class members...
~Fraction() // <--- destructor
{ // do something before object is destroyed
cout << "** obbject is going to be destroyed" << end;
}
};
int main()
{ Fraction P5; // P5 is created
return 0; //-->screen:** obbject is going to be destroyed
}
8
Class functions
Fraction Obj;
Obj.add1(p1,p2) returns a new fraction (p1 + p2)
P2-ClassFraction2.cpp
class Fraction
{ public:
int numerator, denominator;
Fraction (int n=0, int d=1) { numerator=n; denominator=d;}
Fraction add1(Fraction p1, Fraction p2)
{ Fraction p3; // create new object p3, p3 = p1 + p2
p3.numerator = p1.numerator*p2.denominator
+ p2.numerator*p1.denominator,
p3.denominator= p1.denominator * p2.denominator;
// should we do simplifying p3 ? do it !
return p3;
}
};
9
Class functions
class Fraction
{ public:
int numerator, denominator;
Fraction (int n=0, int d=1) { numerator=n; denominator=d;}
Fraction add1(Fraction, Fraction); // it's just a prototype
};
Fraction p;
p.SetN(1).SetD(2); // p(1,2)
class Line2D
{
public:
float Distance(Point2D p1, Point2D p2){/*read (x,y) of p1,p2 */}
Point2D Middle(Point2D p1, Point2D p2){ /*...*/ }
}
20
Class inheritance
Inheritance is the ability to create a derived class (child
class) from an existing one (base, or parent class).
The
Thesame
samename
nameand
andtype
type
Different
The same parametters
parametters (=>
but different behaviors.
different behaviors)
int baseclass::fn(int a)
int
intfnderived::fn(int
(int a) a)
int fn (int a, int b)
Different scope
The same scope
baseclass <> derived class
In the same block { },..
Need inheritance
Don't need inheritance
31
Class: function overloading
class Fraction
{ //....
P2-FunctionOverload.cpp
Fraction Add (int n)
{ int n2 = numerator+n*denominator, d = denominator;
Fraction q( n2, d ); return q;
}
Fraction Add (Fraction p) // Add() is overloaded
{ Fraction
q(numerator*p.denominator+p.numerator*denominator,
denominator * p.denominator); return q;
}
Fraction Add (double a) // Add() is overloaded
{ Fraction q(round(a*100),100); return Add(q); // which Add() ?
}
};
Fraction a(1,2); Fraction c = a.Add(2); // 1/2 + 2 = 5/2
Fraction b(1,3); c = b.Add(0.15); // 1/3 + 0.15
32
Class: operator overloading
Fraction a(1,2); a+2 is more expressive than a.Add(2). That's
why we need operator (+,-,...) overloading for user define type
like class or struct.
TimeEn t;
cin >> t; // 08<enter>10<enter>00<enter>
cout << t; // 8:10:0
38
Class scope: overloadable operators
Binary Arithmetic: +, -, *, /, %
Unary Arithmetic: +, -, ++, --
Assignment: =, +=,*=, /=,-=, %=
Bitwise: & , | , << , >> , ~ , ^
Logical: &, | |, !
Relational: >, < , = =, <=, >=
De-referencing: ->
Subscript: []
Function call: ()
Dynamic memory allocation: New, delete
https://www.programiz.com/cpp-programming/operator-
overloading
39
Class scope: Non-overloadable ops.
Scope resolution (::)
Class member access operators
.(dot),
.* (pointer to member operator)
Ternary or conditional (?:)
sizeof
typeid
https://www.programiz.com/cpp-programming/operator-
overloading