Starting from:

$30

CSE165-Lab 5 Solved

1.                   Create a class Counted that contains an int id and a static int count. The default constructor should begin: Counted() : id(count++). It should also print its id and that it’s being created. The destructor should print that it’s being destroyed and its id. Test your class. 

 

Output example:  

In the constructor, cout << “An object is being created is created, id: ”<< id << endl; In the destructor, cout << “ The created object is being destroyed, id: ” << id << endl; 

 

2.                   Create a vector< Counted*> and fill it with pointers to new Counted objects (from Exercise 1). Move through the vector and print the Counted objects, then move through again and delete each one. 

 

Output example:  

In the constructor, cout << “An object is being created is created, id: ”<< id << endl; 

In the destructor, cout << “ The created object is being destroyed, id: ” << id << endl; 

[Optional] In the main(), print each created object’s id. 

 

3.                   In AddingVirtuals.cpp, make all the member functions of Pet pure virtual, but provide a definition for name(). Fix Dog as necessary, using the base-class definition of name(). 

 

Output example:  int main() { 

  Pet* p = new Dog("placeholder"); // replace the placeholder with any name you’d like  cout << p->speak(); 

                delete p; 

 

                return 0; 

More products