How do I use class objects within another class??

IMPORTANT! MY CODE I NEED HELP WITH IS IN THE COMMENTS,
I have come a bit further even but the code is still a mess


I have an assignment past due, and haven't read all the required chapters.
Even though this fact I have done the first part of the assignment.

My assigment is this;

"To manage their accounts with a bank in its computer system created classes Konto and Banken.

The class Konto should have the following attributes:
o number: account number
o holder: Account holder name
o Balance: account balance
o Interest rate: interest rate for the account

The class should have the following methods:
o Account: constructor
O & Print: prints all attributes
o ge_nummer: returns the account number
o ranteutbetalning: calculates the interest and adding it to the balance


The class Banken should have the following attributes:
o Accounts: an array of accounts (use class Konto above)
o antal_konton: the number of accounts in the array
The class should have the following methods:
o banking: constructor
o skriv_kontolista: prints a list of all the bank's accounts (account holder, account balance and interest rate)
o nytt_konto: make a new account at the end of the array
o ranteutbetalning: pays interest to all accounts
o sok_kontonr: searches for a given account number, which should be a parameter to the method. If the account is writing method out its attributes on the screen, otherwise a message is printed if the account is not available."

For this, a menu where the user can select:
Add a new account.
Print Account List
Interest Payment
Search Account
End




My problem begins with creating the structure the class Banken, and I can't read the 100+ pages that I have missed during these hours and still do the assignment.
Last edited on
Do you know how to create an array of int in your Banken class?

Creating an array of any User Defined Type like Konto is basically the same.
Do you mean like declaring int array[4] ?
Yes that will declare an array of int with a size of 4.

Yes but how do I declare an array of the class Konto in the class Banken
?

So that class Banken displays every object of the class Konto in an array?

Like the assignment said;
"The class Banken should have the following attributes:
Accounts: an array of accounts (use class Konto above)
antal_konton: the number of accounts in the array

The class should have the following methods:
banken: constructor
skriv_kontolista: prints a list of all the bank's accounts (account holder, account balance and interest rate)
nytt_konto: make a new account at the end of the array
ranteutbetalning: pays interest to all accounts
sok_kontonr: searches for a given account number, which should be a parameter to the method.
If the account is writing method out its attributes on the screen, otherwise a message is printed if the account is not available."
Last edited on
Can't anyone give me some advise atleast? Something clearer than the answer I already got?
Yes but how do I declare an array of the class Konto in the class Banken


1
2
3
4
5
6
class Banken
{
private:
  Konto  Konten[NumberOfNeededKonten];
};
Havent' come much further with this, can someone please help? Not sure if I am doing this right
The first topic post contains what requirements the program must meet so it's not confusing what I want and is trying to do.
Last edited on
Bump
Either you use std::vector/std::list or you add a member int KontonCount (or a similar name). With this count you are able to tell how many of the Konto objects are valid.

Removing: --KontonCount;
Adding: ++KontonCount;
The assignment requires arrays not vectors. I have read tons about using vectors instead but my teacher wants me to use array to first learn how to use arrays between classes.

Should I add this datamember to Konto Class and call it from Banken somehow or should I add this in Banken and then call it from Konto?

Thanks for the help btw
Last edited on
By the way, is this valid?

Just wrote it piece but not sure if it's really valid, didn't give me any intellisens or other errors but still
1
2
3
4
5
6
Class Banken
{	
private:	
int KontonCount;
string *antal_konton = stringConstIterator.toInt32[KontonCount];
}
Should I add this datamember to Konto Class and call it from Banken somehow or should I add this in Banken and then call it from Konto?
I guess that you mean 'KontonCount'. Banken is the container class for Konto, hence the count must be placed in Banken (where the Konto array is managed).

By the way, is this valid?
I have no idea what stringConstIterator.toInt32[KontonCount] does, but toInt32 doesn't look right for a type string. Plus: KontonCount does not have a valid value.
stringConstIterator.toInt32[KontonCount] Wasn't valid I have learned, I am still having serious issues with this but I have come a bit further, can' somebody look through this and help med, and if you add code please comment on what it's doing too, I have only studied C++ for approx 1 month and 3 weeks, many newbie misses and such.

Btw, the top post contains the objectives of my Assignment, both those I have done and those I have not.


Added some comments on variables so you can understand even though they are written in my language
Bank.h
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

#ifndef BankH
#define BankH

#include <iostream>
#include <string>



namespace Bank{
	

	

	class Banken
	{
	private:
		int KontonCount;
		std::string *antal_konton; //numer_of_accounts
		Konto *Konton[1000];      // array of empty account objects
	public:
		Banken();
		Banken(Konto *Konton[1000], std::string *antal_konton){}   //Banken constructor

		void Skrivut_Kontolista(std::string* _antal_konton, Konto* _Konton[])
		{
//Printout_Accountlist
		}	

		void ny_Kund(std::string _Konto_Nummer, std::string _Innehavare, double _Saldo, double _Rantesats, Konto *_Konton[])
		{
//new_Account/new_customer
		}
	};

	class Konto : public Banken
	{
	public:
		std::string Konto_Nummer;  //Account_Number
		std::string Innehavare;         //Account_Holder
		double Saldo;                       // Balance
		double Rantesats;  //  Interest, is defined in .cpp
	public:
		Konto();
		Konto(std::string _Konto_Nummer, std::string _Innehavare, double _Saldo, double _Rantesats)  //Konto constructor
		{
		};

		std::string get_Konto_Nummer() const  { return Konto_Nummer; }
		void set_Konto_Nummer(std::string set) { Konto_Nummer = set; }

		std::string get_Innehavare() { return Innehavare; }
		void set_Innehavare(std::string set) { Innehavare = set; }

			double get_Saldo() const { return Saldo; }
			void set_Saldo(double set) { Saldo = set; }

			double get_Rantesats() const { return Rantesats; }

			void skriv_ut();
			void Addera_Ranta_till_Saldo();
		
	};

}
#endif




Bank.cpp
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 "Bank.h"

namespace Bank{
	//Class Konto
	double berakna_Ranta = 3.3;



	Konto::Konto(std::string _Konto_Nummer, std::string _Innehavare, double _Saldo, double _Rantesats)
	{   // Konstruktor
		Konto_Nummer = _Konto_Nummer;
		Innehavare = _Innehavare;
		Saldo = _Saldo;
		Rantesats = _Rantesats;

	};

	Konto::Konto() : Rantesats(berakna_Ranta)
	{
		Konto_Nummer = "";
		Innehavare = "";
		Saldo = 0.0;
	}

	void Konto::skriv_ut() //Skriver ut värdena
	{
		std::cout << "Innehavare: " << Innehavare << std::endl;
		std::cout << "Kontonummer: " << Konto_Nummer << std::endl;
		std::cout << "Saldo: " << Saldo << std::endl;
		std::cout << "Ranta: " << Rantesats << std::endl;
	}

	void Konto::Addera_Ranta_till_Saldo()
	{
		Saldo = ((Rantesats / 100) + 1) * Saldo;
	}



	//Class Banken
	void Banken::Skrivut_Kontolista(std::string* _antal_konton, Konto* _Konton[1000])
	{	// Konstruktor
		std::string *antal_konton = _antal_konton;
		*Konton[1000] = *_Konton[1000];
	}

	// No destructor needed







	int main()
	{
		int KontonCount;
		std::string *antal_konton;
		Konto Konton[1000];

		std::cout <<
			"1. Lägg in ett nytt konto."  //Add new asccount
			"2. Skriv ut kontolista"        // Print account list
			"3. Ränteutbetalning"          // Balance after interest
			"4. Sök efter konto"            // Search for account
			"5. Avsluta"                        // Exit
			<< std::endl; //1.  

	}

	}

Last edited on
Topic archived. No new replies allowed.