Answers to written HW#2

Disclaimer: even if it looks like C++ code, it is supposed to be pseudo-code. I haven't compiled/run any of the below code, although I believe it to be correct modulo some possible typos. --Ami.

Self-Test Exercises

    1. SphereClass S(2.0);
    2. ballClass B(6.0, "Beachball");
    3. cout << S.diameter();
      cout << B.diameter();
  1. class planetClass : public ballClass {
        double soldist; // Distance to the sun
        double getSolDist();
        void setSolDist(double);
        planetClass(double, const char*, double);
    1. No, since SphereClass::TheRadius is a private member, only the class and its friends can access it directly.
    2. Yes, because the public inheritance makes protected members of SphereClass protected in ballClass, which is accessible to ballClass.
  2. Play and Record should be made virtual, in anticipation of being overriden in the derived VCR class.
  3. L can be an instance of sortedListClass, if the listNode struct is defined to support character arrays as the key on which to sort.
  4. No, because DisplayList is a pure virtual function, and pure virtuals need not be defined in descendants of abstract classes.
  5. Because a class's private members will not be re-definable in a descendant class, making it impossible to define the function in a derived class. However, virtual functions must be defined, and the only place a private member can be defined is in the base class, so it makes no sense to make it virtual.
  6. aClass C;
    cout << C.Data();


    1. Sphere.DisplayStatistics() will call sphereClass::Area(), and Ball.DisplayStatistics() will call ballClass::Area(). This is because the binding is done at compile time, and each class's DisplayStatistics's call to Area gets bound by the type of class calling DisplayStatistics().
    2. The first two calls execute the sphere Area, whereas the last one executes ball's Area. This is again due to the fact that the binding is done at compile time, and all the compiler knows about what Sptr is pointing to comes from its type, which is a sphere pointer, so it executes functions out of the sphereClass namespace.
    1. All of them.
    2. AE and InE.
    3. InE.
    4. double Eval(algExprClass);
      //Assuming we now have an infix expression in InE, let's evaluate it:
      cout << InE << " = " << Eval(InE);
  1. struct listNode;  // defined later
    typedef listNode* ptrType;
    class frontListClass : public baseListClass {
        ptrType head;
        bool ListIsEmpty() {return ((head!=NULL)? true : false);};
        int ListLength() {
            int i=0;
    	ptrType t=head;
    	for(; t; t=t->next, i++);
    	return i;
        void DisplayList() {
            ptrType t=head;
    	while(t) {
    	    cout << t->data << ", ";
        bool Insert(ptrType);
        ptrType Remove(); // Always from head of list, so don't need arg.
        ptrType Retrieve();
  2. class stack : private frontListClass {
        bool empty() {return IsEmpty();};
        bool Push(ptrType) {return Insert(ptrType);};
        ptrType Pop() {return Remove();};
  3. #include 
    class personClass {
        char fname[], lname[], minitial;
        char SSN[9];
        personClass(char* f, char* l, char m, char *s) {
            strcpy(fname, f);
    	strcpy(lname, l);
    	strcpy(SSN, s);
        virtual ostream& print(ostream&) = 0;
    class studentClass : public personClass {
        char studentID[9];
        studentClass(char*f, char*l, char m, char*s, char* sid) :
            personClass(f, l, m, s) {strcpy(studentID, sid);};
        virtual ostream& print(ostream& os) {
            os << fname << " " << minitial << ". " << lname << endl
    	   << "SSN: " << SSN << ", StudentID: " << studentID << endl;
    class gradStudentClass : public studentClass {
        bool TA; // Is the person a TA?
        gradStudentClass(char*f, char*l, char m, char*s, char* sid, bool t) :
                studentClass(f, l, m, s, sid), TA(t) {;};
        ostream& print(ostream& os)	{
            os << fname << " " << minitial << ". " << lname << endl
    	   << "SSN: " << SSN << ", StudentID: " << studentID << endl;
    	if (TA) os << "And is a TA." << endl;