Classes in C

Discussion in 'Programmer's Corner' started by learneros, Dec 21, 2007.

  1. learneros

    Thread Starter Member

    Nov 2, 2007
    Hi all,
    I am working in C language.In problem statement i m asked to make a class and then
    use this class methods later.I think there is nothin like classes in C,there is struct
    bt again struct dont have functions.Any idea how can i do it?

    Problem statement is :

    write the code to provide the synchronization to control access to a collection of pedestrian bridges. The bridges are used to cross a dangerous, shark-infested river.

    You have to write a class called Bridges. This class has the following methods that you can call.
    • void Bridges(int bridgeCount, int capacity): Calling this constructor creates an instance of the Bridges class with bridgeCount bridges across the river, each able to carry at most capacity persons crossing at one time. You will create exactly one instance of Bridges, probably in your main() method.
    • void runSimulation(int personCount): You will also call this method from your main() method. It runs a simulation by creating threads representing people trying to cross the river. New people are created at random intervals on random sides of the river. When all the people have been created, the simulation waits for them all to finish (i.e., it waits for the the threads to terminate) before printing (on System.out) some statistics about throughput, bridge occupancy, etc. and returning from the method runSimulation().
    • void cross(int bridgeNumber, int direction): This method causes a person thread to cross bridge bridgeNumber (a number in the range 0...bridgeCount-1). You don't know how long this takes to execute. You will call it from (described later). The direction parameter is Bridges.GOWEST for a person starting on the east shore traveling to the west shore, and Bridges.GOEAST for a person starting on the west shore traveling to the east shore. This method returns when the action is completed. Until it returns, the thread that called it is blocked. A thread can call this this method while another is blocked in it, subject to the rules below.
    Each person is a thread running a Person class, which you must write, that implements the following interface (see also the complete specification.)
    • public Person(Bridges river, int direction): This constructor is used by the Bridges object to create a new person. The river argument is a pointer to the Bridges object; it is included so that the person can call river.cross(bridge, direction) at the appropriate time.
    • public void run():. The run method should print a startup message, wait until it is safe to cross the river, call river.cross(bridge, direction), print another message, and return.
    Your Person class may also contain any additional methods that you need.
  2. Pootworm


    May 18, 2007
    Eh, no, no classes in C. You can fancify structs quite a bit (I think you could actually pull off everything you're required here with function pointers inside structs as i'm not seeing any inheritance-type specifications), but this sounds like a textbook problem from a intro C++ book.
  3. Dave

    Retired Moderator

    Nov 17, 2003
    This assignment sounds like a Java/C++ exercise to me. Either there is some confusion in the brief and it should be a Java/C++ exercise, else the person who wrote it doesn't know what they are talking about. The fact that it is for OO programming and the question talks about the C programming language I would be asking questions to/of the person who set this assignment.