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

What is the error in the given C language program?
There are three types of errors exist in C Programming: Syntax errors, Semantic errors and runtime errors.
Suresh
0 0
5
How do I learn C++ from the basics?
First you learn C LANGUAGE
Priyanka
0 0
6
Why are C and C++ faster than other programming languages?
C is basic programming language. It is very simple language & interested. C is procedural programming language
Kalyani
0 0
8
C machine independent Programming Language? C is portable . Means What?
It can work on various hardware configuration . C doesnt require mandatory or same set of hardware to run program. A program can written on one comp. And can run on other comp.(machine)
Shiva
Why is a compiler used for language C and not an interpreter?
C is a structured programming language. It’s code consists of many macros, function calls references etc., which is required to be decoded before program’s execution can take place, so that any error is...
Savitha
0 0
5

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

Ask a Question

Related Lessons

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 0
0

Difference Between C Language and C Program
C Language: C Language is structured, high level and machine independent language. C Program: C Program is the collection of functions that are supported by C library.
S

Shashwat Kumar

0 0
0

C Program-Temperature [conversion from farenheit to degree celsius]
//WAP to convert temperture from farenheit into degree celsius //Header files#include<stdio.h>#include<conio.h> //Main functrion void main(){ //Variable declaration of type float float...

Variables
Variables in C Language:A variable is a name that may be used to store a data value. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer...

What Are IT Industries Performance Metrics?
1. Outstanding Expectation: Eligible to get Promotion easily and good salary hike. Always preferrable to go abroad. 2. Exceed Expectation: Can get Promotion as per schedule of company with good salary...

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