In: Computer Science
Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child class.
// File name: Person.h
// Person is the base, or parent for chapter11
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string fName;
string lName;
int areaCode;
int phone;
public:
Person();
Person(string, string);
void setFirst(string);
void setLast(string);
void setPhoneNumber(int, int);
string getFirstlast();
string getLastFirst();
string getPhoneNumber();
};
// Student is the child, or specialization, for class
chapter11
#pragma once
#include "Person.h"
class Student : public Person {
private:
int idNumber;
string major;
static int baseForID;
public:
Student();
Student(string, string);
void setMajor(string);
void printStudent();
};
// File name: Student.cpp
// Student is the child, or specialization, class for
chapter11
#include "Student.h"
// initialize the static classs variable to 20200000
// The default constructor initializes all strings to "" (the empty
string),
// and sets the id number for the student. The id number for a
student is the
// value of the static class variable. Once assigned, the value of
the static
// class variable must be incremented by 1
Student::Student()
{
}
// The parametrized constructor invokes the parent class'
methods to assign
// values to first name and last name.The id number for a student
is the
// value of the static class variable. Once assigned, the value of
the static
// class variable must be incremented by 1
Student::Student(string, string)
{
}
// set the value of major to that of the parameter
void Student::setMajor(string)
{
}
// prints the student information in the following format
// ID# idnumber lastName, firstName major
void Student::printStudent()
{
}
// Person is the base, or parent, class for chapter11
#include "Student.h"
int main() {
// create a new student object, student1, using the
default constructor
// assign the following values using the methods
for the class
// first name: Pedro last name: Picapiedra phone:
787-555-5555,
// major: Computer Science
// print the information for student1
// create a new student object, student2, using the
parametrized constructor
// use the following values as arguments:
// firt name: Pablo last name: Marmol
// assign the following values using the methods
for the class
// phone: 787-787-8787,
// major: Electrical Engineering
// print the information for student2
// create a new student object, student3, using the
parametrized constructor
// use the following values as arguments:
// firt name: Senor last name: Rajuela
// assign the following values using the methods
for the class
// phone: 787-755-5000,
// major: Computer Engineering
// print the information for student3
system("pause");
return 0;
// File name: Person.cpp
// Person is the base, or parent, class for chapter11
#include "Person.h"
// The default constructor initializes all strings to "" (the empty
string),
// and all integers to 0
Person::Person()
{
}
// The parametrized constructor initializes all strings to the
values in the parameters,
// and all integers to 0
Person::Person(string, string)
{
}
// Assigns the value of the parameter to fName; NO COUT's!
void Person::setFirst(string)
{
}
// Assigns the value of the parameter to lName; NO COUT's!
void Person::setLast(string)
{
}
// Assigns the values of the parameter to areaCode and phone; NO
COUT's!
// must verifiy that the area code value has only 3 digits, and
the
// phone number has only 7 digits
void Person::setPhoneNumber(int, int)
{
}
// returns a string in the format firstName lastName
string Person::getFirstlast()
{
return string();
}
// returns a string in the format lastName, firstName
string Person::getLastFirst()
{
return string();
}
// returns a string with the phone number
// the phone number should be in the following format
// (area code) xxx-xxxx
string Person::getPhoneNumber()
{
return string();
}
}
SOURCE CODE
//Person.h
// File name: Person.h
// Person is the base, or parent for chapter11
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string fName;
string lName;
int areaCode;
int phone;
public:
Person();
Person(string, string);
void setFirst(string);
void setLast(string);
void setPhoneNumber(int, int);
string getFirstlast();
string getLastFirst();
string getPhoneNumber();
};
//Person.cpp
// File name: Person.cpp
// Person is the base, or parent, class for chapter11
#include "Person.h"
// The default constructor initializes all strings to "" (the empty string),
// and all integers to 0
Person::Person()
{
fName = "";
lName = "";
areaCode = 0;
phone = 0;
}
// The parametrized constructor initializes all strings to the values in the parameters,
// and all integers to 0
Person::Person(string fname, string lname)
{
fName = fname;
lName = lname;
areaCode = 0;
phone = 0;
}
// Assigns the value of the parameter to fName; NO COUT's!
void Person::setFirst(string s)
{
fName = s;
}
// Assigns the value of the parameter to lName; NO COUT's!
void Person::setLast(string s)
{
lName = s;
}
// Assigns the values of the parameter to areaCode and phone; NO COUT's!
// must verifiy that the area code value has only 3 digits, and the
// phone number has only 7 digits
void Person::setPhoneNumber(int areacode, int phno)
{
if(areacode / 1000 != 0)
return;
if(phno / 10000000 != 0)
return;
areaCode = areacode;
phone = phno;
}
// returns a string in the format firstName lastName
string Person::getFirstlast()
{
return string(fName + lName);
}
// returns a string in the format lastName, firstName
string Person::getLastFirst()
{
return string(lName + ", " + fName);
}
// returns a string with the phone number
// the phone number should be in the following format
// (area code) xxx-xxxx
string Person::getPhoneNumber()
{
string str = "(";
str += to_string(areaCode);
str += ") ";
str += to_string(phone/10000);
str += "-";
str += to_string(phone%10000);
return str;
}
//Student.h
// Student is the child, or specialization, for class chapter11
#pragma once
#include "Person.h"
class Student : public Person {
private:
int idNumber;
string major;
static int baseForID;
public:
Student();
Student(string, string);
void setMajor(string);
void printStudent();
};
//Student.cpp
// File name: Student.cpp
// Student is the child, or specialization, class for chapter11
#include "Student.h"
// initialize the static classs variable to 20200000
int Student::baseForID = 20200000;
// The default constructor initializes all strings to "" (the empty string),
// and sets the id number for the student. The id number for a student is the
// value of the static class variable. Once assigned, the value of the static
// class variable must be incremented by 1
Student::Student()
{
major = "";
idNumber = baseForID;
baseForID++;
}
// The parametrized constructor invokes the parent class' methods to assign
// values to first name and last name.The id number for a student is the
// value of the static class variable. Once assigned, the value of the static
// class variable must be incremented by 1
Student::Student(string fname, string lname) : Person(fname, lname)
{
idNumber = baseForID;
baseForID++;
}
// set the value of major to that of the parameter
void Student::setMajor(string s)
{
major = s;
}
// prints the student information in the following format
// ID# idnumber lastName, firstName major
void Student::printStudent()
{
cout << "ID# " << idNumber << " " << getLastFirst() << " " << major << endl;
}
//main.cpp
#include "Student.h"
int main() {
// create a new student object, student1, using the default constructor
Student student1;
// assign the following values using the methods for the class
// first name: Pedro last name: Picapiedra phone: 787-555-5555,
// major: Computer Science
student1.setFirst("Pedro");
student1.setLast("Picapiedra");
student1.setPhoneNumber(787, 5555555);
student1.setMajor("Computer Science");
// print the information for student1
student1.printStudent();
// create a new student object, student2, using the parametrized constructor
// use the following values as arguments:
// firt name: Pablo last name: Marmol
Student student2("Pablo", "Marmol");
// assign the following values using the methods for the class
// phone: 787-787-8787,
// major: Electrical Engineering
student2.setPhoneNumber(787, 7878787);
student2.setMajor("Electrical Engineering");
// print the information for student2
student2.printStudent();
// create a new student object, student3, using the parametrized constructor
// use the following values as arguments:
// firt name: Senor last name: Rajuela
Student student3("Senor", "Rajuela");
// assign the following values using the methods for the class
// phone: 787-755-5000,
// major: Computer Engineering
student3.setPhoneNumber(787, 7555000);
student3.setMajor("Computer Engineering");
// print the information for student3
student3.printStudent();
system("pause");
return 0;
}
OUTPUT