SlideShare a Scribd company logo
OO-Like C:
Struct Inheritance and Virtual
Functions
Yosen Chen
Outline
● Implement the following Object-Oriented techniques in
Pure C:
○ Struct “inheritance”
○ Member functions using member variables/functions
○ “Virtual” functions
● PS:
○ I’m not going to condemn non-OO like C, but provide more
programming techniques in C. We may base on the task situations to
consider its fitness.
Struct Inheritance
Class Inheritance in C++ and C (1/2)
This is how we do class inheritance in C++:
// A and B are derived from Base
class Base
{
public:
int type;
int op_Base(int i);
};
class A : public Base
{
public:
int* dataA;
int op_A(int i);
};
class B : public Base
{
public:
int* dataB;
int op_B(int i);
};
This is how we do struct “inheritance” in C:
struct Base
{
int type;
int (*op_Base) (struct Base* _this, int i);
};
struct A
{
struct Base _base; // inherit from Base
int* dataA;
int (*op_A) (struct A* _this, int i);
};
struct B
{
struct Base _base; //inherit from Base
int* dataB;
int (*op_B) (struct B* _this, int i);
};
PS: I would tell you why we need _this later
Class Inheritance in C++ and C (1/2)
In C++, for derived class A, we can access
members of both Base and A:
Given we have:
A a;
Then we can use:
a.dataA
a.op_A(5);
a.type
a.op_Base(5);
We can do the same thing in C as we do in
C++:
Given we have:
struct A a;
Then we can use:
a.dataA
a.op_A(&a, 5);
and
a._base.type
a._base.op_Base(&a._base, 5);
or, more elegantly
struct Base* pBase = (struct Base*)&a;
pBase->type
pBase->op_Base(pBase, 5);
PS: in this way, struct A users can access Base
members as they are struct Base users.
Memory Allocation of Struct “Inheritance”
AddrOffset 0~3: type
AddrOffset 4~7: op_Base
AddrOffset 8~11: dataA
AddrOffset 12~15: op_A
...
start address of a
(i.e., &a)
if we declare struct A a;
The memory allocation of a is like:
a._base
a
Remark:
start address of a == &a == &a._base == &a._base.type
a._base.type == ((struct Base*)&a)->type //this concept can be used in virtual
Member Functions Using Other
Members
Member Function Using Other Class
Members (C++ vs. C)
In C++, member functions can access other
class members (functions, variables):
For example, we can define function op_A as
below:
int A::op_A(int i)
{
return op_Base(i) + type + dataA[i];
}
We can do the same thing in C as we do in C++:
(using _this)
int A_op_A(struct A* _this, int i)
{
return _this->_base.op_Base(&_this->_base, i)
+ _this->_base.type
+ _this->dataA[i];
}
For struct construction, we have:
void A_ctor(struct A* _this)
{
Base_ctor(&_this->_base); // call Base ctor
_this->op_A = A_op_A;
...
}
For A’s user, we call:
a.op_A(&a, 5);
Virtual Function
Virtual Functions in C++
Why we need virtual?
● What is virtual function?
○ By Wikipedia’s definition: In object-oriented programming, a virtual function
or virtual method is a function or method whose behavior can be overridden
within an inheriting class by a function with the same signature. This concept is
an important part of the polymorphism portion of object-oriented programming
(OOP)
● Advantages: (better software architecture design)
1. base class users can use derived classes without modifying their
code.
2. we can localize the knowledge of which derived class is being used.
Virtual Functions in C++
In C++, virtual function goes like this:
class Interface {
...
virtual bool execute();
}
class ImpA : public Interface {


virtual bool execute();
}
class ImpADerived : public ImpA {
...
virtual bool execute();
}
For general class Interface users:
void func(Interface* pInterf) { pInterf->execute(); }
ImpADerived a; func(&a);
PS: actually, ImpADerived::execute() is called, instead of Interface::execute()
PS: Inside func(), it doesn’t need to know which version of execute() is being
used.
Class Userhold
Actually,ituses...
“Virtual” Functions in Pure C (1/2)
In C, virtual functions can be achieved via pointer manipulation:
struct Interface {
...
BOOL (*execute) (struct Interface* _this);
}
struct ImpA {
struct Interface _base; //derived from Interface
...
}
struct ImpADerived {
struct ImpA _base; //derived from ImpA


}
in ImpADerived constructor:
void ImpADerived_ctor (struct ImpADerived* _this) {
_this->_base->_base.execute = ImpADerived_execute;


}
continued in the next page
Class Userhold
Actually,ituse...
“Virtual” Functions in Pure C (2/2)
In C, virtual functions can be achieved via pointer manipulation:
The following is how we implement ImpADerived’s execute():
BOOL ImpADerived_execute (struct Interface* _this) {
// here we can check _this->mType to confirm struct version first
struct ImpADerived* _this2 = (struct ImpADerived*)_this;
_this2->doMoreThings(_this2);


}
So just like what we did in C++ virtual, for general Interface users:
void func(struct Interface* pInterf) { pInterf->execute(pInterf); }
struct ImpADerived a;
ImpADerived_ctor(&a);
func((struct Interface*)&a);
Inside func(), ImpADerived::execute() is used instead of Interface::execute()
Now we achieve the same functionality in C as in C++
Class Userhold
Actually,ituses...
Reference & Appendix
● Appendix:
○ Sample code on GitHub:
https://github.com/YosenChen/SoftwareArchitecture
Design/tree/master/OOLikeC-2
○

More Related Content

PDF
Big Data: Big SQL and HBase
PDF
Data Visualisation & Analytics with Tableau (Beginner) - by Maria Koumandraki
PPT
Database System Concepts and Architecture.ppt
PPTX
Introduction to SQL
PPTX
SQL JOIN
PPTX
CockroachDB
PPTX
Type of database models
PPTX
Joins And Its Types
Big Data: Big SQL and HBase
Data Visualisation & Analytics with Tableau (Beginner) - by Maria Koumandraki
Database System Concepts and Architecture.ppt
Introduction to SQL
SQL JOIN
CockroachDB
Type of database models
Joins And Its Types

What's hot (20)

DOCX
PPTX
Introduction to DAX
PDF
Data Visualization with Tableau - by Knowledgebee Trainings
PPTX
Relational databases
PPTX
Tableau
PDF
ETL VS ELT.pdf
PDF
Introduction to Python for Data Science
PPT
Database Presentation
PPTX
Tableau slideshare
PDF
Introduction to Graph Databases
PPTX
Tableau Presentation
PDF
Query optimization in SQL
PPT
SQL.ppt
PDF
oracle-adw-melts snowflake-report.pdf
PDF
Automotive android
PDF
Google BigQuery
ODP
Partitioning
PPTX
Introduction to Tableau
PDF
Big Data Analytics with Spark
PDF
Azure SQL Database
Introduction to DAX
Data Visualization with Tableau - by Knowledgebee Trainings
Relational databases
Tableau
ETL VS ELT.pdf
Introduction to Python for Data Science
Database Presentation
Tableau slideshare
Introduction to Graph Databases
Tableau Presentation
Query optimization in SQL
SQL.ppt
oracle-adw-melts snowflake-report.pdf
Automotive android
Google BigQuery
Partitioning
Introduction to Tableau
Big Data Analytics with Spark
Azure SQL Database
Ad

Similar to OO-like C Programming: Struct Inheritance and Virtual Function (20)

PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
PPTX
OOC MODULE1.pptx
PPT
Lecture5
PPTX
Object Oriented Programming with Object Orinted Concepts
PPT
An imperative study of c
PDF
Dotnet unit 4
PDF
Functions And Header Files In C++ | Bjarne stroustrup
PDF
Oop Presentation
PPT
C++ polymorphism
PDF
Implementation of oop concept in c++
PPT
C++ Inheritance
PPT
Overloading
PDF
Implementation of oop concept in c++
PPS
C++ Language
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PPTX
05 Object Oriented Concept Presentation.pptx
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPTX
C++ language
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Object Oriented Programming (OOP) using C++ - Lecture 4
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
OOC MODULE1.pptx
Lecture5
Object Oriented Programming with Object Orinted Concepts
An imperative study of c
Dotnet unit 4
Functions And Header Files In C++ | Bjarne stroustrup
Oop Presentation
C++ polymorphism
Implementation of oop concept in c++
C++ Inheritance
Overloading
Implementation of oop concept in c++
C++ Language
Chapter 2 OOP using C++ (Introduction).pptx
05 Object Oriented Concept Presentation.pptx
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
C++ language
Ad

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
ai tools demonstartion for schools and inter college
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Introduction to Artificial Intelligence
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
AI in Product Development-omnex systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
top salesforce developer skills in 2025.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
How to Choose the Right IT Partner for Your Business in Malaysia
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......
VVF-Customer-Presentation2025-Ver1.9.pptx
history of c programming in notes for students .pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
ISO 45001 Occupational Health and Safety Management System
ai tools demonstartion for schools and inter college
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Introduction to Artificial Intelligence
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Odoo Companies in India – Driving Business Transformation.pdf
How Creative Agencies Leverage Project Management Software.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
AI in Product Development-omnex systems
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
top salesforce developer skills in 2025.pdf
Design an Analysis of Algorithms II-SECS-1021-03

OO-like C Programming: Struct Inheritance and Virtual Function

  • 1. OO-Like C: Struct Inheritance and Virtual Functions Yosen Chen
  • 2. Outline ● Implement the following Object-Oriented techniques in Pure C: ○ Struct “inheritance” ○ Member functions using member variables/functions ○ “Virtual” functions ● PS: ○ I’m not going to condemn non-OO like C, but provide more programming techniques in C. We may base on the task situations to consider its fitness.
  • 4. Class Inheritance in C++ and C (1/2) This is how we do class inheritance in C++: // A and B are derived from Base class Base { public: int type; int op_Base(int i); }; class A : public Base { public: int* dataA; int op_A(int i); }; class B : public Base { public: int* dataB; int op_B(int i); }; This is how we do struct “inheritance” in C: struct Base { int type; int (*op_Base) (struct Base* _this, int i); }; struct A { struct Base _base; // inherit from Base int* dataA; int (*op_A) (struct A* _this, int i); }; struct B { struct Base _base; //inherit from Base int* dataB; int (*op_B) (struct B* _this, int i); }; PS: I would tell you why we need _this later
  • 5. Class Inheritance in C++ and C (1/2) In C++, for derived class A, we can access members of both Base and A: Given we have: A a; Then we can use: a.dataA a.op_A(5); a.type a.op_Base(5); We can do the same thing in C as we do in C++: Given we have: struct A a; Then we can use: a.dataA a.op_A(&a, 5); and a._base.type a._base.op_Base(&a._base, 5); or, more elegantly struct Base* pBase = (struct Base*)&a; pBase->type pBase->op_Base(pBase, 5); PS: in this way, struct A users can access Base members as they are struct Base users.
  • 6. Memory Allocation of Struct “Inheritance” AddrOffset 0~3: type AddrOffset 4~7: op_Base AddrOffset 8~11: dataA AddrOffset 12~15: op_A ... start address of a (i.e., &a) if we declare struct A a; The memory allocation of a is like: a._base a Remark: start address of a == &a == &a._base == &a._base.type a._base.type == ((struct Base*)&a)->type //this concept can be used in virtual
  • 7. Member Functions Using Other Members
  • 8. Member Function Using Other Class Members (C++ vs. C) In C++, member functions can access other class members (functions, variables): For example, we can define function op_A as below: int A::op_A(int i) { return op_Base(i) + type + dataA[i]; } We can do the same thing in C as we do in C++: (using _this) int A_op_A(struct A* _this, int i) { return _this->_base.op_Base(&_this->_base, i) + _this->_base.type + _this->dataA[i]; } For struct construction, we have: void A_ctor(struct A* _this) { Base_ctor(&_this->_base); // call Base ctor _this->op_A = A_op_A; ... } For A’s user, we call: a.op_A(&a, 5);
  • 10. Virtual Functions in C++ Why we need virtual? ● What is virtual function? ○ By Wikipedia’s definition: In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is an important part of the polymorphism portion of object-oriented programming (OOP) ● Advantages: (better software architecture design) 1. base class users can use derived classes without modifying their code. 2. we can localize the knowledge of which derived class is being used.
  • 11. Virtual Functions in C++ In C++, virtual function goes like this: class Interface { ... virtual bool execute(); } class ImpA : public Interface { 
 virtual bool execute(); } class ImpADerived : public ImpA { ... virtual bool execute(); } For general class Interface users: void func(Interface* pInterf) { pInterf->execute(); } ImpADerived a; func(&a); PS: actually, ImpADerived::execute() is called, instead of Interface::execute() PS: Inside func(), it doesn’t need to know which version of execute() is being used. Class Userhold Actually,ituses...
  • 12. “Virtual” Functions in Pure C (1/2) In C, virtual functions can be achieved via pointer manipulation: struct Interface { ... BOOL (*execute) (struct Interface* _this); } struct ImpA { struct Interface _base; //derived from Interface ... } struct ImpADerived { struct ImpA _base; //derived from ImpA 
 } in ImpADerived constructor: void ImpADerived_ctor (struct ImpADerived* _this) { _this->_base->_base.execute = ImpADerived_execute; 
 } continued in the next page Class Userhold Actually,ituse...
  • 13. “Virtual” Functions in Pure C (2/2) In C, virtual functions can be achieved via pointer manipulation: The following is how we implement ImpADerived’s execute(): BOOL ImpADerived_execute (struct Interface* _this) { // here we can check _this->mType to confirm struct version first struct ImpADerived* _this2 = (struct ImpADerived*)_this; _this2->doMoreThings(_this2); 
 } So just like what we did in C++ virtual, for general Interface users: void func(struct Interface* pInterf) { pInterf->execute(pInterf); } struct ImpADerived a; ImpADerived_ctor(&a); func((struct Interface*)&a); Inside func(), ImpADerived::execute() is used instead of Interface::execute() Now we achieve the same functionality in C as in C++ Class Userhold Actually,ituses...
  • 14. Reference & Appendix ● Appendix: ○ Sample code on GitHub: https://github.com/YosenChen/SoftwareArchitecture Design/tree/master/OOLikeC-2 ○