isEmpty returns true if the receiver list is empty
isEqual: anotherList returns true if both lists have the
same elements
first returns the first item on the list
rest returns the list minus the first item
addToFirst: anItem places anItem at the front of the list
cons: anotherList returns the receiver list concatenated
with the argument list.
(a) Write the class definition for MyLinkedList
Object subclass: #MyLinkedList
instanceVariableNames: YOU FILL IN HERE
classVariableNames:
poolDictionaries:
category: 'My-Data-Structures'
(b) Write the method definitions for each of the desired functions.
templateclass Count { public: //Constructors/destructors Count() { } virtual ~Count() { } //Implementors virtual void decrement() =0; virtual void increment() =0; void reset() { value = reset_value; } //Accessors Count_Type get_value() { return value; } void set_value(Count_Type new_value) { value = new_value; } Count_Type get_reset_value() { return reset_value; } void set_reset_value(Count_Type new_value) { value = new_value; } protected: //Data members Count_Type value; Count_Type reset_value;} ;
(a) In terms of inheritance, does this definition differ from the definition of the class Count in Smalltalk? Can subclasses of Count in Smalltalk access anything more or less than derived class of Count in C++? Can external classes that use Count in Smalltalk access anything more or less than external classes that use Count in C++?
(b) Why are the data members declared protected in the class above? Why not declare them private?
(c) Why are ~Count(), increment(), and decrement() declared virtual? What does that mean?
(d) Suppose that we don't want to allow the data members of Count to be arbitrarily manipulated from outside of this class or classes derived from Count. What would you change in the above definition? Why?
(a) Describe the relationship between two classes using generalization-specialization inheritance. (For example, what can you say about how IntegerCount and Count are similar/dissimilar given that IntegerCount is a subclass of Count?)
(b) Describe the relationship between two classes that are related by implementation inheritance.
(c) What feature(s) of C++ enable use of implementation inheritance? Explain how.
(d) Since implementation inheritance is not supported in Smalltalk, what can a programmer do to achieve much the same effect?
Discussion from Previous Quarter
(d) What is a virtual function in C++? Why would you want to declare a function virtual?
Discussion from Previous Quarter (This was a separate question last year)
class Time {
public:
Time(int = 0, int = 0, int = 0); // constructor
// set functions
void setTime(int, int, int); //set hour, min. sec.
void setHour(int); // set hour
void setMinute(int); // set minute
void setSecond(int); // set second
// get functions
int getHour(); //return hour
int getMinute(); //return minute
int getSecond(); //return second
void printMilitary(); //output military time
void printStandard(); //output standard time
private:
int hour; // 0 - 23
int minute; // 0 - 59
int second; // 0 - 59
};
A. Why should hour, minute, and second variables be private? Why not just
make them public and do away with the get and set functions?
B. On the back of this page, define a class DateAndTime, derived from class
Time which includes a day (1-31), a month (1-12), and a year (0-9999). Think
about what public and private parts are needed in this new class.
class employee {
public:
employee(char *, char *, float);
void show_employee(void);
protected:
//LINES DELETED HERE - A
private:
//LINES DELETED HERE - B
};
class manager : public employee {
public:
manager(char *, char *, float, char*, float, int);
void show_manager(void);
protected:
//LINES DELETED HERE - C
private:
//LINES DELETED HERE - D
};
Here are some facts about employees and managers in the company being modeled:
class book {
public:
book(char *, char *, int);
void show_book(void);
private:
char title[64];
char author[64];
int pages;
};
book::book(char *title, char *author, int pages)
{
strcpy(book::title,title);
strcpy(book::author,author);
book::pages = pages;
}
void book:show_book(void)
{
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Pages: " << pages << endl;
}
a) Explain how you would implement this same class in Smalltalk. Tell me what instance variables the class would have, how you would initializing new objects in Smalltalk, and how you would provide the functionality of show_book (WRITE THE METHOD showBook). b) In terms of information hiding, how would your Smalltalk class differ from the C++ class?
a) Using the Simulation Package discussed in class, what would you model as the Simulation? As SimulationObjects? As Resources?
b) What statistics would you record and at what events (you don't have to get the names right, but you should be able to describe the significant events in the simulation)?