Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) Create a class named Serial that includes a data member that holds a \"ser

ID: 656093 • Letter: #

Question

(C++) Create a class named Serial that includes a data member that holds a "serial number" for each object created from the

class. That is, the first object created will be numbered 1, the second 2, and so on.

A static class variable should be used to contain the current serial number that is to be applied to the next created object.

Include the following member functions:

Serial::Serial(); // Default constructor, sets serial number of object

int Serial::getSerial(); // Returns serial number of object

Output

Write a main() program that crates three objects and queries each one about its serial number and displays the results.

Use appropriate headings in the output.

Explanation / Answer

Serial.cpp

#include <iostream>
using namespace std;

class Serial {
public:
static int counter;
int number;

//constructor

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

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


int Serial::counter = 0;

int main() {
Serial obj1; //creating first object for class Serial
Serial obj2; // creating another object for class Serial
Serial obj3; // creating third object for class Serial
obj1.getSerial(); //calling them from method getSerial
obj2.getSerial();
obj3.getSerial();
cin.get();
}

output:

I am object 1                                                                                                                                          

I am object 2                                                                                                                                          

I am object 3