creating an object of a class

I am getting a no default constructor error, not sure on what I should change to fix 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
  #include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include "HW3.h"
using namespace std;

int main() {
	LoanData obj;
	return 0;
}

class LoanData
{
	double beginningBalance, Payment, interestPaid, principlePaid, endingBalance;
private:
	double  Bal = 100000;
	double  n = 30;
	double	i = 6;
	double	A;
	double p = Bal;
public:

	LoanData(double p, double n, double i);
	void	MakePayment(double pay);
	void	PrintAmortizationSchedule() {
		printf("%lf	%lf %lf %lf", beginningBalance, interestPaid, principlePaid, endingBalance);
	}
	void     PrintPayOffTimeReport(double p, double i) {
		printf("%lf %lf", n, A);
	}
	
};


LoanData::LoanData(double p, double n, double i)
{
	i = (i / 100) / 12;
	n = n * 12;
	A = p * (i * (pow((1 + i), n)) / (pow((1 + i), n) - 1));
	Bal = p;
}

void LoanData::MakePayment(double pay)
{
	Bal = (i + 1)*Bal - pay;

}

void LoanData::PrintAmortizationSchedule()
{
	while (Bal > 0) {
		double p = Bal;
		if ((i + 1)*p > A) {
			Payment = A;
		}
		else {
			Payment = (i + 1)*p;
		}
			interestPaid = i*p;
			principlePaid = A - i*p;
			endingBalance = (i + 1)*p - A;
			beginningBalance = endingBalance;			
	}
}

void LoanData::PrintPayOffTimeReport(double p, double i)
{
	for (n = 1; n < 50; n++)
	{
		A = p * (i * (pow((1 + i), n)) / (pow((1 + i), n) - 1));
	}
}
Either:

1) Write a default constructor
2) When you create your LoanData objects, specify the correct arguments for your existing non-default constructor to be used.
The create an object of the class LoanData you need to pass 3 doubles to the constructor on line 9
A default constructor would look like this: LoanData(); and LoanData::LoanData() {}
@Thomas1965 a default constructor doesn't have to be empty.

In fact, if it is is empty, there's probably no point having one. You should probably initialise the state of your data members, at the very least.
MikeBoy, it was only an example.

A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter).

http://en.cppreference.com/w/cpp/language/default_constructor


Topic archived. No new replies allowed.