Make your own free website on Tripod.com
 
CHAPTER 7 : OVERLOADED, POLYMORPHISM, COMPOSITION
FUNCTION OVERLOADING             //using overloading function
            #include <iostream.h>
            int square (int x){return x*x;}
            double square (double y) { return y*y;}
            int main(){
                cout << "The square of integer 7 is " << square(7)
                        << "\nThe square of double 7.5 is " << square(7.5)
                        << endl;
                return 0;
            }
              #include <iostream.h>
            void show(int val){
                cout << " integer " << val << endl;
            }

            void show (double val){
                cout << " double " << val << endl;
            }

            void show (char *val) {
                cout << " string " << val << endl;
            }

            int main(){
                show(12);
                show(3.1415);
                show(" Hello world");
                return 0;
            }
 

CONSTRUCTOR OVERLOADING

            #include <iostream.h>
            class myclass {
                int x;
            public:
                //overload constructor two ways
                myclass() {x=0;}        // no initializer
                myclass(int n) { x= n;}    // initializer
                int getx() { return x;}
            };
 
            main(){
                myclass o1(10);        // declare with initial value
                myclass o2;               // declare without initializer
                cout << "o1: " << o1.getx() << endl;
                cout << "o2: " << o2.getx() << endl;
                return 0;
            }
              #include <iostream.h>
            class myclass{
                int x;
            public:
                //overload constructor two ways
                myclass() {x=0;}        // no initializer
                myclass(int n) { x= n;}    // initializer
                int getx() { return x;}
            };
 
            main(){
                myclass o1[0];        // declare array withot initializers
                myclass o2[10]={1,2,3,4,5,6,7,8,9,10};               // declare without initializer
                int i;
                for (i=0; i<10; i++){
                    cout << "o1[ " << i<<"] : "<< o1[i].getx() << endl;
                    cout << "o2[ " << i<<"] : "<< o2[i].getx() << endl;
                }
                return 0;
            }

COPY CONSTRUCTOR

            #include <string.h>
            class TAnyClass {
            private:
                int i; double r; char *s;
            public:
                TAnyClass() {
                    i=0; r=0; s=NULL;
                TAnyClass(int ii, double rr, const char *ss){
                    i=ii; r=rr; s=strdup(ss);
                }
                ~TAnyClass() {delete s;}
                const char *GetStar(void) { return s;}
                TAnyClass(TAnyClass &copy);                //copy constructor
            };

            TAnyClass::TAnyClass(TAnyClass &copy){
                cout << "\ninside TAnyClass's copy constructor\n";
                i = copy.i;
                r = copy.r;
                if (copy.s)
                    s = strdup(copy.s)
                else
                    s = NULL;
            }
 
            main(){
                TAnyClass v1;
                TAnyClass v2(1,2, " a test string ");
                TAnyClass v3 = v2;
            }
 

POLYMORPHISM

VIRTUAL FUNCTION             #include <iostream.h>
            class base{
            public:
                int i;
                base(int x) {i = x;}
                virtual void func(){
                    cout << " using base version of func():";
                    cout << i << endl;
                }
            };

            class derived1 : public base{
            public:
                derived(int x) : base(x) {}
                void func(){
                    cout << " using derived1's version of func() : ";
                    cout << i*i << endl;
                }
            };

            class derived2 : public base {
            public:
                derived2(int x) : base (x) {}
                void func(){
                    cout << "using derived2's version of func():";
                    cout << i + i << endl;
                }
            };

            main() {
                base *p;
                base ob(10);
                derived1 d_ob1(10);
                derived2 d_ob2(10);
                p = &ob;
                p->func();     //use base's func()
                p = &d_ob1;
                p->func();        //use derived1's func()
                p = &d_ob2;
                p->func();        //use derived2's func()
                return 0;
            }
 

            #include <iostream.h>
            class base{
            public:
                int i;
                base (int x) { i =x; }
                virtual void func(){
                    cout << " using base version of func():";
                    cout << i << endl;
                }
            };

            class derived1 : public base{
            public:
                derived1(int x) : base(x){}
                void func(){
                    cout << "using derived1's version of func():";
                    cout << i * i << endl;
                }
            };

            class derived2 : public base{
            public:
                derived2(int x) : base(x) {}
                //derived2 does not override func()
            };

             main() {
                base *p;
                base ob(10);
                derived1 d_ob1(10);
                derived2 d_ob2(10);
                p = &ob;
                p->func();     //use base's func()
                p = &d_ob1;
                p->func();        //use derived1's func()
                p = &d_ob2;
                p->func();        //use derived2's func()
                return 0;
            }

            #include <iostream.h>
            class TValue {
            protected:
                int value;
            public:
                TValue(int n) {value = n;}
                virtual int GetValue(void) { return value;}
            };
 
            class TMult : public TValue{
            protected:
                int multiplier;
            public:
                TMult(int n, int m) : TValue(n) { multiplier = m;}
                virtual int GetValue(void) {return value * multiplier;}
            };

            main(){
                TValue *basep;
                basep = new TValue (10);
                cout << basep ->GetValue() << endl;
                delete basep;
                basep = new TMult(10,2);
                cout << basep->GetValue() << endl;
                delete basep;
                return 0;
            }

PURE VIRTUAL FUNCTION

            class AbstractClass {
            public:
                virtual void f1(void);                // normal virtual function
                virtual void f2(void) = 0;          // pure virtual function
                ..............
            }; COMPOSITION
 

            #include <iostream.h>

            class Date{
            int month, day, year;
            public:
                 Date(int,int,int);
                 void print();
            };

            Date::Date(int m,int d,int y){
             month=m;
             day=d;
             year=y;
             cout<<"Date object constructor:"<<endl;
            }

            void Date::print(){
             cout << month <<  day<< year;
            }

            class child{
            private:
                 char *name; Date birthday; Date regdate;
            public:
                 child(char*, int, int,int,int,int,int);
                 void print(void);
            };

            child:: child(char *nama,int bm, int bd, int by, int rm, int rd, int ry)
                     :birthday(bm,bd,by), regdate(rm,rd,ry){
                 name=nama;
                 cout<<"child object constructor:"<< name;
            }

            void child:: print(){
                 cout<<"\nchild name:"<< name;
                 cout<<"\nBirthdate:";birthday.print();
                 cout<<"\nRegister date for school:"; regdate.print();
            }

            main(){
                 child a ("azman",10,11,92,1,1,99);
                 a.print();
                 return 0;
            }

Anda dikehendaki sentiasa menyemak latihan lab anda dari masa ke semasa