CISC 2000 Computer Science II

CISC 2000 Computer Science II

CISC 2000 Computer Science II

Instructor: / Professor Harazduk / Name: / Name /
Results: / Class: / Class /
Date:

1)Answer the questions below.

a)What is a constructor?

b)What is a default constructor? What is the consequence, if any, if a class does not provide a default constructor? (Hint: if there are no constructors and if there are some constructors)

c)How many constructors can a class have? Can you have a class with no defined constructors? If a class has more than one constructor, which of them is invoked?

d)How are an object’s data members initialized if a class has only an implicitly defined default constructor? In other words, no constructors have been explicitly defined for the class.

e)What is the interface of a class? What is the implementation of a class?

f)What's the benefit of making member variables or functions of a class private?

g)What is a member function and how does it differ from a nonmember function (i.e. an external functions)?

h)What is a mutator function and what is an accessor function? Explain why a class might provide these member functions.

i)What is the purpose of the scope resolution operator?

j)Explain the difference between a class and an object?

k)What is a header file? What is a source code file? Discuss the purpose of each.

l)Explain how an object declared to be const differs from a non const object.

m)Explain why we must initialize data members declared to be static const in the class definition of the class.

n)Explain why data members declared to be static can be accessed independently of any object.

2)Read the following code, and write a comment in the code to mark the point in the program's executionthat constructors of the Student class is called, and which constructor is called. Also write commentnext to the line of code where destructor is called.

class Student {

public:

friend Student OutputDuplicate (Student obj);

Student (); //default constructor

Student (string n);

Student (Student obj); //copy constructor

...

private:

string name;

...

};

Student OutputDuplicate (Student obj)

{

cout <"Student record:";

cout <obj.name < endl;

Student * anotherObj = new Student ("Jane Smith");

*anotherObj = obj;

return *anotherObj;

}

int main()

{

Student a, b ("John Smith");

b = OutputDuplicate (a);

}

3.When you define a class, and do not provide any constructor, then the default constructor will be used. Please type in the following code, add necessary header files, compile and run the code, and a main function with a call to the operator < and answer the question based on your observation.

class YourClassName

{

public:

friend ostream & operator< (ostream & outs, const YourClassName & o)

{

outs < o.value < endl;

outs < o.dvalue < endl;

for (int i=0;i<10;i++)

outs <"a["<i <"]:" < o.a[i]<endl;

outs < o.c < endl;

return outs;

}

private:

int value;

double dvalue;

int a[10];

char c;

};

// Todo: How does the default constructor initialize the member variables for us?

// int member variable:

// double member variable:

// array variable

// char variable:

//

4 For which of the following classes do we need to implement the big three, i.e., copy constructor, destructor, and overload assignment operator? Explain briey why. Note that all member variables for the class is listed, but member functions are omitted.

(a) class Student

class Student {// used for student

public:

...

private:

string name;

Date birthday;

};

(b) class AbsenceDates

class AbsenceDates { // used to keep track absence dates for a student...

public:

..

private:

Date * ptrDateArray; //points to an array of Date

int length; //the capacity of array that ptrDateArray points to

};

(c) class HomeworkGrades

class HomeworkGrades

{

public:

...

private:

double grades[10];

int length; //number of grades entered into the array grades

};

Page 1