Lecture 3 - Inheritance - Code Examples

Code Examples

Code from slides 3-4

Demo of public, private, protected

#include <iostream>
using namespace std;
 
class Parent {
    private:
        int three;
 
    protected:
        int two;
 
    public:
        int one;
 
        Parent() { one = two = three = 42; }
 
        void inParent() {
            cout << one << two << three << endl;
        }
};
 
class Child : public Parent {
    public:
        void inChild() {
            cout << one << endl;
            cout << two << endl;
            cout << three << endl; //not legal here
        }
};
 
int main() {
    Child c;
    cout << c.one << endl;
    //cout << c.two << endl; //not legal here
    //cout << c.three << endl; //not legal here
}

Slide 5 code

access private in subclass

#include <iostream>
using namespace std;
 
class A {
    public: 
        int getAVal() { return a; }
        int getBVal() { return b; }
 
    protected:
        int b;
 
    private:
        int a;
};
 
class B: public A {
    public: 
        int doStuff() {
            cout << getAVal() << endl;
            cout << getBVal() << endl;
            b = 2;
            a = 3; //not legal here
        }    
};
 
int main() {
    B b;
    b.doStuff();
    cout << b.getBVal() << endl;
}

Slides 6-7 Base and derived classes example - overriding using virtual

#include <iostream>
using namespace std;
 
class Base {
 public: 
     virtual void print() {
        cout << "Base class" << endl;
     }
};
 
class Derived : public Base{
 public: 
     virtual void print() {
        cout << "Derived class" << endl;
     }
};
 
void func(Base &b) { //using polymorphic argument
    b.print(); //can call Base.print() and also Derived.print() 
}
 
int main() {
    Base b;
    Derived d;
 
    func(b);
    func(d);    
}

Slide 17 Overriding methods using virtual and using polymorphic code

#include <iostream>
using namespace std;
 
class Base {
 public: 
    virtual void print() {
        cout << "Base class" << endl;
     }
};
 
class Derived : public Base{
 public: 
    virtual void print() {
        cout << "Derived class" << endl;
     }
};
 
int main() {
    Base b;
    Derived d;
 
    b.print();
    d.print();
 
    Base *b1 = new Derived; //polymorphic variable
    b1->print();
}

Slide 18 - Abstract class example using pure virtual function

#include <iostream>
using namespace std;
 
class Base {
 public: 
    virtual void print() = 0; //pure virtual function
};
 
class Derived : public Base{
 public: 
    virtual void print() {
        cout << "Derived class" << endl;
     }
};
 
int main() {
    Base b; //comment this line - Base cannot be instantiated because its abstract
    Derived d;
 
    b.print(); //comment this line
    d.print();
 
    Base *b1 = new Derived; //correct way to use -polymorphic variable
    b1->print();
}

Slide 25 - corrected code using composition not inheritance

For construction use composition not inheritance - compare with original version for on slide 25

public class Stack {
    private Vector elements = new Vector(); //vector is inside stack
 
    public Stack() {}
 
    public Object push(Object item) {            
     elements.addElement(item);    //vector method called            
     return item;
    }
 
    public synchronized Object pop() {
          Object obj;
      int len = elements.size(); //vector method called
      obj = peek();
      elements.removeElementAt(len - 1); //vector method called
      return obj;      
    }
}

Slide 33 - How constructors are called when using inheritance?

Comment different lines in main() and see the output

#include <iostream>
using namespace std;
 
class A {
    public: 
        A() { cout << "A() default constructor " << endl; };
        A(int num) {cout << "A(num) constructor " << endl; }
};
 
class B : public A {
    public: 
        B() { cout << "B() default constructor " << endl;};
 
        B(int num) : A(num) {
            cout << "B(num) constructor" << endl; }
 
        B(char ch) { cout << "B(ch) constructor" << endl; }
};
 
int main() {
    B b1;
 
    B b2(7);
 
    B b3('a');
}

Slide 35 - How destructors are called when using inheritance?

#include <iostream>
using namespace std;
 
class Parent {
    public: 
        virtual ~Parent() { cout << "deleting parent ..." << endl; }
        virtual void print() { cout << " parent class" << endl; }
 
};
 
class Child: public Parent {
    public: 
        ~Child() { cout << "deleting child ..." << endl; }
        void print() { cout << " child class" << endl;
        }
};
 
int main() {
    Parent *p = new Child();
    p->print();
    delete p;
 
    //Child c1; //auto
    //c1.print(); //does not need delete for c1 object
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License