Make your own free website on Tripod.com
 
CHAPTER 6 : INHERITANCE
 
Base-class dan Derived-class  
Base class Derived class
Student GraduateStudent, UndergraduateStudent
Shape Circle 
Triangle 
Rectangle
Loan CarLoan 
HomeImprovementLoan 
MortgageLoan
Employee FacultyMember 
StaffMember
Account CheckingAccount 
SavingsAccount
  Base-class dan derived-class constructor with arguments             derived-constructor(senarai-argumen) : base(senarai-argumen)
            {
                ................
            }
 

Constructor dan Destructor dengan Argumen

            derived-constructor (arg-list) : base1(arg-list), base2(arg-list),....,baseN(arg-list)
            {
                ........     //body of derived's class constructor
            }             #include <iostream.h>
            class base{
            public:
                base() { cout << "Constructing base class\n: ";}
                ~base() { cout << "Destructing base class\n: ";}
            };

            class derived : public base {
            public:
                derived() { cout << "Constructing derived class\n";}
                ~derived() { cout << "Destructing derived class\n: ";}
            };

            main() {
                derived o;
                return 0;
            }
 

            // menunjukkan bagaimana argumen dihantar kepada constructor derived class

            #include <iostream.h>
            class base{
            public:
                base() { cout << "Constructing base class\n: ";}
                ~base() { cout << "Destructing base class\n: ";}
            };

            class derived : public base {
            public:
                derived(int n) {
                    cout << "Constructing derived class\n";
                    j=n;
                }
                ~derived() { cout << "Destructing derived class\n: ";}
                void showj { cout << j << endl;}
            };

            main() {
                derived o(10);
                o.showj();
                return 0;
            }

            // kedua-dua constructor base dan derived class mempunyai argumen yang sama

            #include <iostream.h>
            class base{
                int i;
            public:
                base(int n) {
                    cout << "Constructing base class\n: ";
                    i = n;
                }
                ~base() { cout << "Destructing base class\n: ";}
                void showi() { cout << i << endl;}
            };

            class derived : public base {
                int j;
            public:
                derived(int n) : base(n) {            // pass arg to base class
                    cout << "Constructing derived class\n";
                    j=n;
                }
                ~derived() { cout << "Destructing derived class\n: ";}
                void showj { cout << j << endl;}
            };

            main() {
                derived o(10);
                o.showi();
                o.showj();
                return 0;
            }

            // contoh constructor base dan derived class yang tidak sama dari segi argumen

            #include <iostream.h>
            class base{
                int i;
            public:
                base(int n) {
                    cout << "Constructing base class\n: ";
                    i = n;
                }
                ~base() { cout << "Destructing base class\n: ";}
                void showi() { cout << i << endl;}
            };

            class derived : public base {
                int j;
            public:
                derived(int n, int m) : base(n) {            // pass arg to base class
                    cout << "Constructing derived class\n";
                    j=n;
                }
                ~derived() { cout << "Destructing derived class\n: ";}
                void showj { cout << j << endl;}
            };

            main() {
                derived o(10,20);
                o.showi();
                o.showj();
                return 0;
            }

Multiple Inheritance

            class base1 {
            public:
                base1();
                void show() { cout << "\nbase1";}
            };

            class base2{
            public:
                base2();
                void show() { cout << "\nbase2";}
            };

            class derived : public base1, public base2{
            public:
                derived(): base1(), base2();
                show() { cout << " \nderive "; }
            };

            class derived-class-name : access base-class-name
            {
                .......
            }

            #include <iostream.h>
            class base {
                int x;
            public:
                void setx (int n) { x = n ;}
                void showx() { cout << x << endl; }
            }

            //inherit as public
            class derived : public base {
                int y;
            public :
                void sety (int n) { y=n; }
                void showy() { cout << y << endl; }
            };

            main() {
                derived ab;
                ab.setx(10);     // access member of base class
                ab.sety(20);     // access member of derived class
 
                ab.showx();     // access member of base class
                ab.showy();     // access member of derived class

                return 0;
            }

Using Protected Member
                  class class-name{
                    //private members
                protected:    //optional
                    //protected members
                public:
                    //public members
                };             #include <iostream.h>
            class samp{
                int a;            //private by default
            protected:        //still private relative to samp
                int b;
            public:
                int c;
 
                samp(int n, int n) {a=n; b=m;}
                int geta() {return a;}
                int getb() {return b;}
            };

            main() {
                samp ob(10,20);
                //ob.b =99;           Error! b is protected & thus private
                ob.c = 30;            //ok, C is public
                cout << ob.geta()<<' ';
                cout << ob.getb() << ob.c << '\n';
            }
 

            class base {
            protected:        //private to base
                int a,b;         //but still accessible by derived
            public:
                void setab(int n, int m) { a=n; b=m;}
            };

            class derived:protected base{
                int c;
            public:
                void setc(int n) {c=n;}
                //this function has access to a and b from base
                void showabc(){
                    cout << a << ' ' << b << ' ' << c << '\n';
                }
            };

            main(){
                derived ob;
                //error : setab() is now a protected member of base
                ob.setab(1,2);    // setab() is not accessible here
                ob.setc(3);
                ob.showabc();
                return 0;
            }