0% found this document useful (0 votes)
3 views25 pages

Abstraction 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

Abstraction 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

01

Table of
Abstraction
Contents
02 “Virtual” keyword

03 Virtual function

04 Pure virtual function

05 Abstract class

06
Interface
Abstraction

1 What is “Abstraction” ?
2 When is “Abstraction” used ?

3 Where is “Abstraction” applied ?


4 Why is “Abstraction important ?

5 How is “Abstraction implemented ?


What is “Abstraction” ?

Fundamental concept in OOP

Attributes and behaviors

Simplify the model


When & where is “Abstraction” used ?

Design and development phases

Define class interface

Design large-scale applications


Why is “Abstraction” important ?

Code reusability

Improves maintainability

Building modular systems


How is “Abstraction” implemented ?

Classes and access specifiers

Pure virtual function

Abstract base class


Virtual keyword

01 02 03

Class hierachies Declare virtual in Function


and a base class overriden in
Polymorphism derived class
Virtual function

C++ determines which function is


01 Member function in the base class
to be invoked at the runtime

04 02

Tell the compiler to perform


Redefined in the derived class
dynamic linkage (late binding) 03
Implementation
class Account class Checking: public Account
{ {
public: public:
virtual void withdraw() void withdraw()
{ {
std::cout << "Account::withdraw"; std::cout << "Checking::withdraw";
} }
}; };

int main()
{
Account *ptr1 = new Checking;
PS D:\C++ (OOP)> ./test ptr1->withdraw();
Checking::withdraw
return 0;
}
Rules of virtual function

Base and derived virtual functions


No.1 Members of some classes No.6
must match

Defined in base class, even though


No.2 Cannot be the static member No.5
not use

No.3 Access through object pointers Can be friend of another class No.4
Rules of virtual function
Understand the concept
Instance Context Vtable concept Polymorphism
Object context Vtable and Vptr Runtime Polymorphism
class Account
{
protected:
int data1; int main()
public: {
void withdraw() Account *ptr1 = new Checking;
{
std::cout << "Account::withdraw"
ptr1->withdraw();
<< this->data1;
} return 0;
}; }
class Checking: public Account
{
private:
int data2;
public:
void withdraw()
{
std::cout << "Checking::withdraw"
<< this->data1;
<< this->data2;
}
};
Rules of virtual function
Explanation
Members of some classes Object-Oriented Design
Why 1

Cannot be the static member Virtual vs Static, Polymorphism require


Why 2

Why 3 Accessed through object pointers Support runtime polymorphism

Can be friend of another class Access private data


Why 4

Defined in base class, even though not use virtual table integrity
Why 5

Avoid overloaded
Why 6 Base and derived virtual functions must match
Why 2 – Cannot be the static member
class Base {
public:
virtual void show() { // Hàm ảo
std::cout << "Base class show function\n";
}
static void staticShow() { // Hàm tĩnh
std::cout << "Base class static show function\n";
}
};

class Derived : public Base {


public:
void show() override { // Ghi đè hàm ảo
std::cout << "Derived class show function\n";
}
// static void staticShow() override { // Không được phép
// std::cout << "Derived class static show function";
// }
};

int main() {
Base* b = new Derived();
b->show(); // Gọi hàm show() của lớp Derived

// b->staticShow(); // Sẽ gây lỗi cú pháp


Base::staticShow(); // Gọi hàm staticShow() của lớp Base
}
Why 4 - Can be friend of another class
Explanation
class AnotherClass;
class Base {
public:
virtual void display(AnotherClass& obj){}
};

class AnotherClass {
private:
int privateData;

public:
AnotherClass(int value) : privateData(value) {}
friend class Derived;
};

class Derived : public Base {


public:
void display(AnotherClass& obj) override {
std::cout << "Derived class accessing private data:"
<< obj.privateData << std::endl;
}
};
Late Binding (Dynamic Linkage)

01 02 03

Flexibility Polymorphism Code reusability


Flexibility, Polymorphism and Reusability
class Report { void generateReports(std::vector<Report*>& reports) {
public: for (Report* report : reports) {
virtual void generate() { report->generate();
std::cout << "Generating generic report\n"; }
} }
};
int main() {
AnnualReport annualReport;
QuarterlyReport quarterlyReport;
class AnnualReport : public Report { MonthlyReport monthlyReport;
public:
void generate() override { std::vector<Report*> reports =
std::cout << "Generating annual report\n"; { &annualReport, &quarterlyReport, &monthlyReport
} };
};
generateReports(reports);
class QuarterlyReport : public Report {
public: return 0;
void generate() override { }
std::cout << "Generating quarterly report\n";
}
};

class MonthlyReport : public Report { PS D:\C++ (OOP)> ./test3


public:
void generate() override { Generating annual report
std::cout << "Generating monthly report\n"; Generating quarterly report
}
}; Generating monthly report
class Account
{
Note class Account
{
public: public:
void withdraw() void withdraw()
{ {
std::cout << "Account::withdraw"; std::cout << "Account::withdraw";
} }
}; };

class Checking: public Account class Checking: public Account


{ {
public: public:
void withdraw() virtual void withdraw()
{ {
std::cout << "Checking::withdraw"; PS D:\C++ (OOP)> ./test std::cout << "Checking::withdraw";
} }
}; Account::withdraw };

int main() int main()


{ {
Account *ptr1 = new Checking; Account *ptr1 = new Checking;
ptr1->withdraw(); ptr1->withdraw();

return 0; return 0;
} }
Pure virtual function

1 2 3 4

No Class
Similar with
definition in contains it Define in
virtual
base class. becomes derived class
function
Assign to 0 abstract class
Abstract class

1 2 3 4

Include at
Cannot be Base class
least 1 Intended for
initialized on for other
pure virtual Inheritance
its own classes
function
Implementation
class Account //Abstract class
{
public:
//Pure virtual function
virtual void withdraw() = 0;
};

class Checking: public Account


{
public:
void withdraw()
{
std::cout << "Checking::withdraw";
}
};
Interface

1 2 3 4
Contain no
All member member Doesn’t have
Pure abstract
functions are variables and any
class implemented
pure constructor
methods
Implementation
//Pure abstract class
class Shape
{
public:
//Pure virtual functions
virtual void draw() = 0;
virtual void rotate() = 0;
};
Concept of interface

You might also like