Find us On Facebook

Showing posts with label Virtual function. Show all posts
Showing posts with label Virtual function. Show all posts

Wednesday, April 6, 2011

W.A.P to use of virtual function


#include<iostream.h>
#include<conio.h>
            class cpolygon {
                        protected:
                                    int l,h;
                        public:
                                    void setval(int a, int b){
                                                l=a;
                                                h=b;
                                    }
                                    virtual int area(void) {
                                                return 0;
                                    }
            };
            class crectangle : public cpolygon {
                        public:
                                    int area(void) {
                                                return(l*h);
                                    }
            };
            class ctriangle : public cpolygon {
                        public:
                                    int area(void) {
                                     return((l*h)/2);
                                     }
            };
void main() {
            clrscr();
            crectangle crect;
            ctriangle tangle;
            cpolygon cpoly;
            cpolygon *poly1 = &crect;
            cpolygon *poly2 = &tangle;
            cpolygon *poly3 = &cpoly;
            poly1->setval(4,5);
            poly2->setval(4,5);
            poly3->setval(4,5);
            cout<<"Area : = "<<poly1->area();
            cout<<"\nArea : = "<<poly2->area();
            cout<<"\nArea : = "<<poly3->area();
            getch();
}



OUTPUT:-


Area : = 20
Area : = 10
Area : = 0

Programms

C,C++,VB, PL/SQL EBOOK SEARCH ENGINE