COP 3330 FSU Exam 2 Study Guide
with Complete Solutions
A class constructor
a) may have any number of parameters
b) must be supplied explicitly for each class
c) is a method that is called automatically when an object is created
d) is used to ensure that an object's resources are properly allocated when it comes into
scope
e) is used by an IDE (such as Visual C++) to assist in editing a class
f) will be supplied automatically if none is supplied by the programmer
g) will be used to make sure resources are de-allocated when an object goes out of
scope
h) may be used to build the class - ANSWER-may have any number of parameters
is a method that is called automatically when an object is created
is used to ensure that an object's resources are properly allocated when it comes into
scope
will be supplied automatically if none is supplied by the programme
A class destructor
a. may have any number of parameters
b. must be paired with each constructor for the class
c. is responsible for de-allocating resources when an object goes out of scope
d. is called multiple times by operator delete []
e. is called by operator new
f. is called by operator delete
g. will be created automatically if none is supplied by the programmer
,h. must be supplied with the class if a constructor is also supplied - ANSWER-is
responsible for de-allocating resources when an object goes out of scope
is called multiple times by operator delete []
is called by operator delete
will be created automatically if none is supplied by the programmer
Assume that parent class Animal has a default constructor explicitly defined. Write a
derived class, Cat, and include a default constructor and a deconstructor. - ANSWER-
class Cat:public Animal
{
public:
Cat() : Animal()
{
std::cout << "Cat::Cat()\n";
}
~Cat()
{
std::cout << "Cat::~Cat()\n";
}
};
class Box
{
public:
Box (float length = 0, float width = 0, float height = 0);
virtual ~Box();
float Volume() const;
private:
float length_, width_, height_;
};
a) Box b (2,4,8);
b) b Box (2, 4, 8);
c) class Box b;
d) Box b; - ANSWER-Box b (2,4,8); Box b;
Consider the classes and client program defined below
class A
{
, public:
void F() { std::cout << "A::F()\n"; }
void G() { std::cout << "A::G()\n"; }
virtual void H() { std::cout << " A::H\n"; }
virtual ~A(){};
};
class B : public A
{
public:
void G() { std::cout << "B::G()\n"; }
void H() { std::cout << "B::H()\n"; }
};
#include<iostream>
int main()
{
A a;
B b;
A * p = new B;
a.F();
a.G();
a.H();
b.F();
b.G();
b.H();
p->F();
p->G();
p->H();
}
What is the screen output of this program? - ANSWER-A::F()
A::G()
A::H()
A::F()
B::G()
B::H()
A::F()
A::G()
B::H()
Consider the classes and client program defined below
class BClass
{
with Complete Solutions
A class constructor
a) may have any number of parameters
b) must be supplied explicitly for each class
c) is a method that is called automatically when an object is created
d) is used to ensure that an object's resources are properly allocated when it comes into
scope
e) is used by an IDE (such as Visual C++) to assist in editing a class
f) will be supplied automatically if none is supplied by the programmer
g) will be used to make sure resources are de-allocated when an object goes out of
scope
h) may be used to build the class - ANSWER-may have any number of parameters
is a method that is called automatically when an object is created
is used to ensure that an object's resources are properly allocated when it comes into
scope
will be supplied automatically if none is supplied by the programme
A class destructor
a. may have any number of parameters
b. must be paired with each constructor for the class
c. is responsible for de-allocating resources when an object goes out of scope
d. is called multiple times by operator delete []
e. is called by operator new
f. is called by operator delete
g. will be created automatically if none is supplied by the programmer
,h. must be supplied with the class if a constructor is also supplied - ANSWER-is
responsible for de-allocating resources when an object goes out of scope
is called multiple times by operator delete []
is called by operator delete
will be created automatically if none is supplied by the programmer
Assume that parent class Animal has a default constructor explicitly defined. Write a
derived class, Cat, and include a default constructor and a deconstructor. - ANSWER-
class Cat:public Animal
{
public:
Cat() : Animal()
{
std::cout << "Cat::Cat()\n";
}
~Cat()
{
std::cout << "Cat::~Cat()\n";
}
};
class Box
{
public:
Box (float length = 0, float width = 0, float height = 0);
virtual ~Box();
float Volume() const;
private:
float length_, width_, height_;
};
a) Box b (2,4,8);
b) b Box (2, 4, 8);
c) class Box b;
d) Box b; - ANSWER-Box b (2,4,8); Box b;
Consider the classes and client program defined below
class A
{
, public:
void F() { std::cout << "A::F()\n"; }
void G() { std::cout << "A::G()\n"; }
virtual void H() { std::cout << " A::H\n"; }
virtual ~A(){};
};
class B : public A
{
public:
void G() { std::cout << "B::G()\n"; }
void H() { std::cout << "B::H()\n"; }
};
#include<iostream>
int main()
{
A a;
B b;
A * p = new B;
a.F();
a.G();
a.H();
b.F();
b.G();
b.H();
p->F();
p->G();
p->H();
}
What is the screen output of this program? - ANSWER-A::F()
A::G()
A::H()
A::F()
B::G()
B::H()
A::F()
A::G()
B::H()
Consider the classes and client program defined below
class BClass
{