In c++ Refer to the examples below Develop a program using class inheritance. Th
ID: 3702138 • Letter: I
Question
In c++
Refer to the examples below Develop a program using class inheritance. The header file And implementation files.
Think about and understand the need for class inheritance, its implementation, and its effects. As a comment in the main sentence
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
string ssn;
public:
Person(const string &, const string &);
void print() const;
}; // Person
Person::Person(const string &nm, const string &ss)
: name(nm), ssn(ss) {
} // Person()
void Person::print() const {
cout << "Name : " << name << endl;
cout << "SSN : " << ssn << endl;
} // print();
class Student {
private:
string name;
string ssn;
string major;
int year;
public:
Student(const string &, const string &, const string &, int);
void print() const;
void changeMajor(const string &);
};
Student::Student(const string &nm, const string &ss, const string &mj, int y)
: name(nm), ssn(ss), major(mj) {
year = y;
} // Student()
void Student::print() const {
cout << "Name : " << name << endl;
cout << "SSN : " << ssn << endl;
cout << "Major : " << major << endl;
cout << "Year : " << year << endl;
} // print();
void Student::changeMajor(const string &mj) {
major = mj;
} // changeMajor()
int main() {
Person pe("Mary", "123456789");
Student st("Bob", "987654321", "ICE", 2);
Student *p = &st;
pe.print();
cout << endl;
st.print();
cout << endl;
st.changeMajor("Info & Comm Emg");
p->print();
return 0;
} // main()
Explanation / Answer
/**** Complete code using inheritence as below ****/
#include <iostream>
#include <string>
using namespace std;
/****
Person is the base Class and it has two prperty name and ssn
I have declared these property as protected as these properties are also used in the Student class
we declare the property in the base class as protected so that this can be inherited in the derived class. We can inherit only
protected and public members of the base class in the derived class not the private members
****/
class Person {
protected:
string name;
string ssn;
public:
Person(const string &, const string &);
void print() const;
}; // Person
Person::Person(const string &nm, const string &ss)
: name(nm), ssn(ss) {
} // Person() class print method
void Person::print() const {
cout << "Name : " << name << endl;
cout << "SSN : " << ssn << endl;
} // print();
/*****
Creating derived class Student which inherits from the Person class , as it inherits from Person class so all the
Protected and public members are also part of Student class. So we don't have to agian declare the name and ssn in the
Student class as they are visible from Person class due to inheritence
****/
class Student : public Person{
private:
string major;
int year;
public:
Student(const string &, const string &, const string &, int);
void print() const;
void changeMajor(const string &);
};
/**
As there is parameterized constructor in the base class so first it is mandatory to class the
base class parameterised constructor to initialze its member function.
In this Constructor name and ssn are initialized with Person Constructor and major and year are initialized with
Student class constructor.
***/
Student::Student(const string &nm, const string &ss, const string &mj, int y)
: Person(nm,ss) {
major = mj;
year = y;
} // Student() class print method
void Student::print() const {
cout << "Name : " << name << endl;
cout << "SSN : " << ssn << endl;
cout << "Major : " << major << endl;
cout << "Year : " << year << endl;
} // print();
void Student::changeMajor(const string &mj) {
major = mj;
} // changeMajor()
/****
As we have used the inheritence so it very important to understand that which object is created
and what member function's you can access from that object.
Case 1: If we create the base class Object and assign it Base class refrence then we can access all the public member function of the base class
Case2: If we create the derived class Object and assign it derived class refrence then we can access all the public member function of the derived class
Case 3: If we create the derived class object and if it is refrenced by the base class then
Student st("Bob", "987654321", "ICE", 2);
Person *p1 = &st;
p1->print();
As both Student and Person class has the print method so which print method will be called in above case.
This is a bit tricky in c++. It will always be called of base class.
// See the magic by declaring the Person class print method using vertual keyword like void virtual print() const;
Always remeber the method of the class whose object is created will be called . In our case
Derived class i.e. Student class object is created so its print method will be called
****/
int main() {
// Case1:
Person pe("Mary", "123456789");
pe.print();
cout<<endl;
// Case2:
Student st("Bob", "987654321", "ICE", 2);
st.print();
cout<<endl;
Student *p = &st;
st.changeMajor("Info & Comm Emg");
p->print();
cout<<endl;
//case3:
Student st2("Harry", "54321", "Wand", 1);
Person *p1 = &st2;
p1->print();
return 0;
} // main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.