SlideShare a Scribd company logo
1 of 17
Object-Oriented Programming
Review
1
Object-Oriented Programming
Object-Oriented Programming languages vary but
generally all support the following features:
Data Abstraction (encapsulation) – Define types
with an interface and hidden implementation.
Inheritance – Classes can inherit data or
behaviour.
Polymorphism – allows you create a hierarchy of
classes with a common interface.
2
C++ Revision
Classes have a public interface (methods only)
and a private implementation containing all data
members.
class Point {
public:
Point(int a, int b=0): x(a), y(b) {}
void draw() const;
private:
int x, y;
};
3
C++ Revision
Constructors are used to create and initialise a
new object. Every constructor should initalise
every data member.
A constructor with 1 parameter defines an
implicit conversion unless the constructor is
declared explicit.
Point p = 3; // Point p(3, 0);
p = 4; // p = Point(4)
4
C++ Revision
If you don’t write any constructors, the compile
will supply a default constructor.
Point(): x(), y() {}
The compiler also supplies default destructor,
copy constructor and assignment operator.
~Point() {}
Point(const Point& p): x(p.x), y(p.y) {}
Point& operator=(const Point& p) {
x = p.x; y = p.y; return *this;
}
5
C++ Revision
C++ classes can be referred to in three different
ways. Each have implications when assigning
variables to each other:
Point p(5, 10); // Actual instance
Point *p1 = &p; // p1 is a pointer to p
Point &p2 = p; // P2 is a reference to p
Point p3 = p; // Copy Constructor
p3 = p; // Assignment operator
Point *p4 = p1; // p1 and p3 both point to p
Point &p5 = p2; // (Same as Point p3 = p;)
6
C++ Revision
Methods which are defined in the class are
inline. Functions longer than a few lines should
not be defined here.
If the method has default arguments, they should
only be specified once.
What is the efficiency of passing instances,
references, pointers?
7
C++ Revision
Methods have access to private members of other
objects of the same class.
A function or class can be declared as a friend.
A friend has access to private data members.
8
C++ Static
Static data members are shared by all objects of a
class. It is initialised in a declaration outside the
class.
class Counter {
public:
Counter() {++count;}
~Counter() {--count;}
Counter(const Counter& c) {++count;}
Counter& operator=(const Counter& c) {return *this;}
static int getCount() {return count;}
private:
static int count;
};
int Counter::count = 0;
9
C++ Operators
Operators may be overloaded for classes as either
a global function with one parameter for each
operand or as a method of the left-hand operand.
Point operator+(const Point &a, const Point &b) {
return Point(a.getX()+b.getX(), a.getY()+b.getY());
}
Point Point::operator+(const Point &b) {
return Point(x+b.x, y+b.y);
}
10
C++ Inheritance
Inheritance allows a derived class to inherit all
the members of a base class except the
constructors, destructor and assignment
operator.
class Derived: public Base { ... };
class Duck: public Bird { ... };
class Square: public Rectangle { ... };
11
C++ Inheritance
Important terms you should know:
• pubic, private, protected base classes.
• Abstract and nonabstract base classes
• Virtual and nonvirtual member functions
12
C++ Polymorphism
Polymorphism class hierarchies consist of an
interface (abstract base class) and a set of derived
implementations.
Typically the base class has no data members or
code and derived classes do not add any new
public methods (except constructors).
13
C++ Overriding Methods
A method overriding a base method must have
the same parameters, return type and const-ness.
An overridden method must be virtual (in base
class) if the correct version is to be called through
a base pointer or reference.
14
C++ Programming
Generally you should not create objects of an
abstract class or interface.
The abstractness of a base class can be enforced
by either using protected constructors or pure
(=0) virtual methods.
A class with overridden methods should have a
public virtual destructor, even if it does nothing.
15
Example
class Complex{
public:
Complex(double real, double imaginary=0):
_real(real),_imaginary(imaginary){}
void operator+(Complex other){
_real = _real + other._real;
_imaginary = _imaginary + other._imaginary;
}
void operator <<(ostream os){
os <<"("<<_real<<","<<_imaginary<<")";
}
Complex operator++(){
++_real;
return *this;
}
Complex operator++(int){
Complex temp = *this;
++_real;
return temp;
}
private:
double _real,_imaginary;
};
16
Example
class Complex{
public:
explicit Complex(double real, double imaginary=0):
mReal(real),mImaginary(imaginary){}
Complex& operator+=(const Complex& other){
mReal += other.mReal;
mImaginary += other.mImaginary;
return *this;
}
Complex& operator++(){
++mReal;
return *this;
}
const Complex operator++(int){
Complex temp(*this);
++*this;
return temp;
}
private:
double mReal, mImaginary;
};
ostream& Print(ostream& os, Complex a) const {
return os << ...
}
17

More Related Content

Similar to polymorphism.ppt

C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxurvashipundir04
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asmaAbdullahJana
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.pptDeepVala5
 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Ali Raza Zaidi
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan collegeahmed hmed
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxRAJASEKHARV10
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaSandesh Sharma
 

Similar to polymorphism.ppt (20)

Inheritance
InheritanceInheritance
Inheritance
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
L6
L6L6
L6
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
 

Recently uploaded

ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

Recently uploaded (20)

ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

polymorphism.ppt

  • 2. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features: Data Abstraction (encapsulation) – Define types with an interface and hidden implementation. Inheritance – Classes can inherit data or behaviour. Polymorphism – allows you create a hierarchy of classes with a common interface. 2
  • 3. C++ Revision Classes have a public interface (methods only) and a private implementation containing all data members. class Point { public: Point(int a, int b=0): x(a), y(b) {} void draw() const; private: int x, y; }; 3
  • 4. C++ Revision Constructors are used to create and initialise a new object. Every constructor should initalise every data member. A constructor with 1 parameter defines an implicit conversion unless the constructor is declared explicit. Point p = 3; // Point p(3, 0); p = 4; // p = Point(4) 4
  • 5. C++ Revision If you don’t write any constructors, the compile will supply a default constructor. Point(): x(), y() {} The compiler also supplies default destructor, copy constructor and assignment operator. ~Point() {} Point(const Point& p): x(p.x), y(p.y) {} Point& operator=(const Point& p) { x = p.x; y = p.y; return *this; } 5
  • 6. C++ Revision C++ classes can be referred to in three different ways. Each have implications when assigning variables to each other: Point p(5, 10); // Actual instance Point *p1 = &p; // p1 is a pointer to p Point &p2 = p; // P2 is a reference to p Point p3 = p; // Copy Constructor p3 = p; // Assignment operator Point *p4 = p1; // p1 and p3 both point to p Point &p5 = p2; // (Same as Point p3 = p;) 6
  • 7. C++ Revision Methods which are defined in the class are inline. Functions longer than a few lines should not be defined here. If the method has default arguments, they should only be specified once. What is the efficiency of passing instances, references, pointers? 7
  • 8. C++ Revision Methods have access to private members of other objects of the same class. A function or class can be declared as a friend. A friend has access to private data members. 8
  • 9. C++ Static Static data members are shared by all objects of a class. It is initialised in a declaration outside the class. class Counter { public: Counter() {++count;} ~Counter() {--count;} Counter(const Counter& c) {++count;} Counter& operator=(const Counter& c) {return *this;} static int getCount() {return count;} private: static int count; }; int Counter::count = 0; 9
  • 10. C++ Operators Operators may be overloaded for classes as either a global function with one parameter for each operand or as a method of the left-hand operand. Point operator+(const Point &a, const Point &b) { return Point(a.getX()+b.getX(), a.getY()+b.getY()); } Point Point::operator+(const Point &b) { return Point(x+b.x, y+b.y); } 10
  • 11. C++ Inheritance Inheritance allows a derived class to inherit all the members of a base class except the constructors, destructor and assignment operator. class Derived: public Base { ... }; class Duck: public Bird { ... }; class Square: public Rectangle { ... }; 11
  • 12. C++ Inheritance Important terms you should know: • pubic, private, protected base classes. • Abstract and nonabstract base classes • Virtual and nonvirtual member functions 12
  • 13. C++ Polymorphism Polymorphism class hierarchies consist of an interface (abstract base class) and a set of derived implementations. Typically the base class has no data members or code and derived classes do not add any new public methods (except constructors). 13
  • 14. C++ Overriding Methods A method overriding a base method must have the same parameters, return type and const-ness. An overridden method must be virtual (in base class) if the correct version is to be called through a base pointer or reference. 14
  • 15. C++ Programming Generally you should not create objects of an abstract class or interface. The abstractness of a base class can be enforced by either using protected constructors or pure (=0) virtual methods. A class with overridden methods should have a public virtual destructor, even if it does nothing. 15
  • 16. Example class Complex{ public: Complex(double real, double imaginary=0): _real(real),_imaginary(imaginary){} void operator+(Complex other){ _real = _real + other._real; _imaginary = _imaginary + other._imaginary; } void operator <<(ostream os){ os <<"("<<_real<<","<<_imaginary<<")"; } Complex operator++(){ ++_real; return *this; } Complex operator++(int){ Complex temp = *this; ++_real; return temp; } private: double _real,_imaginary; }; 16
  • 17. Example class Complex{ public: explicit Complex(double real, double imaginary=0): mReal(real),mImaginary(imaginary){} Complex& operator+=(const Complex& other){ mReal += other.mReal; mImaginary += other.mImaginary; return *this; } Complex& operator++(){ ++mReal; return *this; } const Complex operator++(int){ Complex temp(*this); ++*this; return temp; } private: double mReal, mImaginary; }; ostream& Print(ostream& os, Complex a) const { return os << ... } 17