Constructors and Inheritance Problem

Hi, I'm trying to create a Saving Account derived from a Base Account using only setters and getters. I'm replicating what I have in the Base Account because I don't know which other way to create the Saving Account by using the setters/getters. Now I'm facing a problem with the Constructor in Line 79 that I don't know how to resolve. Thanks!


↓Code↓


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Account {
private:
int accountNumber;
double balance;
string fullName;
int phoneNumber;

public:
Account(int accountNumber, int phoneNumber, string fullName, double balance = 0.0);
int getAccountNumber() const;
int getPhoneNumber() const;
string getFullName() const;
double getBalance() const;
void setBalance(double balance);
void deposit(double amount);
void withdraw(double amount);
void print() const;
};


Account::Account(int no, int p, string fn, double b) : accountNumber(no), phoneNumber(p), fullName(fn), balance(b) { }

int Account::getAccountNumber() const
{
return accountNumber;
}

double Account::getBalance() const {
return balance;
}

void Account::setBalance(double b) {
balance = b;
}

string Account::getFullName() const {
return fullName;
}

int Account::getPhoneNumber() const {
return phoneNumber;
}


void Account::deposit(double amount) {
balance += amount;
}


void Account::withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
}
else {
cout << "Amount withdrawn exceeds the current balance!" << endl;
}
}


void Account::print() const {
cout << fixed << setprecision(2);
cout << "A/C no: " << accountNumber << " A/C Name: " << fullName << " A/C Phone Number: " << phoneNumber << " Balance=$" << balance << endl;
}

class Savings : public Account {
private:
double interestrate;

public:
Savings(double interestrate);
void setInterestRate(double interestrate);
};

Savings::Savings(double rate) : interestrate(rate) {} // No default constructor exists for class "Account"/'Account':no appropiate default constructor available

void Savings::setInterestRate(double rate) {
interestrate = rate;
}


int main() {

Account a1(8111, 78611111, "Juan Enrique", 99.99);
a1.print();
a1.setBalance(500);
a1.deposit(20);
a1.withdraw(10);
a1.print();

return 0;
};

The class Account has a single constructor that requires to pass three parameters. So either you provide these parameter to the constructor within the Savings constructor like so:

Savings::Savings(double rate) : Account{1, 2, ""}, interestrate(rate) {}

or you provide a constructor for Account that does not require any paramter.

or something like this:

Savings::Savings(double rate, int accountNumber, int phoneNumber, string fullName, double balance = 0.0) : Account{accountNumber, phoneNumber, fullName, balance}, interestrate(rate) {}
Thanks for your comment, Coder777. Even though I tried both of your solutions, I'm still getting more errors. When you mention, "The class Account has a single constructor that requires to pass three parameters. So either you provide these parameter to the constructor within the Savings constructor like so:" You refer to this "Savings::Savings(double rate) : Account{ int no, int p, string fn, double b }, interestrate(rate) {}"?
When posting code, please use code tags so that the code is readable:

[code]
Code goes here
[/code]


Maybe something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <iomanip>
#include <string>

class Account {
protected:
	int accountNumber {};
	double balance {};
	std::string fullName;
	int phoneNumber {};

public:
	Account() = default;
	Account(int accountNumber, int phoneNumber, const std::string& fullName, double balance = 0.0);
	int getAccountNumber() const;
	int getPhoneNumber() const;
	std::string getFullName() const;
	double getBalance() const;

	void setBalance(double balance);
	void deposit(double amount);
	void withdraw(double amount);
	void print() const;
};

Account::Account(int no, int p, const std::string& fn, double b) : accountNumber(no), phoneNumber(p), fullName(fn), balance(b) {}

int Account::getAccountNumber() const {
	return accountNumber;
}

double Account::getBalance() const {
	return balance;
}

void Account::setBalance(double b) {
	balance = b;
}

std::string Account::getFullName() const {
	return fullName;
}

int Account::getPhoneNumber() const {
	return phoneNumber;
}

void Account::deposit(double amount) {
	balance += amount;
}

void Account::withdraw(double amount) {
	if (amount <= balance)
		balance -= amount;
	else
		std::cout << "Amount withdrawn exceeds the current balance!\n";
}

void Account::print() const {
	std::cout << std::fixed << std::setprecision(2);
	std::cout << "A/C no: " << accountNumber << " A/C Name: " << fullName << " A/C Phone Number: " << phoneNumber
		<< " Balance= $" << balance;
}

class Savings : public Account {
private:
	double interestrate {};

public:
	using Account::Account;

	//Savings(double interestrate);
	void setInterestRate(double interestrate);
	void print() const;
};

//Savings::Savings(double rate) : interestrate(rate) {}

void Savings::setInterestRate(double rate) {
	interestrate = rate;
}

void Savings::print() const {
	Account::print();
	std::cout << std::fixed << std::setprecision(2);
	std::cout << " Interest rate " << interestrate;
}

int main() {
	Savings a1(8111, 78611111, "Juan Enrique", 99.99);

	a1.print();
	std::cout << '\n';
	a1.setBalance(500);
	a1.deposit(20);
	a1.withdraw(10);
	a1.print();
	std::cout << '\n';
	a1.setInterestRate(3);
	a1.print();
	std::cout << '\n';
};



A/C no: 8111 A/C Name: Juan Enrique A/C Phone Number: 78611111 Balance= $99.99 Interest rate 0.00
A/C no: 8111 A/C Name: Juan Enrique A/C Phone Number: 78611111 Balance= $510.00 Interest rate 0.00
A/C no: 8111 A/C Name: Juan Enrique A/C Phone Number: 78611111 Balance= $510.00 Interest rate 3.00

Refers to this:
1
2
3
4
class Account {
public:
    Account( int, int, string, double = 0.0 );
};

If you create class without any constructor:
1
2
struct Sample {
};

then compiler adds default constructor:
1
2
3
struct Sample {
    Sample() {}
};

However, your Account has custom constructor and therefore no default is added.
You can add it explicitly:
1
2
3
4
5
class Account {
public:
    Account() : accountNumber{0}, balance{0.0}, phoneNumber{0} {}
    Account( int, int, string, double = 0.0 );
};

But account with such info makes no sense. It is better to have only one constructor that forces user to supply data on initialization:
1
2
3
4
5
6
7
8
9
class Savings : public Account {
public:
  Savings(double rate, int accountNumber, int phoneNumber, string fullName, double balance = 0.0);
};

Savings::Savings(double rate, int accountNumber, int phoneNumber, string fullName, double balance)
: Account{accountNumber, phoneNumber, fullName, balance},
  interestrate{rate}
{}
Last edited on
Great, I understand now everything. Thanks for taking your time and dedication to explain keskiverto. So, in theory, having just one constructor and defining it at the last derivated class is the best option for this case. And seeplus, thank you as well for your answer; believe me, I wanted to post the code as readily as you did, but I don't have an idea of how to do it. Anyways I swear next time, ill find a way to do it, or if someone is so kind to tell me, that would be awesome. Thanks for the help!
For code tags, see https://www.cplusplus.com/articles/jEywvCM9/

On "just one constructor", the Account has one and the Savings has one.
Thanks.
My generic response on using code tags:
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

As mentioned there are tags other than just code tags that can be useful. Quote tags, for example. Or output tags.
1
2
3
4
5
6
7
8
9
10
// helloworld.cpp

import <iostream>;

int main()
{
   std::cout << "Hello, World!" << std::endl;

   return 0;
}
Hello, World!
Topic archived. No new replies allowed.