Question
#include #include using namespace std; class Student { public: Student() { cout < < Student(), constructor called. < < endl; } explicit Student(string stuName) :
#include
#include
using namespace std;
class Student {
public:
Student() {
cout << "Student(), constructor called." << endl;
}
explicit Student(string stuName) : stuName(stuName) {
cout << "Student(stuName), constructor called." << endl;
}
~Student() {
cout << "~Student(), destructor called." << endl;
}
string getName() {
return this->stuName;
}
virtual void sayHi() {
cout << "Student: Hi!" << endl;
}
void sayHello() {
cout << "Student: Hello!" << endl;
}
private:
string stuName{ "Student" };
};
class CSStudent : public Student {
public:
CSStudent() {}
explicit CSStudent(string stuName) : Student() {}
void sayHello() {
cout << "CSStudent: Hello!" << endl;
}
};
class CSStudent340 : public CSStudent {
public:
CSStudent340() {
cout << "CSStudent340(), constructor called." << endl;
}
explicit CSStudent340(string stuName) : CSStudent(stuName) {
cout << "CSStudent340(stuName), constructor called." << endl;
}
~CSStudent340() {
cout << "~CSStudent340(), destructor called." << endl;
}
void sayHi() {
cout << "CSStudent340: Hi!" << endl;
}
};
int main() {
CSStudent340 stuM{ "Mickey" };
cout << "Stu: " << stuM.getName() << endl;
unique_ptr
stu->sayHi();
stu->sayHello();
return 0;
}
I know that the program output 10 lines but I do not know why. Can you please explain.
These are the line the program outputs. Student(), constructor called. CSStudent340(stuName), constructor called. Stu: Student Student(), constructor called. CSStudent340(stuName), constructor called. CSStudent340: Hi! Student: Hello! ~Student(), destructor called. ~CSStudent340(), destructor called. ~Student(), destructor called.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started