Hi I'm new to c++ and trying to learn something. maybe it can't be done. but I want to:
create an Employee class that records all employees, and their information (see my code below).
Then I want to make a templated class for a linked list. inside the private scope of this class, I will have a "T data" type. I want my constructor to instantiate an "Employee" every time a LinkedList object is created in the main. I hope that makes sense. I have my code working the way I set it up now. However, I'm not necessarily templating it the way I want.
template <class T>
class Employees { //Parent
private:
int EmployeeID;
double Paycheck;
int Hours;
double Payrate;
std::string Employeename;
public:
Employees(int ID, double Prate, std::string EName) {
EmployeeID = ID;
Payrate = Prate;
Employeename = EName;
Hours = 0; //our constructor can just give each Employee 0 hours.
Paycheck = 0.00;
}
Employees() {
EmployeeID = 0;
Payrate = 0.0;
Employeename = "N/A";
Hours = 0;
Paycheck = 0.00;
}
void setPaycheck() {
std::cout << std::fixed;
std::cout << std::setprecision(2);
double i = Payrate;
i = i * 100;
i = i * Hours;
i = i / 100;
Paycheck = i;
//so I'm not sure why I couldn't just cast the double, but the complier kept rounding down each time.
// I had to make a conversion of my decimal into a whole number and then go back and divide it to get the
// paycheck amount.
};
void setEMPname(std::string Name) {
Employeename = Name;
}
void setPayrate(double payrate) {
Payrate = payrate;
}
void setEMPid(int IDnum) {
EmployeeID = IDnum;
}
void setHOURS(int hours) {
Hours += hours;
}
int IDReturn() { return EmployeeID; }
int HoursReturn() { return Hours; }
double PaycheckReturn() { return Paycheck; }
double PayrateReturn() { return Payrate; }
std::string NameReturn() { return Employeename; }
};
template <class T>
class LinkedLists :Employees<T> { //child
friend class Employees<T>;
private:
////I want change the object below to "T data"
Employees<T> EmployeeObject; //contains all the employee information
LinkedLists<T>* nextlink;
LinkedLists<T>* prevlink;
public:
LinkedLists() {
EmployeeObject;
nextlink = nullptr;
prevlink = nullptr;
}
void setname(std::string Name) {
EmployeeObject.setEMPname(Name);
}
void setPrate(double payrate) {
EmployeeObject.setPayrate(payrate);
}
void setid(int IDnum) {
EmployeeObject.setEMPid(IDnum);
}
void setPayCheck() { EmployeeObject.setPaycheck(); }
LinkedLists<T>*& setLinkforward() {
return nextlink;
}
LinkedLists<T>*& setLinkbackward() {
return prevlink;
}
int idReturn() { return EmployeeObject.IDReturn(); }
double payrateReturn() { return EmployeeObject.PayrateReturn(); }
std::string nameReturn() { return EmployeeObject.NameReturn(); }
void AddHours(int Hours) { EmployeeObject.setHOURS(Hours); }
int ReturnHours() { return EmployeeObject.HoursReturn(); }
double ReturnPayCheck() {
return EmployeeObject.PaycheckReturn();
}
};
Related
I have been practicing creating bankaccount class with 3 derived classes.
I was wondering how I could get lumsum amount of all three derived classes into parent balance as how a bank statement shows total net amount of all the different kind of accounts related to a user account.
should I just make separate a function to reconcile all three with get_Balance func and setbalance? or with using inheritance there is something I can do without creating the func?
#include "pch.h"
#include <iostream>
#include <vector>
using namespace std;
class bankaccount {
int accountnum;
double balance;
public:
//bankaccount() { accountnum = 0; balance = 0.00; }
bankaccount(int newacctnum) :accountnum(newacctnum) { balance = 1000.00; }
int getacctnum() { return accountnum; }
double getbalance() { return balance; }
void set_balance(double x) { balance = x; }
void deposit(double deposits) { balance += deposits; }
void withdraw(double minus) { balance -= minus; }
virtual void runMonthly() { balance = balance; };
};
class CheckingAccount:public bankaccount {
int fee;
public:
CheckingAccount(int acctnum, int newfee) :bankaccount(acctnum) { fee = newfee; };
void runMonthly() { set_balance(getbalance() - fee); }
};
class SavingsAccount:public bankaccount {
double interest;
public:
//SavingsAccount() { interest = 0.00; }
SavingsAccount(int acctnum, double newint) :bankaccount(acctnum) { interest = newint; };
void runMonthly() { set_balance(getbalance() *(1.00+ interest)); }
};
class CreditCard :public bankaccount {
double cardint;
public:
//CreditCard() { cardint = 0.00; }
CreditCard(int acctnum, double newint) :bankaccount(acctnum) { cardint = newint/100; }
void runMonthly() { set_balance(-1*getbalance() *(1.00 + cardint)); }
};
class Bank {
vector<bankaccount*>thelist;
public:
void addAccount(bankaccount *accounts){
thelist.push_back(accounts);
}
void runMonthly() {
thelist[0]->deposit(60000);
for (int i = 0; i < thelist.size(); i++)
thelist[i]->runMonthly();
for (int i = 0; i < thelist.size(); i++) {
cout << thelist[i]->getacctnum() << " " << thelist[i]->getbalance() << endl;
}
}
};
int main() {
Bank b;
b.addAccount(new bankaccount(122552));
b.addAccount(new CheckingAccount(12345, 18)); //$18 monthly fee
b.addAccount(new SavingsAccount(12346, 0.02)); // 2% per month interest!
b.addAccount(new CreditCard(12347, 21.5)); // 21.5% interest rate!
b.runMonthly();
}
The key insight, in my opinion, is to realize that the Parent Account is not actually a parent account.
In my code below, each customer will have one BaseAccount instead of the parent account in your code. (Although for more advanced banking, multiple customers may have access to a single account.)
You cannot withdraw() or deposit from a BaseAccount. Instead, the BaseAccount just holds all the sub-accounts.
NOTE: This virtual inheritance is relatively slow. The code will end up doing a lot of pointer chasing. Better alternatives to runtime polymorphism (== virtual inheritance) are CRTP and std::variant. I did not want to deviate from the shown original code, too much.
Result: https://godbolt.org/z/WjWMe-
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <numeric>
#include <vector>
class BankAccount {
int accountnum;
double balance;
public:
BankAccount(int newacctnum) : accountnum(newacctnum), balance(0.0) {}
virtual ~BankAccount(){};
int getAccountNumber() { return accountnum; }
double getBalance() { return balance; }
void set_balance(double x) { balance = x; }
void deposit(double deposits) { balance += deposits; }
void withdraw(double minus) { balance -= minus; }
virtual void consolidateMonthly() = 0; // Better name than runMonthly
};
class CheckingAccount : public BankAccount {
int fee;
public:
CheckingAccount(int acctnum, int newfee) : BankAccount(acctnum) {
fee = newfee;
};
void consolidateMonthly() final { set_balance(getBalance() - fee); }
};
class SavingsAccount : public BankAccount {
double interest;
public:
SavingsAccount(int acctnum, double newint) : BankAccount(acctnum) {
interest = newint;
};
void consolidateMonthly() final {
set_balance(getBalance() * (1.00 + interest));
}
};
class CreditCard : public BankAccount {
double cardint;
public:
CreditCard(int acctnum, double newint) : BankAccount(acctnum) {
cardint = newint / 100;
}
void consolidateMonthly() final {
set_balance(-1 * getBalance() * (1.00 + cardint));
}
};
class BaseAccount { // NOTE: Purposefully not inheriting from a 'BankAccount' as
// deposit,withdraw do not make sense.
public:
BaseAccount(int account_number) : account_number_(account_number){};
void addSubsidiaryAccount(std::unique_ptr<BankAccount> account) {
sub_accounts_.push_back(std::move(account));
}
std::vector<std::tuple<int, double>> getSubAccountStatements() const {
std::vector<std::tuple<int, double>> ret;
ret.push_back({account_number_, getBalance()});
for (const auto &account : sub_accounts_) {
ret.push_back({account->getAccountNumber(), account->getBalance()});
}
return ret;
}
void consolidateMonthly() {
for (auto &account : sub_accounts_) {
account->consolidateMonthly();
}
}
int getAccountNumber() const { return account_number_; }
int getBalance() const {
return std::accumulate(
sub_accounts_.begin(), sub_accounts_.end(), 0.,
[](double balance, const std::unique_ptr<BankAccount> &acc) {
return balance + acc->getBalance();
});
}
BankAccount *getAccount(int account_number) const {
auto act =
std::find_if(sub_accounts_.begin(), sub_accounts_.end(),
[account_number](const std::unique_ptr<BankAccount> &ptr) {
return ptr->getAccountNumber() == account_number;
});
return act != sub_accounts_.end() ? act->get() : nullptr;
}
private:
const int account_number_;
std::vector<std::unique_ptr<BankAccount>> sub_accounts_;
};
class Bank {
std::vector<std::unique_ptr<BaseAccount>> account_list;
public:
void addAccount(std::unique_ptr<BaseAccount> account) {
account_list.push_back(std::move(account));
}
void runMonthly() const {
for (const auto &account : account_list) {
account->consolidateMonthly();
auto sub = account->getSubAccountStatements();
for (auto [acct, balance] :
sub) { // Requires "structured bindings" from C++17
std::cout << acct << ": " << balance << "\n";
}
}
}
void deposit(BankAccount *account, double amount) {
assert(account);
account->deposit(amount);
}
};
int main() {
Bank bank;
// First, let's have a base_account and add all the subsidiary accounts to
// this base account.
auto base_account = std::make_unique<BaseAccount>(122552);
base_account->addSubsidiaryAccount(
std::make_unique<CheckingAccount>(12345, 18));
base_account->addSubsidiaryAccount(
std::make_unique<SavingsAccount>(12346, 0.02)); // 2% per month interest!
base_account->addSubsidiaryAccount(
std::make_unique<CreditCard>(12347, 21.5)); // 21.5% interest rate!
auto checking_account_ptr = base_account->getAccount(12345);
if (!checking_account_ptr) {
std::cerr << "Failed to find account with ID 12345!\n";
return -1;
}
// Deposit 60k into the checking account(12345)
checking_account_ptr->deposit(60000);
bank.addAccount(std::move(base_account));
bank.runMonthly();
}
I'm designing a strategy pattern based on the following diagram:
I have to base the entire program on two things: the contents of the main function, and the Sale function getTotal() - which I was provided with;
Here's what I managed to write so far:
class Product {
private:
int code;
string type;
string description;
int price;
public:
Product(int code, string type, string description, int price): code{code}, type{type}, description{description}, price{price}{}
int getPrice() {
return price;
}
};
class SaleItem {
private:
Product product;
public:
int quantity;
Product getProduct() {
return product;
}
int getQuantity() {
return quantity;
}
};
class Sale {
public:
SaleItem s;
Sale(SaleItem s) : s{ s }{}
vector<SaleItem> items;
void additem(int n, Product p) {
items.push_back(s(n,p));
}
double getTotal();
};
class DiscountPolicy {
public:
virtual double getDiscount(const Sale* s, SaleItem si) = 0;
};
class CreditCardDiscount : public DiscountPolicy {
public:
virtual double getDiscount(const Sale* s, SaleItem si) {
return si.getQuantity() * si.getProduct().getPrice() * 0.02;
}
};
class NoDiscount : public DiscountPolicy {
public:
virtual double getDiscount(const Sale* s, SaleItem si) {
return si.getQuantity() * si.getProduct().getPrice() * 0.00;
}
};
double Sale::getTotal() {
double total = 0;
for (int i = 0; i < items.size(); i++) {
SaleItem sIt = items[i];
double price = sIt.getQuantity() * sIt.getProduct().getPrice();
//apply discount
price -= discountPolicy->getDiscount(this, sIt);
total += price;
}
return total;
}
int main() {
Sale s(new NoDiscount());
Product p1(1, "Apple", "food", 2.0);
Product p2(1, "TV", "electronics", 2000.0);
s.addItem(3, p1);
s.addItem(1, p2);
assert(s.getTotal() == 2006);
Sale s2(new CreditCardDiscount());
s2.addItem(3, p1);
s2.addItem(1, p2);
//total with discount for card
assert(s2.getTotal() == 1965.88);
}
I'm currently encountering two issues:
I can't wrap my head around designing the Sale class so that "Sale s(new NoDiscount());" and " Sale s2(new CreditCardDiscount());", from the main class, make sense.
I'm fairly certain there should be some code where the //apply discount comment is found in the getTotal() function, but I'm not sure how to implement it so that "price -= discountPolicy->getDiscount(this, sIt);" works.
The rest of the program should be implemented correctly, since I've seen similar designs, it's mainly the Sale function that I don't understand.
Appreciate the help!
Hello I do a shopping list. I have a class Customer and a class Item. My program work like this, I ask the name of the customer if the customer isn't in the database I add this customer, and if the customer is I print an error in order to say that the customer exists. After that I print a list of 10 products, and ask the customer to choose a product and increase the count of the specific Item for this customer.
My program don't give any error, I try do debug but I don't find the error, because the program doesn't increase the count of anything.
This is my function in my main:
void DisplayList(Item shopList[10], Customer& custom)
{
int choose_item = 1;
while (choose_item != 0)
{
cout << "The items you can buy are: (0 to exit)" << endl;
for (int i = 0; i < 10; i++)
{
cout << i + 1 << ". " << shopList[i].getName() << " " << shopList[i].getUnitPrice() << " x" << shopList[i].getCount() << endl;
}
cout << "What item you would like to buy, Input: ";
cin >> choose_item;
custom.addItem(shopList[choose_item - 1]);
}
}
My customer header:
class Customer
{
public:
Customer(string);
Customer(){}
void addItem(Item);
set<Item> getItems() const;
private:
string _name;
set<Item> _items;
};
My customer cpp
Customer::Customer(string name)
{
_name = name;
}
Customer::Customer(){ };
void Customer::addItem(Item SpecificItem)
{
set<Item>::iterator it;
if ((it = _items.find(SpecificItem)) != _items.end())
{
SpecificItem.setCount(SpecificItem.getCount() + 1);
}
else
{
_items.insert(SpecificItem);
}
}
set<Item> Customer::getItems() const
{
return _items;
}
My item header:
class Item
{
public:
Item(string, string, double);
~Item();
//get and set functions
string getName() const;
string getSerialNumber() const;
int getCount();
double getUnitPrice() const;
void setCount(int count);
private:
string _name;
string _serialNumber; //consists of 5 numbers
int _count; //default is 1, can never be less than 1!
double _unitPrice; //always bigger than 0!
};
My item cpp
#include "Item.h"
Item::Item(string name, string serial_number, double price) : _name(name), _serialNumber(serial_number), _unitPrice(price), _count(1)
{
};
Item::~Item(){};
string Item::getName() const
{
return _name;
}
string Item::getSerialNumber() const
{
return _serialNumber;
}
int Item::getCount()
{
return _count;
}
double Item::getUnitPrice() const
{
return _unitPrice;
}
void Item::setCount(int count)
{
_count = count;
}
I need to write the name, act# balance and address of the object that is stored in the vector, to a file.
I believe I have the program to push the objects into the vectors, but since they are vectors of object pointers I am having problems figure out how to call the object and print all 3 objects out.
Main.cpp
vector<Account*> accounts;
accounts.push_back(new Savings(new Person("Bilbo Baggins", "43 Bag End"), 1, 500, 0.075));
accounts.push_back(new Checking(new Person("Wizard Gandalf", "Crystal Palace"), 2, 1000.00, 2.00));
accounts.push_back(new Savings(new Person("Elf Elrond", "Rivendell"), 3, 1200, 0.050));
ofstream outFile;
outFile.open("accounts.txt");
if (outFile.fail())
{
cout << "\nYour file did not open, the program will now close!\n";
system("PAUSE");
return 0;
}
else
{
cout << "\nBINGO!!! It worked.\n\n";
system("PAUSE");
cout << "\n";
}
// New : Using a loop, send messages to each of the three Account objects to write themselves out to the file.
cout << "\nNow we are going to write the information to \"Accounts.txt\" \n\n";
system("PAUSE");
for (int i = 0; i < accounts.size(); i++) {
accounts[i]->writeAccount(outFile);
}
Account.h
#pragma once
#include <string>
#include <iostream>
#include "Person.h"
using namespace std;
// Account class - abstract/parent class
class Account
{
private:
int actNumber;
double actBallance;
Person PersonName;
public:
Account();
Account(int, double, Person*);
int getActNumber();
virtual double getActBallance();
string getName();
string getAdd();
void deposit(double);
void withdrawl(double);
virtual void writeAccount(ofstream&);
virtual void readAccount(ifstream&);
void testAccount(int i);
};
// Checking class: inherits from the Account class
class Checking : public Account
{
private:
double monthlyFee;
public:
Checking();
Checking(Person*, int, double, double);
void setMonthlyFee(double);
double getActBallance();
void writeAccount(ofstream&);
void readAccount(ifstream&);
};
// Savings class: inherits from the Account class
class Savings : public Account
{
private:
int interestRate;
public:
Savings();
Savings(Person*, int, double, double); // person, act#, Ballance, Interest Rate
void setInterestRate(double);
double getActBallance();
void writeAccount(ofstream&);
void readAccount(ifstream&);
};
Account.cpp
#include "Account.h"
#include <string>
using namespace std;
Account::Account()
{
actNumber = 0;
actBallance = 0.0;
}
Account::Account(int act, double bal, Person* name)
{
actNumber = act;
actBallance = bal;
}
int Account::getActNumber()
{
return actNumber;
}
double Account::getActBallance()
{
return actBallance;
}
string Account::getName()
{
return PersonName.getName();
}
string Account::getAdd()
{
return PersonName.getAddress();
}
void Account::deposit(double money)
{
actBallance += money;
}
void Account::withdrawl(double money)
{
actBallance -= money;
}
void Account::writeAccount(ofstream& output)
{
output << actNumber << "\n" << actBallance << "\n" << PersonName.getName() << "\n" << PersonName.getAddress() << endl;
}
void Account::readAccount(ifstream& output)
{
output >> actNumber;
output >> actBallance;
}
// Checking Account
Checking::Checking() {
monthlyFee = 0;
}
Checking::Checking(Person* per, int actNum, double bal, double interest) {
bal -= monthlyFee;
Account:Account(actNum, bal, per);
}
void Checking::setMonthlyFee(double fee) {
monthlyFee = fee;
}
double Checking::getActBallance() {
double ballance = Account::getActBallance();
return ballance = monthlyFee;
}
void Checking::readAccount(ifstream& output) {
int actNumber = Account::getActNumber();
int actBallance = Account::getActBallance() - monthlyFee;
output >> actNumber;
output >> actBallance;
}
void Checking::writeAccount(ofstream& output) {
int actNumber = Account::getActNumber();
int actBallance = Account::getActBallance();
output << actNumber << "\n" << actBallance << endl;
}
// Savings Account
Savings::Savings() {
interestRate = 0;
}
// Savings(Person, int, double, double) // person, act#, Ballance, Interest Rate
Savings::Savings(Person* per, int actNum, double bal, double interest) {
bal += (bal * interest);
Account:Account(actNum, bal, per);
}
void Savings::setInterestRate(double rate) {
interestRate = rate;
}
double Savings::getActBallance() {
double ballance = Account::getActBallance();
return ballance + (ballance * interestRate);
}
void Savings::readAccount(ifstream& output) {
double actBallance = Account::getActBallance();
int actNumber = Account::getActNumber();
actBallance += (actBallance * interestRate);
output >> actNumber;
output >> actBallance;
}
void Savings::writeAccount(ofstream& output) {
int actNumber = Account::getActNumber();
int actBallance = Account::getActBallance();
output << actNumber << "\n" << actBallance << endl;
}
I realize I am so far off... but I have been at this for HOURS and I can not figure out for the life of me, but to take the vector of object pointers and output the objects values.
Person.h
#pragma once
#include <string>
#include <fstream>
using namespace std;
class Person
{
private:
string name;
string address;
public:
Person();
Person(string a, string b);
string getName();
string getAddress();
void writePerson(ofstream&);
void readPerson(ifstream&);
};
Person.cpp
#include "Person.h"
#include <string>
using namespace std;
Person::Person()
{
name = "NAME";
address = "123 STREET";
}
Person::Person(string a, string b)
{
name = a;
address = b;
}
string Person::getName()
{
return name;
}
string Person::getAddress()
{
return address;
}
void Person::writePerson(ofstream& output)
{
output << name << " " << address << endl;
}
void Person::readPerson(ifstream& output)
{
output >> name;
output >> address;
Person(name, address);
}
Read again your course books on constructors: there are severe issues in all of your constructors. As a result, you don't initialize the object member variables, and you effectively end up printing lots of zeros and empty strings...
Firstly, for your base-class, you must initialize the person name. You should have written:
Account::Account(int act, double bal, Person* name)
: actNumber(act)
, actBallance(bal)
, PersonName(name)
{}
Secondly, for your derived classes, the initialisation of the base-class must be done in the initializer-list, not in the body of the ctor. Here is for exemple the correct definition for the Checking's ctor:
Checking::Checking(Person* per, int actNum, double bal, double interest)
: Account(actNum, bal, per)
, monthlyFee(-bal)
{}
Thirdly, be careful to initialize the member variables with the arguments of the ctor. You sometimes do the opposite and assign the ctor arguments with the (uninitialized) member variables.
BTW, Account is a base-class for a polymorphic hierarchy: thus, the Account destructor must be declared virtual.
I don't know how to call my class functions into printData(Testscore&) and readData(TestScore).
Also, could someone tell me why my Average() isn't being called to the main? I just learned about using static member variables and static member functions and was wondering if I am using them incorrectly.
The readData function:
Does not use copy constructor.
Reads all instance variables like the student's names and all their
grades.
Uses functions to store the variables, name, element of each grade of array pointed to private pquiz, and static member
grades of how many grades to read.
The printData function:
Writes the name and average grade of the quizzes.
Uses copy constructor.
This is my program so far:
#include <iostream>
#include <string>
using namespace std;
class TestScore {
private:
static int grades;
string name;
double *pquiz;
double average;
public:
TestScore();
~TestScore();
void setName(string);
static void setGrades(int);
void setPquiz(double *);
void setAverage(double);
string getName();
static int getGrades();
double getPquiz();
void readData(TestScore &);
void printData(TestScore);
double Average(double *, int);
static void Grade(int);
};
TestScore::TestScore() {
name="";
pquiz=new double[grades];
average=0;
}
void TestScore::setName(string name1) {
if(name1!="1") {
name=name1;
}
}
void TestScore::setPquiz(double *pquiz1) {
if(pquiz>=0) {
pquiz=pquiz1;
}
}
void TestScore::setGrades(int grades1) {
if(grades1>=0) {
grades=grades1;
}
}
void TestScore::setAverage(double average1) {
if(average1>=0) {
average=average1;
}
}
string TestScore::getName() {
return name;
}
int TestScore::getGrades() {
return grades;
}
double TestScore::getPquiz() {
return *pquiz;
}
double Average(double *pquiz, int grade) {
int count;
double total=0;
double average=0;
for(count=0; count<grade; count++) {
total+=pquiz[count];
}
average=total/grade;
return average;
}
void readData(TestScore&) {
}
void printData(TestScore) {
}
TestScore::~TestScore() {
delete [] pquiz;
pquiz=0;
}
int TestScore::grades=0;
void TestScore::Grade(int a) {
grades+=a;
}
int main() {
const int grades = 3;
const int students = 4;
TestScore exam;
string student;
int grade;
double *pquiz;
double average;
for(int i=0; i<students; i++) {
cout<<"Student "<<(i+1)<<": ";
cin>>student;
exam.setName(student);
cout<<endl;
for(int count=0; count<grades; count++) {
cout<<"Quiz "<<(count+1)<<": ";
cin>>pquiz[count];
exam.setPquiz(pquiz);
exam.getPquiz();
while(pquiz[count]<0) {
cout<<"Error, invalid test score, please try again."<<endl;
cout<<"Quiz "<<(count+1)<<": ";
cin>>pquiz[count];
}
}
exam.setAverage(average);
cout<<exam.getName()<<" average is "<<Average(pquiz, grade)<<endl<<endl;
}
readData(exam);
printData(exam);
return 0;
}
Don't use static anywhere, at least not for now. You have too many variables of the same name, scattered all over the place. Try to clean them up.
TestScore::TestScore()
{
name = "";
//pquiz = new double[grades];//#grades is undefined
pquiz = NULL;
average = 0;
}
grades is not defined yet, it could be zero, or it could be -817. You should just remove that line, or you can put something like pquiz = new double[10] that's if you are sure the number of quiz will not exceed 10.
TestScore::~TestScore()
{
if (pquiz) delete[] pquiz;
pquiz = NULL;
}
delete pquiz only if it is not NULL
int main() {
const int grades = 3;
const int students = 4;
TestScore exam;
string student;
int grade;
double *pquiz;
...
This is a different pquiz, it is a pointer which points to nothing, it doesn't really exist, you can't use it like that.