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

How do I write code in C programming that will invert a string like "Welcome to programming" to “gnimmargorp ot emocleW”?
void main() { char msg = "Welcome to Programming"; char str; int i = 0, j = 0; clrscr(); while (msg != '\0') { if (msg != ' ') { str = msg; j++; ...
Mukul
How and where do I start learning C programming?
C language is the basic of the programming to learn the basic of programming we learn the c language
Gaurav
0 0
7
What is = and == in C language?
= is assignment operator while ==is comparision operator
Sayed
0 0
7
What are the control statements in C language?
The if statement, for loop, while loop, switch statement, break statement, and continue statement are C's most widely used control statements.
Jitender
0 0
6
Why is C still so popular?
C remains popular for several reasons, and its enduring popularity can be attributed to a combination of historical significance, efficiency, and versatility. Here are some key reasons why C is still widely...
A
0 0
6

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

Ask a Question

Related Lessons

All About Programming And A Good Programmer.
Hi, This is my first lesson for you guys. Hope you enjoy reading it. In recent community questions, I found many people wanted to be good programmers, or wanted to have good hands on certain language,...

C Program Sample Application
//Standard Library Functions(Header Files used) #include<stdio.h> #include <conio.h> //Main method int main() { // function for clearing screen clrscr(); // function to print the output...
S

How to write shared library program using C Programming?
Library Name : hellofilename : hello.c#include void Hello(){ printf("Hello World .. How are you 1\n");}======================================Header File:hello.h#ifndef __HELLO_H#define __HELLO_Hvoid Hello(void);#endif===============================Application...

Why do pointers have a datatype?
Before we start with pointers you must know what is a variable and a datatype. int a; This is the basic line in every program in 'C' . It means that we are asking the compiler to give us 2 bytes of space...

Find out the Output of the following with reason and get C Language Training fess less by 10%
1. void main() { clrscr(); printf(5+"Beautifull"); getch(); } 2. void main() { int a=50; clrscr(); ...

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