Create the class Student consisting of data members register_no and name. Define array of objects by using constructor Student() to assign the values for the class Student data members, display() member function is used to display the information of student. Finally free the resources of data objects using destructor member function. (Note: Define object size of two)
#include <iostream>
#include <stdexcept>
#define OBJECT_SIZE 2
using namespace std;
class Student {
private:
int register_no;
string name;
public:
Student(int register_no, const string& name);
~Student();
void display();
};
Student::Student(int register_no, const string& name) {
this->register_no = register_no;
this->name = name;
}
Student::~Student() {
}
void Student::display() {
cout << "Student number: " << this->register_no << ", Name: " << this->name << "\n";
}
int main() {
Student arr[OBJECT_SIZE] = { Student(1, "Mike"), Student(2, "John")};
for (int i = 0; i < OBJECT_SIZE; i++) {
arr[i].display();
}
return 0;
}
Comments
Leave a comment