Outline


  • Access Specifier: public, protect, and private
  • Inheritance
  • Operator Overloading
  • Polymorphism

Access Specifier: public, protect, and private


#include <iostream>
 
using namespace std;
 
class Actress {
private:
    int age;
 
public:
    char name[255];
    Actress(char *name, int age):age(age) {
        strcpy(this->name, name);
    }
};
 
int main() {
    Actress actress("Alice", 25);
 
    cout << actress.name << endl; // allowed
    // cout << actress.age << endl; // NOT allowed
 
    // this is legal but ill-logical
    // the name of an actress object should NOT
    // be modified from outside
    strcpy(actress.name, "Eve"); // allowed
 
    return 0;
}

Inheritance


#include <iostream>
 
using namespace std;
 
class A {
private:
    int x;
protected:
    int y;
public:
    int z;
};
 
class B : public A {
public:
    void print() {
        cout << z;
        y = 0;
        // cout << x; not allowed
    }
};
 
class C : protected A {
public:
    void print() {
        cout << z;
        y = 0;
        // cout << x; not allowed
    }
};
 
class D : private A {
public:
    void print() {
        cout << z;
        y = 0;
        // cout << x; not allowed
    }
};
 
int main() {
    B obj1;
    // obj1.x = 0; not allowed
    // obj1.y = 0; not allowed
    obj1.z = 0;
    obj1.print();
 
    C obj2;
    // obj2.x = 0; not allowed
    // obj2.y = 0; not allowed
    // obj2.z = 0; not allowed
    obj2.print();
 
    D obj3;
    // obj3.x = 0; not allowed
    // obj3.y = 0; not allowed
    // obj3.z = 0; not allowed
    obj3.print();
    return 0;
}

Constructors in Inheritance

#include <iostream>
 
using namespace std;
 
class A {
public:
    A() {
        cout << "A's default constructor" << endl;
    }
    A(int a) {
        cout << "A's non-default constructor" << endl;
    }
};
 
class B : public A {
public:
    B() {
        cout << "calling A(2311) in B()" << endl;
        A(2311);
        cout << "calling A() in B()" << endl;
        A();
        cout << "B's constructor" << endl;
    }
};
 
int main() {
    B b;
    // Output:
    // "A's default constructor"
    // "calling A(2311) in B()"
    // "A's non-default constructor"
    // "calling A() in B()"
    // "A's default constructor"
    // "B's constructor"
}

Passing Arguments to Constructors

#include <iostream>
 
using namespace std;
 
class Student {
protected:
    int sid;
public:
    Student(int sid = 0) : sid(sid) {}
 
    int getSid() {
        return sid;
    }
};
 
class TA : public Student {
protected:
    int courseid;
public:
    // To pass argument from child to parent, use initializer list
    TA(int sid = 0, int courseid = 0) : Student(sid), courseid(courseid) {}
 
    int getCourseId() {
        return courseid;
    }
};
 
int main() {
    int sid = 12345, courseid = 2311;
    TA bob(sid, courseid);
    cout << bob.getSid() << ": " << bob.getCourseId() << endl;
    return 0;
}

Destructor in Inheritance

#include <iostream>
 
using namespace std;
 
class A {
public:
    ~A() { cout << "A's destructor\\n"; }
};
 
class B : public A {
public:
    ~B() { cout << "B's destructor\\n"; }
};
 
int main() {
    B *b = new B();
    delete b;
    return 0;
    // Output:
    // "B's destructor"
    // "A's destructor"
}

Operator Overloading


#include <iostream>
 
using namespace std;
 
class Circle {
private:
    int radius;
public:
    Circle(int radius) : radius(radius) {};
 
    bool operator<(Circle &rhs) {
        if (radius < rhs.radius) {
            return true;
        } else {
            return false;
        }
    }
};
 
int main() {
    Circle a(3);
    Circle b(5);
    cout << (a < b);
    return 0;
}

Friend Function

#include <iostream>
#include <cmath>
 
using namespace std;
 
class Triangle {
private:
    double s1, s2, s3;
public:
    Triangle() {
        s1 = 0;
        s2 = 0;
        s3 = 0;
    }
 
    Triangle(double s1, double s2, double s3) : s1(s1), s2(s2), s3(s3) {}
 
    double getArea() {
        double s = (s1 + s2 + s3) / 2;
        return sqrt(s * (s - s1) * (s - s2) * (s - s3));
    }
 
    friend bool operator<(Triangle &lhs, Triangle &rhs) {
        return lhs.getArea() < rhs.getArea();
    }
 
    friend bool operator>(Triangle &lhs, Triangle &rhs) {
        return lhs.getArea() > rhs.getArea();
    }
 
    friend ostream &operator<<(ostream &outs, Triangle &t) {
        outs << "The sides are: ";
        outs << t.s1 << " " << t.s2 << "" << t.s3 << " ";
        outs << "The area is: ";
        outs << t.getArea() << endl;
        return outs;
    }
};
 
int main() {
    Triangle t1(3, 4, 5);
    Triangle t2(5, 6, 7);
    cout << t1;
    cout << t2;
    if (t1 < t2) {
        cout << "t1 is smaller" << endl;
    } else {
        cout << "t2 is smaller" << endl;
    }
    return 0;
}

Polymorphism


Static Type vs Dynamic Type

#include <iostream>
 
using namespace std;
 
class Animal {
public:
    void sayHi() {
        cout << "..." << endl;
    }
};
 
class Human : public Animal {
public:
    void sayHi() {
        cout << "hi" << endl;
    }
};
 
class Dog : public Animal {
public:
    void sayHi() {
        cout << "wow wow" << endl;
    }
};
 
int main() {
    Human *human = new Human();
    Dog *dog = new Dog();
 
    Animal *a;  // static type of "a" is Animal
    a = human;  // dynamic type of "a" is Human
    a->sayHi(); // will print "..."
    a = dog;    // dynamic type of "a" is Dog
    a->sayHi(); // will print "..."
 
    delete human;
    delete dog;
    return 0;
}

Dynamic Binding: Virtual Function

#include <iostream>
 
using namespace std;
 
class Animal {
public:
    virtual void sayHi() {
        cout << "..." << endl;
    }
};
 
class Human : public Animal {
public:
    void sayHi() {
        cout << "hi" << endl;
    }
};
 
class Dog : public Animal {
public:
    void sayHi() {
        cout << "wow wow" << endl;
    }
};
 
int main() {
    Human *human = new Human();
    Dog *dog = new Dog();
 
    Animal *a;  // static type of "a" is Animal
    a = human;  // dynamic type of "a" is Human
    a->sayHi(); // will print "hi"
    a = dog;    // dynamic type of "a" is Dog
    a->sayHi(); // will print "wow wow"
 
    delete human;
    delete dog;
    return 0;
}