473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Abstraction in C

I'm developing a project which I'm trying to keep as organized as
possible. Since C doesn't support classes, I've been trying to make do
mostly with structs and functions that operate on them. However,
sometimes some of the "higher-level" modules contain functions that
map more or less directly to functions calls in "lower-level" modules.
So I've been having trouble naming these such functions, since ideally
both of them (the one in the higher-level module and the one in the
lower-level module) should have the same names. So, are there any
naming conventions for these cases? Or are there any alternatives for
doing what I'm doing?

Thanks,
Sebastian

Jul 18 '08 #1
4 1393
On Jul 18, 1:07*am, s0s...@gmail.com wrote:
I'm developing a project which I'm trying to keep as organized as
possible. Since C doesn't support classes, I've been trying to make do
mostly with structs and functions that operate on them. However,
sometimes some of the "higher-level" modules contain functions that
map more or less directly to functions calls in "lower-level" modules.
So I've been having trouble naming these such functions, since ideally
both of them (the one in the higher-level module and the one in the
lower-level module) should have the same names. So, are there any
naming conventions for these cases? Or are there any alternatives for
doing what I'm doing?
Add a short (one or two letter) prefix to all functions in the module.
This has the advantage of making it obvious where to find the code in
a multi-file project.

For exzmple, in Unix, the X11 library functions all start with "X",
the toolkit functions all start with "Xt", and Motif functions start
with "Xm".
--
Fred
Jul 18 '08 #2
<s0****@gmail.comwrote in message news:
>
I'm developing a project which I'm trying to keep as organized as
possible. Since C doesn't support classes, I've been trying to make do
mostly with structs and functions that operate on them. However,
sometimes some of the "higher-level" modules contain functions that
map more or less directly to functions calls in "lower-level" modules.
So I've been having trouble naming these such functions, since ideally
both of them (the one in the higher-level module and the one in the
lower-level module) should have the same names. So, are there any
naming conventions for these cases? Or are there any alternatives for
doing what I'm doing?
If you have, say, EMPLOYEE and CUSTOMER structures in your program, it is
quite likely that both will have getname() functions associated with them.

So use emp_getname() and cust_getname(). It's not ideal. In fact if you use
lcc by Jacob Navia, who posts here, you can get C++ style function
overloading. The problem with the lcc route is that the code will no longer
compile under another C compiler.
I don't think it is my job to either recommend or warn against lcc, but to
point out the issues, then you can make your choice.

Another issue you might consider is that if we have

char *emp_getname(EMPLOYEE *emp)
and
char *cust_getname(CUSTOMER *cust)

then we can't have a fucntion pointer to both

char (*getname)(void *obj)

because EMPLOYEE and CUST *s aren't necessarily the same size as void *s.
However you can write emp_getname() and cust_getname() to take void *s
instead of the real structure type, and convert the parameter in the first
line. Then you can do fancy mapping and polymorphism.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 19 '08 #3
On Jul 19, 11:18*am, "Malcolm McLean" <regniz...@btinternet.com>
wrote:
However you can write emp_getname() and cust_getname() to take void *s
instead of the real structure type, and convert the parameter in the first
line. Then you can do fancy mapping and polymorphism.
Just be aware that C won't give you the benefits of type checking and
type safety. The compiler or C lang. won't stop you from passing you
an object of Employee to cust_getname(this may not fail as both the
structures may have a member named name). You will have to introduec
type checking yourself(that is, if you really need it).
Jul 19 '08 #4
s0****@gmail.com writes:
I'm developing a project which I'm trying to keep as organized as
possible. Since C doesn't support classes, I've been trying to make do
mostly with structs and functions that operate on them. However,
sometimes some of the "higher-level" modules contain functions that
map more or less directly to functions calls in "lower-level" modules.
So I've been having trouble naming these such functions, since ideally
both of them (the one in the higher-level module and the one in the
lower-level module) should have the same names. So, are there any
naming conventions for these cases? Or are there any alternatives for
doing what I'm doing?
It does not support classes but structs so you can use them

just assume the following structs
struct Employee {
struct EmployeeFtable *funTable;
struct EmployeeData *dataTable;
};

struct EmployeeFtable {
int (*get_id)(struct Employee *);
int (*set_id)(struct Employee *, int nId);
}

struct EmployeeData {
int id;
char *first_name
char *last_name;
};
struct OthterStruct {
struct OtherStruct *funTable;
...

struct OtherStructFtable {
int (*get_id)(struct ....
};
You can use as many struct with get_id as you can...

Howerver you can use the same approach as glib-2 and will probably
gain quit a lot of it.

Regards
Friedrich

--
Please remove just-for-news- to reply via e-mail.
Jul 21 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: ambika | last post by:
Hello, I have a very basic doubt. Why is C called a structured programming language??why structured? C++ is called a Object Oriented language 'cos it obeys the OOP's concepts..Why is C called a...
6
by: Mark Broadbent | last post by:
this might sound like an obvious question but I have found that usually these two evolve at the same time. One of the biggest reasons for creating the abstraction in the first place (in my...
1
by: rickycornell | last post by:
Greetings All, On past projects in PHP4 I had always just written my own libraries to deal with database interaction. Somehow I was operating in the dark that there were all these database...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
8
by: Ivan S | last post by:
What are your recommendations for lightweight database abstraction library (Oracle/MySQL)? I prefer OOP. :) Tnx, Ivan.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.