第二次作业。

This commit is contained in:
unlockable
2023-03-07 09:21:44 +08:00
parent 0e46b18876
commit 14eed1ed79
4 changed files with 152 additions and 0 deletions

40
OOP/02/Exercise01.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include <iostream>
class Student {
private:
int ID, grade;
public:
Student() {
this->ID = 0;
this->grade = 0;
};
Student(int newID, int newGrade) : ID(newID), grade(newGrade){};
void display();
void set(int, int);
};
void Student::display() {
std::cout << "ID: " << this->ID << ", Grade: " << this->grade << std::endl;
}
void Student::set(int newID, int newGrade) {
this->ID = newID;
this->grade = newGrade;
}
int main() {
Student stu[5];
stu[0].set(21394,89);
stu[1].set(2394888, 100);
stu[2].set(299, 70);
stu[3].set(3999, 10);
stu[4].set(4999, 60);
stu[0].display();
stu[2].display();
stu[4].display();
return 0;
}

47
OOP/02/Optional02.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include <iostream>
#include <stdlib.h>
class Strings {
public:
Strings(char *s);
~Strings();
void Print();
void Set(char *s);
private:
int length;
char *str;
};
Strings::Strings(char *s) {
length = strlen(s);
str = (char *)malloc(length + 1);
strcpy(str, s);
}
Strings::~Strings() {
free(str);
}
void Strings::Print() {
std::cout << str << std::endl;
}
void Strings::Set(char *s) {
free(str);
length = strlen(s);
str = (char *)malloc(length + 1);
strcpy(str, s);
}
int main() {
Strings* str1 = new Strings("didig");
Strings* str2 = new Strings("dirir");
str1->Print();
str2->Print();
str1->Set("ifirk");
str1->Print();
return 0;
}

View File

@@ -0,0 +1,15 @@
class Employee {
private:
int individualEmpNo;
int grade;
int accumPay;
public:
Employee();
Employee(int no, int newGrade, int newAccumPay);
~Employee();
void printInfo();
void setEmpNo(int);
void setGrade(int);
void setAccumPay(int);
};

View File

@@ -0,0 +1,50 @@
#include <iostream>
#include "employee.h"
Employee::Employee() {
this->individualEmpNo = 0;
this->grade = 0;
this->accumPay = 0;
}
Employee::Employee(int no, int newGrade, int newAccumPay) {
this->individualEmpNo = no;
this->grade = newGrade;
this->accumPay = newAccumPay;
}
Employee::~Employee() {
std::cout << "欢迎使用,再见" << std::endl;
}
void Employee::printInfo() {
std::cout << "Employee No. " << this->individualEmpNo << ": grade " << this->grade << ", accumPay: " << this->accumPay << std::endl;
}
void Employee::setEmpNo(int newNo) {
this->individualEmpNo = newNo;
}
void Employee::setGrade(int newGrade) {
this->grade = newGrade;
}
void Employee::setAccumPay(int newAccumPay) {
this->accumPay = newAccumPay;
}
int main() {
Employee* staff[4];
staff[0] = new Employee();
staff[1] = new Employee(11343, 2, 2000);
staff[2] = new Employee(199, 1, 4000);
staff[3] = new Employee(59949, 4, 400);
for (int i = 0; i < 4; i++) {
staff[i]->printInfo();
}
delete staff[3];
return 0;
}