Skip to content

Commit e8f42e4

Browse files
committed
classes & inheritance
1 parent 32281e4 commit e8f42e4

6 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main(int argc, char const *argv[]) {
6+
7+
return 0;
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "Person.h"
2+
using namespace std;
3+
4+
Person::Person(const string &name, const string &idNum) {
5+
this->name = name;
6+
this->idNum = idNum;
7+
}
8+
9+
string Person::getName() const { return name; }
10+
string Person::getIdNum() const { return idNum; }
11+
12+
void Person::print() {
13+
cout << "ID " << idNum << ": " << name << endl;
14+
}
15+
16+
void Person::printNoNewLine() {
17+
cout << "ID " << idNum << ": " << name << " ";
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef PERSON_H
2+
#define PERSON_H
3+
4+
#include <iostream>
5+
#include <string>
6+
7+
using namespace std;
8+
9+
class Person {
10+
private:
11+
string name;
12+
string idNum;
13+
public:
14+
Person(const string& name, const string& idNum);
15+
16+
string getName() const;
17+
string getIdNum() const;
18+
19+
void print();
20+
void printNoNewLine();
21+
};
22+
#endif
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "Student.h"
2+
3+
Student::Student(const string& name, const string& idNum, const string& major) : Person(name, idNum) {
4+
this->major = major;
5+
}
6+
7+
void Student::changeMajor(const string& newMajor) {
8+
this->major = newMajor;
9+
}
10+
11+
void Student::print() {
12+
Person::printNoNewLine();
13+
cout << ", " << "Major: " << major << endl;
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef STUDENT_H
2+
#define STUDENT_H
3+
4+
#include "Person.h"
5+
6+
class Student : public Person {
7+
private:
8+
string major;
9+
int gradYear;
10+
11+
public:
12+
Student(const string& name, const string& idNum, const string& major);
13+
14+
void changeMajor(const string& newMajor);
15+
void print();
16+
};
17+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "Person.h"
2+
#include "Student.h"
3+
#include <iostream>
4+
5+
int main(int argc, char const *argv[]) {
6+
Person person("Jim Person", "12-231");
7+
person.print();
8+
9+
Student student("Jim Student", "13-576", "Chef");
10+
student.print();
11+
12+
return 0;
13+
}

0 commit comments

Comments
 (0)