Exercise 7:
Create a directory ex7 and store in this directory all the files corresponding to this exercise.
Question 1: Access control and inheritance (40 points).
First, copy the files from the Question 2 of the Exercise 6: "Person.h" and "Student.h/cpp" to the directory of ex7.
Step 1:
Add a method print() to the class Student:
void print() const {
// code to print the name, student id and year of the student
}
Step 2:
Modify the access control of the class Person.h in order to let the print() method of Student access the member data "name" directly without using the method "get_name()" of Person.
Make sure that the member data "name" is still not accessible from class methods and functions outside of Person and the class inheriting from Person.
To test step 1 and 2, you can type in "test_access.cpp" the following code:
// test_access.cpp
#include <iostream>
#include "Person.h"
#include "Student.h"
int main() {
Student* s = new Student("Tanaka", 2, 12345);
s->print();
delete s;
}
Question 2: Substitution principle (20 points).
In this question you will exercise your understanding of the substitution principle.
Create a file "test_substitution.cpp" and complete the following code:
#include <iostream>
#include "Person.h"
#include "Student.h"
void print_information (const Person& p)
{
// complete here by printing the name of p
}
void print_information (const Person* p)
{
// complete here by printing the name of p
}
int main()
{
Student s1("Tanaka", 2, 12345);
Student* s2 = new Student("Nakata", 4, 789);
print_information(s1);
print_information(s2);
delete s2;
}
Question 3: Inheritance and substitution principle (40 points).
Create files "ForeignStudent.h" and "ForeignStudent.cpp". ForeignStudent inherits from Student (public inheritance). In addition to Students' methods and data, it has the additional data (private):
And the additional methods (public):
- std::string get_country() const;
- void set_country(const std::string& c);
Constructor for a ForeignStudent has the form:
- ForeignStudent(const std::string& name, int year, int student_id, const std::string& country);
Create this class ForeignStudent. Add the methods and data in "ForeignStudent.h" and write the implementation in "ForeignStudent.cpp". Write the code for the constructor in "ForeignStudent.h".
In addition to the constructor, please also write the code for the copy constructor and the assignment operator. Be careful to properly call the direct base class copy constructor and assignment operator. The code for the copy constructor and the assignment operator can go in the header file "ForeignStudent.h".
Create a file "test_inheritance.cpp" and type the following code (nothing to complete):
#include <iostream>
#include "Person.h"
#include "Student.h"
#include "ForeignStudent.h"
int main()
{
ForeignStudent fs("Jack", 2, 1577, "HK");
std::cout << "Name: " << fs.get_name() << std::endl;
std::cout << "Country: " << fs.get_country() << std::endl;
ForeignStudent fs2("James", 4, 15, "Australia");
fs2 = fs;
std::cout << "Name: " << fs2.get_name() << std::endl;
std::cout << "Country: " << fs2.get_country() << std::endl;
}