Skip to content

Commit 86a7b8c

Browse files
committed
Started Homework3
1 parent d4ae149 commit 86a7b8c

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

Homework3/Account.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include "Account.h"
4+
5+
Account::Account(const char iban[], int ownerId, double amount) : ownerId(ownerId), amount(amount) {
6+
strcpy(this->iban, iban);
7+
}
8+
9+
double Account::getBalance() const {
10+
return this->amount;
11+
}

Homework3/Account.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include <iostream>
3+
4+
class Account{
5+
public:
6+
Account(const char [] = "unspecified", int = 0, double = 0);
7+
8+
virtual void deposit(double) = 0;
9+
virtual bool withdraw(double) = 0;
10+
virtual void display() const = 0;
11+
12+
double getBalance() const;
13+
protected:
14+
char iban[34];
15+
int ownerId;
16+
double amount;
17+
};

Homework3/CurrentAccount.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
#include "CurrentAccount.h"
3+
4+
void CurrentAccount::deposit(double amountToDeposit){
5+
this->amount+=amountToDeposit;
6+
}
7+
8+
bool CurrentAccount::withdraw(double amountToWithdraw){
9+
if(this->amount - amountToWithdraw < 0)
10+
return false;
11+
12+
this->amount-=amountToWithdraw;
13+
return true;
14+
}
15+
16+
void CurrentAccount::display() const{
17+
std::cout<<"This is an account of type current account. It has IBAN: "<<this->iban<<"\nIt belong to a customer with id: "
18+
<<this->ownerId<<"\nIt has a current balance of "<<this->amount<<" eur.\n";
19+
}

Homework3/CurrentAccount.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include <iostream>
3+
#include "Account.h"
4+
5+
class CurrentAccount : public Account{
6+
void deposit(double);
7+
bool withdraw(double);
8+
void display() const;
9+
};

Homework3/Customer.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include "Customer.h"
4+
5+
Customer::Customer(int id, const char* name, const char* address) : id(id) {
6+
this->name = new char[strlen(name)+1];
7+
strcpy(this->name, name);
8+
this->address = new char[strlen(address)+1];
9+
strcpy(this->address, address);
10+
}
11+
12+
Customer::Customer(const Customer& other){
13+
copy(other);
14+
}
15+
16+
Customer::~Customer(){
17+
erase();
18+
}
19+
20+
Customer& Customer::operator=(const Customer& other){
21+
if(this!=&other){
22+
erase();
23+
copy(other);
24+
}
25+
26+
return *this;
27+
}
28+
29+
int Customer::getId() const{
30+
return id;
31+
}
32+
33+
const char* Customer::getName() const{
34+
return name;
35+
}
36+
37+
const char* Customer::getAddress() const{
38+
return address;
39+
}
40+
41+
void Customer::display() const{
42+
std::cout<<"This is a customer with id: "<<getId()<<"\nHe is called "<<getName()<<"\nThis customer's address is "<<getAddress()<<std::endl;
43+
}
44+
45+
void Customer::copy(const Customer& other){
46+
this->name = new char[strlen(other.name) + 1];
47+
strcpy(this->name, other.name);
48+
this->address = new char[strlen(other.address) + 1];
49+
strcpy(this->address, other.address);
50+
}
51+
52+
void Customer::erase(){
53+
delete[] name;
54+
delete[] address;
55+
}

Homework3/Customer.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include <iostream>
3+
4+
class Customer{
5+
public:
6+
Customer(int = 0, const char* = "no name specified", const char* = "no address specified");
7+
Customer(const Customer&);
8+
~Customer();
9+
Customer& operator=(const Customer&);
10+
11+
int getId() const;
12+
const char* getName() const;
13+
const char* getAddress() const;
14+
void display() const;
15+
16+
private:
17+
int id;
18+
char* name;
19+
char* address;
20+
void copy(const Customer&);
21+
void erase();
22+
};

0 commit comments

Comments
 (0)