Spring 2013 - PIC 10C: Advanced Programming

Humberto Naves

Email: hnavesatmath.ucla.edu
Office Location: MS 6160
Office hours: Class homepage: PIC 10C
My webpage: Humberto


  1. Example 1
    #include <iostream>
    
    using namespace std;
    
    class B
    {
    public:
        B();
        B(int n);
    };
    
    B::B()
    {
        cout << "B::B()\n";
    }
    
    B::B(int n)
    {
        cout << "B::B(" << n << ")\n";
    }
    
    class D : public B
    {
    public:
        D();
        D(int n);
    private:
        B b;
    };
    
    D::D()
    {
        cout << "D::D()\n";
    }
    
    D::D(int n) : B(n)
    {
        b = B(-n);
        cout << "D::D(" << n << ")\n";
    }
    
    int main()
    {
        D d(3);
        return 0;
    }
        
  2. Example 2
    #include <iostream>
    
    using namespace std;
    
    class B
    {
    public:
        B();
        virtual void p() const;
        void q() const;
    };
    
    B::B()
    {
    }
    
    void B::p() const
    {
        cout << "B::p()\n";
    }
    
    void B::q() const
    {
        cout << "B::q()\n";
    }
    
    class D : public B
    {
    public:
        D();
        virtual void p() const;
        void q() const;
    };
    
    
    D::D()
    {
    }
    
    void D::p() const
    {
        cout << "D::p()\n";
    }
    
    void D::q() const
    {
        cout << "D::q()\n";
    }
    
    int main()
    {
        B b;
        D d;
        B* pb = new B();
        B* pd = new D();
        D* pd2 = new D();
    
        b.p(); b.q();
        d.p(); d.q();
        pb->p(); pb->q();
        pd->p(); pd->q();
        pd2->p(); pd2->q();
        return 0;
    }
        
  3. Example 3
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Person
    {
    public:
        Person(); // Default Constructor
        Person(string name, int age); // Constructor with parameters
        Person(const Person &other); // Copy Constructor
        virtual ~Person(); // Destructor
    
        Person& operator= (const Person &other); // Copy assignment operator
    private:
        string name;
        int age;
    };
    
    Person::Person()
    {
        name = "John Doe";
        age = 41;
        cout << "Person::Person() [Who am I?]\n";
    }
    
    Person::Person(string name, int age) : name(name), age(age)
    {
        cout << "Person::Person(" << name << ", " << age << ")\n";
    }
    
    Person::Person(const Person &other) : name(other.name), age(other.age)
    {
        cout << "Person::Person(" << name << ", " << age << ") [Copied from someone else]\n";
    }
    
    Person::~Person()
    {
        cout << name << " just passed away\n";
    }
    
    Person& Person::operator=(const Person &other)
    {
        this->name = other.name;
        this->age = other.age;
        cout << "Copying from Person(" << name << ", " << age << ") [assignment operator]\n";
        return *this;
    }
    
    
    int main()
    {
        Person p;
        Person me("Humberto Naves", 29);
        Person copy(p);
        Person q;
    
        q = me;
        return 0;
    }