Skip to content

Instantly share code, notes, and snippets.

@hoangvvo
Last active October 4, 2023 09:03
Show Gist options
  • Save hoangvvo/51aa0b4fd70486a762b5f1e5b443d32e to your computer and use it in GitHub Desktop.
Save hoangvvo/51aa0b4fd70486a762b5f1e5b443d32e to your computer and use it in GitHub Desktop.
CO3001_OOP class exercise
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
string address;
public:
Person(string name, string address) {
this -> name = name;
this -> address = address;
}
string getName() {
return name;
}
string getAddress() {
return address;
}
void setAddress(string address) {
this -> address = address;
}
string toString() {
return "Person[name=" + name + ",address=" + address + "]";
}
};
class Student: public Person {
private:
string program;
int year;
double fee;
public:
Student(string name, string address, string program, int year, double fee): Person(name, address) {
this -> program = program;
this -> year = year;
this -> fee = fee;
}
string getProgram() {
return program;
}
void setProgram(string program) {
this -> program = program;
}
int getYear() {
return year;
}
void setYear(int year) {
this -> year = year;
}
double getFee() {
return fee;
}
void setFee(double fee) {
this -> fee = fee;
}
string toString() {
return "Student[" +
Person::toString() +
",program=" + program + ",year=" + to_string(year) + ",fee=" +
to_string(fee) + "]";
}
};
class Staff: public Person {
private:
string school;
double pay;
public:
Staff(string name, string address, string school, double pay): Person(name, address) {
this -> school = school;
this -> pay = pay;
}
string getSchool() {
return school;
}
void setSchool(string school) {
this -> school = school;
}
double getPay() {
return pay;
}
void setPay(double pay) {
this -> pay = pay;
}
string toString() {
return "Staff[" +
Person::toString() +
",school=" + school + ",pay=" + to_string(pay) + "]";
}
};
class SchoolBusinessTest {
public:
void main() {
Student arr[5] = {
Student("Nguyen Van A", "Ha Noi", "Computer Engineering", 2020, 63000000),
Student("Nguyen Van B", "Ho Chi Minh", "Computer Science", 2020, 65000000),
Student("Nguyen Van C", "Da Nang", "Cybersecurity", 2020, 67000000),
Student("Nguyen Van D", "Hue", "Image Processing and Computer Vision", 2020, 69000000),
Student("Nguyen Van E", "Hai Phong", "Applied Artificial Intelligence", 2020, 71000000)
};
for (int i = 0; i < 5; i++) {
arr[i].setYear(2023);
}
for (int i = 0; i < 5; i++) {
cout << arr[i].toString() << endl;
}
Staff staff = Staff("Nguyen Van F", "Ho Chi Minh", "HCMUT", 5500000);
staff.setAddress("Da Nang");
cout << staff.toString() << endl;
}
}
int main() {
SchoolBusinessTest schoolBusinessTest;
schoolBusinessTest.main();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
string address;
int id;
public:
Person(int id, string name, string address) {
this -> id = id;
this -> name = name;
this -> address = address;
}
string getName() {
return name;
}
string getAddress() {
return address;
}
void setAddress(string address) {
this -> address = address;
}
int getId() {
return id;
}
void setId(int id) {
this -> id = id;
}
string toString() {
return "Person[name=" + name + ",address=" + address + "]";
}
};
class Student: public Person {
private:
string program;
int year;
double fee;
public:
Student(int id, string name, string address, string program, int year, double fee): Person(id, name, address) {
this -> program = program;
this -> year = year;
this -> fee = fee;
}
string getProgram() {
return program;
}
void setProgram(string program) {
this -> program = program;
}
int getYear() {
return year;
}
void setYear(int year) {
this -> year = year;
}
double getFee() {
return fee;
}
void setFee(double fee) {
this -> fee = fee;
}
string toString() {
return "Student[" +
Person::toString() +
",program=" + program + ",year=" + to_string(year) + ",fee=" +
to_string(fee) + "]";
}
};
class Staff: public Person {
private:
string school;
double pay;
public:
Staff(int id, string name, string address, string school, double pay): Person(id, name, address) {
this -> school = school;
this -> pay = pay;
}
string getSchool() {
return school;
}
void setSchool(string school) {
this -> school = school;
}
double getPay() {
return pay;
}
void setPay(double pay) {
this -> pay = pay;
}
string toString() {
return "Staff[" +
Person::toString() +
",school=" + school + ",pay=" + to_string(pay) + "]";
}
};
class Course {
private:
string courseID;
string courseName;
string courseDescription;
int noCredit;
Student** studentList;
int studentCount;
Staff** staffList;
int staffCount;
public:
Course(string courseID, string courseName, string courseDescription, int noCredit) {
this -> courseID = courseID;
this -> courseName = courseName;
this -> courseDescription = courseDescription;
this -> noCredit = noCredit;
studentList = new Student*[100];
studentCount = 0;
staffList = new Staff*[100];
staffCount = 0;
}
void addStudent(Student* student) {
studentList[studentCount++] = student;
}
void removeStudent(int student_id) {
for (int i = 0; i < studentCount; i++) {
if (studentList[i] -> getId() == student_id) {
for (int j = i; j < studentCount - 1; j++) {
studentList[j] = studentList[j + 1];
}
studentCount--;
break;
}
}
}
void addStaff(Staff* staff) {
staffList[staffCount++] = staff;
}
void removeStaff(int staff_id) {
for (int i = 0; i < staffCount; i++) {
if (staffList[i] -> getId() == staff_id) {
for (int j = i; j < staffCount - 1; j++) {
staffList[j] = staffList[j + 1];
}
staffCount--;
break;
}
}
}
string toString() {
string result = "Course[courseID=" + courseID + ",courseName=" + courseName + ",courseDescription=" + courseDescription + ",noCredit=" + to_string(noCredit) + ",studentList=[";
for (int i = 0; i < studentCount; i++) {
result += studentList[i] -> toString();
if (i != studentCount - 1) {
result += ",";
}
}
result += "],staffList=[";
for (int i = 0; i < staffCount; i++) {
result += staffList[i] -> toString();
if (i != staffCount - 1) {
result += ",";
}
}
result += "]]";
return result;
}
}
class SchoolBusinessTest {
public:
void main() {
Student arr[5] = {
Student("Nguyen Van A", "Ha Noi", "Computer Engineering", 2020, 63000000),
Student("Nguyen Van B", "Ho Chi Minh", "Computer Science", 2020, 65000000),
Student("Nguyen Van C", "Da Nang", "Cybersecurity", 2020, 67000000),
Student("Nguyen Van D", "Hue", "Image Processing and Computer Vision", 2020, 69000000),
Student("Nguyen Van E", "Hai Phong", "Applied Artificial Intelligence", 2020, 71000000)
};
for (int i = 0; i < 5; i++) {
arr[i].setYear(2023);
}
for (int i = 0; i < 5; i++) {
cout << arr[i].toString() << endl;
}
Staff staff = Staff("Nguyen Van F", "Ho Chi Minh", "HCMUT", 5500000);
staff.setAddress("Da Nang");
cout << staff.toString() << endl;
}
}
int main() {
SchoolBusinessTest schoolBusinessTest;
schoolBusinessTest.main();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment