第三次作业。

This commit is contained in:
unlockable
2023-03-08 20:54:29 +08:00
parent d4c9f154db
commit 244d8754e8
4 changed files with 141 additions and 14 deletions

66
OOP/03/Optional01.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include <iostream>
class Seller {
private:
static int totalCount;
static double totalPrice;
int sellerNumber;
int sellCount;
double singlePrice;
public:
Seller();
Seller(int sellerNum, int sellCount, double price);
int getSellerNumber();
int getSellCount();
double getSinglePrice();
static double calcAvg();
static double getTotal();
};
Seller::Seller() {
this->sellerNumber = 0;
this->sellCount = 0;
this->singlePrice = 0;
}
Seller::Seller(int sellerNum, int newSellCount, double price) {
this->sellerNumber = sellerNum;
this->sellCount = newSellCount;
this->singlePrice = price;
Seller::totalCount += newSellCount;
Seller::totalPrice += price * newSellCount;
}
int Seller::getSellerNumber() {
return this->sellerNumber;
}
int Seller::getSellCount() {
return this->sellCount;
}
double Seller::getSinglePrice() {
return this->singlePrice;
}
double Seller::getTotal() {
return Seller::totalPrice;
}
double Seller::calcAvg() {
return Seller::totalPrice / Seller::totalCount;
}
int Seller::totalCount = 0;
double Seller::totalPrice = 0;
int main() {
Seller* s1 = new Seller(101, 5, 23.5);
Seller* s2 = new Seller(102, 12, 24.5);
Seller* s3 = new Seller(103, 100, 21.5);
std::cout << "Total: " << Seller::getTotal() << std::endl;
std::cout << "Avg: " << Seller::calcAvg() << std::endl;
return 0;
}

55
OOP/03/Optional03.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include <iomanip>
#include <iostream>
class Date {
private:
int d, m, y;
public:
Date(int dd = 0, int mm = 0, int yy = 0);
Date(Date &D);
~Date();
void display();
};
Date::Date(int dd, int mm, int yy) : d(dd), m(mm), y(yy) {
std::cout << "Constructor called! Address = 0x" << std::hex << std::setw(8)
<< std::setfill('0') << this << std::endl;
}
Date::Date(Date &D) {
d = D.d;
m = D.m;
y = D.y;
std::cout << "Copy constructor called! Address = 0x" << std::hex
<< std::setw(8) << std::setfill('0') << this << std::endl;
}
Date::~Date() {
std::cout << "Destructor called! Address = 0x" << std::hex << std::setw(8)
<< std::setfill('0') << this << std::endl;
}
void Date::display() {
std::cout << "Displaying" << std::endl;
}
// Date func(Date A) {
// Date B(A);
// return B;
// }
// Date & func(Date A) {
// Date B(A);
// return B;
// }
Date func(Date A) {
return A;
}
int main() {
Date today;
today = func(today);
return 0;
}

View File

@@ -3,10 +3,11 @@ private:
int individualEmpNo;
int grade;
int accumPay;
static int empCount;
public:
Employee();
Employee(int no, int newGrade, int newAccumPay);
Employee(int newGrade, int newAccumPay);
~Employee();
void printInfo();
void setEmpNo(int);

View File

@@ -1,14 +1,18 @@
#include <iostream>
#include "employee.h"
#include <iostream>
int Employee::empCount = 0;
Employee::Employee() {
this->individualEmpNo = 0;
Employee::empCount++;
this->individualEmpNo = empCount;
this->grade = 0;
this->accumPay = 0;
}
Employee::Employee(int no, int newGrade, int newAccumPay) {
this->individualEmpNo = no;
Employee::Employee(int newGrade, int newAccumPay) {
Employee::empCount++;
this->individualEmpNo = Employee::empCount;
this->grade = newGrade;
this->accumPay = newAccumPay;
}
@@ -18,7 +22,8 @@ Employee::~Employee() {
}
void Employee::printInfo() {
std::cout << "Employee No. " << this->individualEmpNo << ": grade " << this->grade << ", accumPay: " << this->accumPay << std::endl;
std::cout << "Employee No. " << this->individualEmpNo << ": grade "
<< this->grade << ", accumPay: " << this->accumPay << std::endl;
}
void Employee::setEmpNo(int newNo) {
@@ -34,17 +39,17 @@ void Employee::setAccumPay(int 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);
Employee staff[20];
for (int i = 0; i < 4; i++) {
staff[i]->printInfo();
for (int i = 0; i < 20; i++) {
staff[i].setGrade(i + 1);
staff[i].setAccumPay((i + 1) * 10);
}
delete staff[3];
for (int i = 0; i < 20; i++) {
staff[i].printInfo();
}
return 0;
}