Answer to Question #40782 in C++ for tydoh priscilla

Question #40782
create a class that includes a data member that holds a serial number for each object created from a class. that is the first object will be numbered 1 , the second 2 and so on. then each object is create , its constructor can examine this count member variable to determine thhe approprate serial number for the new object.
add a member function that permits an object to report its own serial number . then the main() program that creates three objects and queries each one about its serial number. they should respond i am object 2, and so on.
1
Expert's answer
2014-04-07T12:17:42-0400
#include <iostream>
using namespace std;

class CounterClass {
private:
static int counter;
int number;

public:
CounterClass() {
number = ++counter;
};

void tell_number() {
cout << "I am object " << number << endl;
};
};


int CounterClass::counter = 0;

int main() {
CounterClass obj1;
CounterClass obj2;
CounterClass obj3;
obj1.tell_number();
obj2.tell_number();
obj3.tell_number();
cin.get();
}
</iostream>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
13.11.18, 17:13

Dear Vaikalya, you're welcome!

vaikalya
12.11.18, 18:14

thank you very much:)

Assignment Expert
22.05.14, 16:33

Dear Ayesha, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!

ayesha
19.05.14, 03:07

good job. helped me a lot. thank u sooooooooo much.

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS