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 Lecture02-OOP-Review.ppt

Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
AbdullahJana
 
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
DeepVala5
 
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 sharma
Sandesh Sharma
 

Similar to Lecture02-OOP-Review.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
 

More from 02LabiqaIslam (7)

lecture 30 @@@@@@.pptx
lecture 30 @@@@@@.pptxlecture 30 @@@@@@.pptx
lecture 30 @@@@@@.pptx
 
lecture10 virtual reality and augmented reality.ppt
lecture10 virtual reality and augmented reality.pptlecture10 virtual reality and augmented reality.ppt
lecture10 virtual reality and augmented reality.ppt
 
2 funda.ppt
2 funda.ppt2 funda.ppt
2 funda.ppt
 
garbage collection in c ++.ppt
garbage collection in c ++.pptgarbage collection in c ++.ppt
garbage collection in c ++.ppt
 
Testing method pptx
Testing method pptxTesting method pptx
Testing method pptx
 
Chapter 3 software engineering.pptx
Chapter 3 software engineering.pptxChapter 3 software engineering.pptx
Chapter 3 software engineering.pptx
 
labiqa'd.pptx
labiqa'd.pptxlabiqa'd.pptx
labiqa'd.pptx
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

Lecture02-OOP-Review.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