Got something to write about? Check out our Article Builder.
*/
*/

View \#5_VER2.CPP

Some Introductory C++ Code Examples

Submitted By: WEBMASTER
Rating: starhalf star (Rate It)


#include <stdio.h>

class animal {
     public:
          virtual void identify() { printf("%s\n", "animal"); }
};

class mammal : public animal {
     public:
          void identify() { printf("%s\n", "mammal"); }
};

class bird : public animal {
     public:
          void identify() { printf("%s\n", "bird"); }
};

class dog : public mammal {
     public:
          void identify() { printf("%s\n", "dog"); }
};

class eagle : public bird {
     public:
          void identify() { printf("%s\n", "eagle"); }
};

void print1(void), print2(void), print3(void);

main()
{
     print1();
     print2();
     print3();
}

void print1()
{
     animal *ap = new animal;    ap->identify();
     mammal *mp = new mammal;    mp->identify();
     bird   *bp = new bird;      bp->identify();
     dog    *dp = new dog;       dp->identify();
     eagle  *ep = new eagle;     ep->identify();

     printf("\n");
}

void print2()
{
     animal *ap = new animal;
     ap->identify();                   

     ((mammal *) ap)->identify();         
     mammal *mp = (mammal *) ap;
     mp->identify();                   

     printf("\n");
}

void print3()
{
     animal a, *ap = &a;
     mammal m;
     eagle  e;

     ap->identify();
     
     ap = &m;
     ap->identify();

     printf("\n");
}

/* output:

animal
mammal
bird
dog
eagle

animal
animal
animal

animal
mammal

*/

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings