Demo 1 - Inheritance

Main Program

// main.cpp // InheritanceDemo // Created by Kevin Roark on 4/2/23. // Example: class Student inherits from class Person #include <iostream> #include "Student.hpp" using namespace std; int main() { // Create a Student object Student myStudent("Kevin", "Roark", "210-837-0600", "kroark8@alamo.edu", 3.94, "Computer Science"); // Print Student info myStudent.PrintStudentInfo(); return 0; }

Explanation:

  • This is the main program that demonstrates inheritance in C++.

  • A Student object is created by passing information like name, phone number, email, GPA, and major to the Student constructor.

  • The PrintStudentInfo() function is called to display the student’s information, utilizing both Person and Student class functionality.


Base Class: Person (Header File)

// Person.hpp // InheritanceDemo // Created by Kevin Roark on 4/2/23. #ifndef Person_hpp #define Person_hpp #include <string> using namespace std; class Person { private: string firstName; // Variable to store the first name string lastName; // Variable to store the last name string phoneNumber; // Variable to store the phone number string email; // Variable to store the email address public: // Setters void setName(string pFirst, string pLast); void setPhoneNumber(string pPhone); void setEmail(string pEmail); // Getters string getFirstName() const; string getLastName() const; string getPhoneNumber() const; string getEmail() const; // Constructor Person(string pFirst = "", string pLast = "", string pPhone = "", string pEmail = ""); protected: // Protected function to print basic info (accessible to derived classes) void print() const; }; #endif /* Person_hpp */

Explanation:

  • The Person class represents a base class with private member variables for storing personal information such as name, phone number, and email.

  • The Person class has public setters and getters to access and modify these private members.

  • The print() method is protected, meaning it is accessible to derived classes like Student, but not to other external classes or functions.


Base Class: Person (Implementation File)

// Person.cpp // InheritanceDemo // Created by Kevin Roark on 4/2/23. #include "Person.hpp" #include <iostream> using namespace std; void Person::print() const { cout << "Name: " << firstName << " " << lastName << endl; cout << "Email: " << email << "\nPhone: " << phoneNumber << endl; } void Person::setName(string pFirst, string pLast) { firstName = pFirst; lastName = pLast; } void Person::setPhoneNumber(string pPhone) { phoneNumber = pPhone; } void Person::setEmail(string pEmail) { email = pEmail; } string Person::getFirstName() const { return firstName; } string Person::getLastName() const { return lastName; } string Person::getPhoneNumber() const { return phoneNumber; } string Person::getEmail() const { return email; } // Constructor implementation Person::Person(string pFirst, string pLast, string pPhone, string pEmail) : firstName(pFirst), lastName(pLast), phoneNumber(pPhone), email(pEmail) { }

Explanation:

  • The Person class implementation file defines how each of the member functions works.

  • The constructor initializes the member variables, either using the provided arguments or default values.

  • The print() function outputs the person's information, and it's intended for use by derived classes such as Student.


Derived Class: Student (Header File)

Explanation:

  • The Student class is derived from Person using public inheritance, meaning that all public members of Person remain public in Student.

  • Student adds two private data members: gpa and major, along with getters, setters, and a PrintStudentInfo() method to display the student's full information.

  • The constructor uses the initialization list to call the Person constructor, ensuring that inherited attributes are properly initialized.


Derived Class: Student (Implementation File)

Explanation:

  • The Student class constructor initializes both the inherited Person attributes and the specific Student attributes (gpa and major).

  • The PrintStudentInfo() method calls the print() function from the Person class to display the basic information (name, phone number, email), then adds the student's GPA and major to the output.

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark