Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- My notes in last odyssey how freelancer
- last work was nes to fix a code with
- WxWidgets, so i lost time to fix that
- code, z dont h exp in "C++",b i was haz
- to read Stroustrup's book,i ob quite stu
- to pru to ry, i vot to write this dok.
- So i ub to write code in ansi C
- b in last times, i played quite with
- C++ , so this code is demo of su thing
- that i ob about cpp.
- author:Antonio aka "Cooler_"
- e-mail=c00f3r[at]gmail[dot]com
- -
- C like functions same to "stdio.h",
- so u can declare "stdio.h" too,b with
- out of cpp standard...
- */
- #include <cstdio>
- using namespace std;
- class Animal {
- // simple heritage ,default is "private:" so private not use heritage
- public:
- // virtual modifier defines polymorphic behavior of function members
- virtual void food() = 0;
- virtual void sound() = 0;
- };
- class Dog : public Animal {
- public:
- void food() { std::puts("Pedigree"); }
- void sound() { std::puts("wof!"); }
- };
- class Cat : public Animal {
- public:
- void food() { std::puts("Purina"); }
- void sound() { std::puts("meow!"); }
- };
- // templates,with this u can use vars without declare each type of var,however quite util to reuse code
- template <class X>
- void swap(X &a, X &b) // tempalte function
- {
- a^=b;
- b^=a;
- a^=b;
- }
- // tru this change dest 2 src
- void transmute(void *dest, void *src) {
- *(void **)dest = *(void **)src;
- }
- // Number of elements in an array
- #define sizeof_array(a) (sizeof a / sizeof a[0])
- int main()
- {
- // first trick array size
- char word[5]="bobo";
- std::printf("array word size: %d\n", sizeof_array(word));
- // second step swap with template
- int x=2,y=8;
- swap(x,y);
- std::printf("%d\n",x);
- // third step , the simple transmute
- Dog *dog = new Dog();
- Cat *cat = new Cat();
- dog->food();
- // function to change dog with cat
- transmute(dog,cat);
- dog->food();
- /*
- gag caution!
- az u need use "new", this use heap,
- then u nes us "delete", to free memory.
- if not use cause memory leak.
- */
- delete dog,cat;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment