UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do I use structs in C to emulate classes?

Asked by Last Modified  

Follow 3
Answer

Please enter your answer

Follow below example program: typedef struct object object, *setCoordinates; struct object { float x, y; void (*setCoordinates)(object *self, float x, float y); void (*getYCoordinate)(object *self); void (*getXCoordinate)(object *self); }; void object_setCoordinates(object...
read more
Follow below example program: typedef struct object object, *setCoordinates; struct object { float x, y; void (*setCoordinates)(object *self, float x, float y); void (*getYCoordinate)(object *self); void (*getXCoordinate)(object *self); }; void object_setCoordinates(object *self, float x, float y){ self->x = x; self->y = y; } float object_getXCoordinate(object *self){ return self->x; } float object_getYCoordinate(object *self){ return self->y; } object point; point.setCoordinates = object_setCoordinates; point.getYCoordinate = object_getYCoordinate; point.getXCoordinate = object_getXCoordinate; point.setCoordinates(&point, 1, 2); printf("Coordinates: X Coordinate: %f, Y Coordinate: %f", point.getXCoordinate, point.getYCoordinate); read less
Comments

Tutor

Classes are structures with a hidden pointer to a table of function pointers called VTable. The Vtable itself is static. When types point to Vtables with the same structure but where pointers point to other implementation, you get polymorphism. It is recommended to encapsulate the calls logic in function...
read more
Classes are structures with a hidden pointer to a table of function pointers called VTable. The Vtable itself is static. When types point to Vtables with the same structure but where pointers point to other implementation, you get polymorphism. It is recommended to encapsulate the calls logic in function that take the struct as parameter to avoid code clutter. You should also encapsulate structures instantiation and initialization in functions (this is equivalent to a C++ constructor) and deletion (destructor in C++). These are good practice anyway. typedef struct { int (*SomeFunction)(TheClass* this, int i); void (*OtherFunction)(TheClass* this, char* c); } VTable; typedef struct { VTable* pVTable; int member; } TheClass; To call the function int CallSomeFunction(TheClass* this, int i) { (this->pVTable->SomeFunction)(this, i); } read less
Comments

Related Questions

Is C/C++ language is still important in 2019?
Of course, C language is only the heart of any processor or controller.Because of complexity in C peoples are looking to work on High-level languages like JAVA etc.
Tejas
0 0
7
What is a void main() in the C programming language?
Void main () is the entry point for execution in C program. The void is a keyword that represents function will not return anything but a void value. Main is the name of the function and () represents...
Ambar
0 0
5
Why are operating systems written in C? Why aren't other languages used?
C is commonly used for developing operating systems due to its low-level capabilities, efficiency, and direct access to hardware. Its minimalistic design allows for better control over system resources,...
Deepa
0 0
7
How do I check my C programming skills?
You should try to solve your daily life problems with c , i.e - build your own budget calculator or chess game and many more. Moreover make a habbit of solving 10 questions daily.
Ramnarayanan
0 0
5
Which book is best for learning C programming by a beginner?
C Programming Absolute Beginner's Guide Greg Perry, Dean Miller BeginnersLow-Level Programming: C, Assembly, and Program Execution Igor Zhirkov AdvancedC Programming in easy steps (5th Edition) Mike McGrath Beginners
Anil
0 0
8

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Bit wise operators in C
Bit Wise Operators Bit Wise operators are manipulates of individual bits with in a word of memory. The bit wise operators can be divided in to three general category. One’s Complement...

Working with C/C++ applications
Inorder to learn C and C++ programming languages one can work with various editors available.To name a few are the most popular one is turbo c++, DEV C++, Eclipse, NetBeans. Here are the screen shots...

Java and C trainer
Always think any conspect with real-time example like Object -- object must have states and behaviour then only we will call that thing is Object like fan is Object (rotating,color)

Swapping variable contents using C
/* WAP to swap the content of variables using C*/ //Header files #include<stdio.h>#include<conio.h> //Main function void main(){ //Variable declaration of type integer int a,b,c; //function...

Program to swap the value of two variables without using third variable(simplest way)...
int main() { int a=10,b=20; printf("values of a before swap ="%d,a); printf("values of b before swap ="%d,b); a=a+b; b=a-b; a=a-b; printf("******************"); printf("values of a after swap...

Recommended Articles

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more