Code From Class 2026/03/23
#include<iostream>
struct SN{
public:
double c;
SN* aNext;
};
SN* push(SN* aOT, double x){
SN* aNT=new SN;
aNT->c=x;
aNT->aNext=aOT;
return aNT;
}
SN* pop(SN* aOT){
if(aOT==nullptr){return nullptr;}
SN* aNT=aOT->aNext;
delete aOT;
return aNT;
}
SN* copyStack(SN* aOT){
if(aOT==nullptr){return nullptr;}
SN* aNT=new SN;
aNT->c=aOT->c;
aNT->aNext=copyStack(aOT->aNext);
return aNT;
}
class CCAccount{
private:
double balance;
SN* aTT;
public:
std::string cName;
CCAccount();
CCAccount(const CCAccount& );
CCAccount& operator=(const CCAccount& );
void printStatement() const;
double getBalance() const;
void addCharge(double);
~CCAccount();
};
CCAccount::CCAccount(){
std::cout<<"DC "<<this<<"\n";
balance=0;cName="NoNameYet";
aTT=nullptr;
}
CCAccount::CCAccount(const CCAccount& copyFrom){
std::cout<<"CC Making: "<<this<<" from "<<©From<<"\n";
cName=copyFrom.cName;
balance=copyFrom.balance;
aTT=copyStack(copyFrom.aTT);
}
CCAccount& CCAccount::operator=(const CCAccount& copyFrom){
std::cout<<"CA Copying to: "<<this<<" from "<<©From<<"\n";
if(©From==this){return *this;}
cName=copyFrom.cName;
balance=copyFrom.balance;
while(aTT!=nullptr){aTT=pop(aTT);}
aTT=copyStack(copyFrom.aTT);
return *this;
}
void CCAccount::printStatement() const{
std::cout<<cName<<" owes "<<balance<<". Transactions: ";
SN* aRunner=aTT;
while(aRunner!=nullptr){
std::cout<<aRunner->c<<" ";
aRunner=aRunner->aNext;
}
std::cout<<"\n";
}
double CCAccount::getBalance() const{
return balance;
}
void CCAccount::addCharge(double x){
balance+=x;
aTT=push(aTT,x);
}
CCAccount::~CCAccount(){
while(aTT!=nullptr){aTT=pop(aTT);}
}
int main(){
CCAccount x;
x.cName="Bart";
x.addCharge(15.0);x.addCharge(7.2);x.addCharge(3.3);
std::cout<<"Address of x is "<<&x<<"\n";
CCAccount y;
std::cout<<"Address of y is "<<&y<<"\n";
CCAccount z;
std::cout<<"Address of z is "<<&z<<"\n";
std::cout<<"Test 1: (z=y)=x"<<"\n";
(z=y)=x;
std::cout<<"Test 2: z=(y=x)"<<"\n";
z=(y=x);
std::cout<<"Test 3: z=y=x"<<"\n";
z=y=x;
return 0;
}