Cooler_

cpp stu

Mar 4th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. /*
  2.  
  3. My notes in last odyssey how freelancer
  4.  
  5.  last work was nes to fix a code with
  6.  WxWidgets, so i lost time to fix that
  7.  code, z dont h exp in "C++",b i was haz
  8.  to read Stroustrup's book,i ob quite stu
  9.  to pru to ry, i vot to write this dok.
  10.  
  11.  So i ub to write code in ansi C
  12.  b in last times, i played quite with
  13.  C++ , so this code is demo of su thing
  14.  that i ob about cpp.
  15.  
  16.  
  17. author:Antonio aka "Cooler_"
  18. e-mail=c00f3r[at]gmail[dot]com
  19.  
  20. -
  21.  C like functions same to "stdio.h",
  22.  so u can declare "stdio.h" too,b with
  23.  out of cpp standard...
  24. */
  25. #include <cstdio>
  26.  
  27. using namespace std;
  28.  
  29. class Animal {
  30. // simple heritage ,default is "private:" so private not use heritage
  31.  public:
  32. // virtual modifier defines polymorphic behavior of function members
  33.   virtual void food() = 0;
  34.   virtual void sound() = 0;
  35. };
  36.  
  37. class Dog : public Animal {
  38.  public:
  39.   void food() { std::puts("Pedigree"); }
  40.   void sound() { std::puts("wof!"); }
  41. };
  42.  
  43. class Cat : public Animal {
  44.  public:
  45.   void food() { std::puts("Purina"); }
  46.   void sound() { std::puts("meow!"); }
  47. };
  48.  
  49.  
  50. // templates,with this u can use vars without declare each type of var,however quite util to reuse code
  51. template <class X>
  52.  void swap(X &a, X &b)    // tempalte function
  53.  {
  54.   a^=b;
  55.   b^=a;
  56.   a^=b;
  57.  }
  58.  
  59.  
  60. // tru this change dest 2 src
  61. void transmute(void *dest, void *src) {
  62.   *(void **)dest = *(void **)src;
  63. }
  64.  
  65. // Number of elements in an array
  66. #define sizeof_array(a) (sizeof a / sizeof a[0])
  67.  
  68. int main()
  69. {
  70. // first trick array size
  71.  char word[5]="bobo";
  72.  std::printf("array word size: %d\n", sizeof_array(word));
  73.  
  74. // second step swap with template
  75.  int x=2,y=8;
  76.  swap(x,y);
  77.  std::printf("%d\n",x);
  78.  
  79. // third step , the simple transmute
  80.   Dog *dog = new Dog();
  81.   Cat *cat = new Cat();
  82.   dog->food();
  83. // function to change dog with cat
  84.   transmute(dog,cat);
  85.   dog->food();
  86.  
  87. /*
  88.  gag caution!
  89.   az u need use "new", this use heap,
  90.   then u nes us "delete", to free memory.
  91.   if not use cause memory leak.
  92. */
  93.   delete dog,cat;
  94.  
  95.   return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment