access darray data from struct

i am doing a program for my class to calculate loan payments.
i implemented my darray, struct of variables (principal, interest, length of loan) and now i am in process of creating the loan classes.

i just need to figure out how to pass the data that's stored in the struct and use it in the class.
--heres the requirements of which i am confused about...

2. Read the data from the input file and store it in a darray of type principals, where principals is a struct. The name of the file that contains the data is listed in positional parameter argv[1].
---I already have this DONE

3. Create a darray of type pointer to loan, where each element points to a loan (amortized and simple for each line of data). Notice the type of the darray is a base class pointer.

--so i created the darray, but i dont know what "darray of type pointer to loan" means...what's a base class pointer????

thanks in advance for your help.

.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef LOAN_H
#define LOAN_H
#include <iostream>

struct principal
{
	float principal, interest;
	int length;
};

class loan:public principal
{
public:
	float monthlyPayment();
};

#endif 



cpp file
1
2
3
4
5
6
7
#include "loan.h"

float monthlyPayment()
{
	//just testing return val..
	return (principal*interest);
}


i get this error:
1
2
3
4
RRFA5-4:pa6 alexandern17$ g++ *.cpp
loan.cpp: In function ‘float monthlyPayment()’:
loan.cpp:5: error: ‘length’ was not declared in this scope
RRFA5-4:pa6 alexandern17$ 



and if here's how im reading the data and storing into darray
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "pa6functions.h"
#include "loan.h"
#include "darray.h"

void readFile(const char* file, darray<principal>& d1)
{
	std::ifstream in;
	in.open(file);
	loan prin;
	do
	{
		in >> prin.principal >> prin.interest >> prin.length;
		d1.insert(prin);
		
	} while(!in.eof());
	
	std::cout << '\n' << prin.principal << ", " << prin.interest << ", " << prin.length << std::endl;
}
Base class pointer: pointer to the base class.

Looks like Classname* pointername.

Advantages: More secure, harder to take advantage of stack overflow protection (or rather lack thereof).
Disadvantages: Slightly slower.

When in doubt use this, you can also do more with it depending on your skill.

-Albatross
1
2
loan *simple = new simpleLoan;
loan *amortized = new amortizedLoan;


is this a base class pointer? if so..this is indeed useful and i understand this concept. but
now comes my initial problem...

how am i able to access struct variables from my loan class? is public inheritance possible for classes and structs?
if so...
how do i get the struct vars into my class so i can start manipulating them?
OMYAHHH...so my mistake was....
1
2
3
4
5
6
7
8
#include "loan.h"

float loan::montlyPayment()
//float monthlyPayment()
{
	//just testing return val..
	return (principal*interest);
}



im so dumb...
Topic archived. No new replies allowed.