CARI Infonet

 Forgot password?
 Register

ADVERTISEMENT

View: 2054|Reply: 1

C++ Class Problem

[Copy link]
Post time 15-6-2008 11:21 AM | Show all posts |Read mode
  1. #include<iostream.h>
  2. #include<windows.h>

  3. void Colour(unsigned short);

  4. class traffic
  5. {
  6.     public:
  7.     void lightA();
  8.     void lightB();
  9.     void lightC();

  10. };

  11. void traffic::lightA()
  12. {
  13.     Colour(10);
  14.     cout<<"O"<<endl;
  15.     Colour (7);
  16.     cout<<"O"<<endl;
  17.     Colour (7);
  18.     cout<<"O"<<endl;
  19.     return;
  20. }
  21. void traffic::lightB()
  22. {
  23.     Colour(7);
  24.     cout<<"O"<<endl;
  25.     Colour (7);
  26.     cout<<"O"<<endl;
  27.     Colour (12);
  28.     cout<<"O"<<endl;
  29.     return;
  30. }
  31. void traffic::lightC()
  32. {
  33.     Colour(7);
  34.     cout<<"O"<<endl;
  35.     Colour (7);
  36.     cout<<"O"<<endl;
  37.     Colour (12);
  38.     cout<<"O"<<endl;
  39.     return;
  40. }

  41. int main()
  42. {
  43.     traffic l;
  44.     Colour(15);
  45.     cout<<"Road A"<<endl;
  46.     l.lightA();
  47.     Colour(15);
  48.     cout<<"Road B"<<endl;
  49.     l.lightB();
  50.     Colour(15);
  51.     cout<<"Road C"<<endl;
  52.     l.lightC();
  53.     return 0;
  54. }
  55. void Colour (unsigned short colour)
  56. {

  57.     HANDLE hcon = GetStdHandle (STD_OUTPUT_HANDLE);
  58.     SetConsoleTextAttribute(hcon,colour);
  59. }
Copy the Code


Sy nak buat simple traffic light simulation.masalahnya skrg sy xtahu macam mana control lampu tu ikut selang masa.


Contoh:

Road A:green Road B:red Road C:red
after 45 second
Road A:yellow Road b:red Road C:red
after 10 second
Road A:red Road B:green Road C:red
dan seterusnya...


Memandangkan skrg sy tgh belajar class & inheritance.So nak aplikasikan dalam coding nie.Sifu2 sekalian tolong beri tunjuk ajar
Reply

Use magic Report


ADVERTISEMENT


Post time 18-6-2008 04:54 PM | Show all posts
nah ni ada cth sample program yg guna timing.
cuba la manipulate n combinekan dgn program kat atas tu.


  1. /* clock example: countdown */
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include <time.h>

  5. void wait ( int seconds )
  6. {
  7.   clock_t endwait;
  8.   endwait = clock () + seconds * CLOCKS_PER_SEC ;
  9.   while (clock() < endwait) {}
  10. }

  11. int main ()
  12. {
  13.   int n;
  14.   cout<<"Starting countdown"<<endl;
  15.   for (n=10; n>0; n--)
  16.   {
  17.     cout<<n<<endl;
  18.     wait (1);
  19.   }
  20.   cout<<"FIRE!!!";
  21.   return 0;
  22. }
Copy the Code


notes.
lampu kuning guna no 14.
utk yg line void Colour (unsigned short colour) tu, knp kena guna data type unsigned short sedangkan nilai yg dihantar berupa interger iaitu 7 dan 12.sy rs lebih profesional kalo unsigned short ditukar kpd int sahaja.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT



 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

13-5-2024 06:02 AM GMT+8 , Processed in 0.286602 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list