Please Help this PART 1: ( Using the Person class we defined in class (included
ID: 667505 • Letter: P
Question
Please Help this
PART 1: (
Using the Person class we defined in class (included below), create a class called Student that inherits from Person. Your Student class should have the following:
A member variable called m_CourseNames which should be an array of size: 20;
A member function called addCourse that accepts a const char* and adds the course name to the m_CourseNames array. The function should return the bool value true if it is able to add the course (the array is not full), otherwise return false.
A constructor that properly initializes the Person base class and any member variables of the Student class. It does not have to accept an array of course names.
Overload the << operator to print information about the student. Output should be in the format:
Jack: Age 33, Courses: CSC1001 CSC111
Part 2:
Write a main() function that does the following
Ask the user for a person’s name and age
Ask if the person is a student
If they are not a student create a Person object
If they are a student create a Student object
If they are a student ask for the course names the student is taking (you can do this however you want). Let the user know if their m_CourseNames is full if they try to add a course but there is no room in the array.
After input is complete, print the Student or Person object that was created using the << operator.
Person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <ostream>
class Person{
public:
Person ();
Person (const char* name, int age);
const char* getName () const;
void setName (const char* name);
int getAge () const;
void setAge (int age);
friend std::ostream& operator <<(std::ostream& os, const Person& person);
protected:const char* m_Name; int m_Age;
};#endif // PERSON_H_
Person.cpp
#include "Person.h"
using namespace std;
Person::Person() : m_Name(""), m_Age(0) {}
Person::Person(const char *name, int age) : m_Name(name), m_Age(age) {}
const char* Person::getName () const {
return m_Name; }
void Person::setName(const char* name) {
m_Name = name; }
int Person::getAge () const {
return m_Age; }
void Person::setAge (int age) {
m_Age = age; }
std::ostream &operator<<(std::ostream &os, const Person &person) {
os << "Name " << person.m_Name << " Age: " << person.m_Age << endl;
return os; }
Explanation / Answer
Person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <ostream>
class Person{
public:
Person ();
Person (const char* name, int age);
const char* getName () const;
void setName (const char* name);
int getAge () const;
void setAge (int age);
friend std::ostream& operator <<(std::ostream& os, const Person& person);
protected:const char* m_Name; int m_Age;
};#endif // PERSON_H_
--------------------------------------------------------------------------------------------------------
#include "Person.h"
using namespace std;
Person::Person() : m_Name(""), m_Age(0) {}
Person::Person(const char *name, int age) : m_Name(name), m_Age(age) {}
const char* Person::getName () const {
return m_Name;
}
void Person::setName(const char* name) {
m_Name = name;
}
int Person::getAge () const {
return m_Age;
}
void Person::setAge (int age) {
m_Age = age;
}
std::ostream &operator<<(std::ostream &os, const Person &person) {
os << "Name " << person.m_Name << " Age: " << person.m_Age << endl;
return os;
}
------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Student: public Person //here person class inherited
{
public: //method protypes
void addCourse(const char* courseName);
int getCoursesCapacity( void );
private: //varaables declared
const char* m_CourseNames[20];
int index=0;
};
void Student::addCourse( const char* courseName) //method to add course name
{
m_CourseNames[index] = courseName;
index++;
}
int Student::getCoursesCapacity( void ) //to get capacity
{
return index+1;
}
// Main function for the program
int main( )
{
char name[256];
char courseName[256];
int age;
//reading data from user
out<<"Enter name: ";
cin.getline(name,256);
cout<<" Enter Age: ";
cin>>age;
int temp=0;
cout<<" Enter 1 if student :";
cin>>temp;
if(temp == 1){ //create student object
Student student;
student.setName(name);
student.setAge(age);
int courses;
cout<<"How many courses you want to add: ";
cin>>courses;
if(student.getCoursesCapacity() < 20){
for(int i=0;i<courses;i++){
cout<<" Enter course name: ";
cin.getline(courseName,256);
student.addCourse(courseName);
}
}
else{
cout<<"Capacity filled!!";
}
cout<<" "<<Student;
}
else{ //create person object
Person Person(name,age);
cout<<" "<<person;
}
return 0;
}