displaying vector full of objects

I am trying to make my program print out the contents of a vector containing objects of class type Checking
i want it to print the balance of each element in the vector but when i try to use
cout<<checking[1]


all i get is nonsense characters

here is what i have so far
Main
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
#include "checking.h"
#include "savings.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
   vector < Checking * > checking(50) ;
vector < Savings *> savings(50);
int accNumS = 0, accNumC = 0;
double bal = 0, amt = 0;
while(accNumC < 3)
{
cout << "Enter balance for accont #" << accNumC+1 << ": ";
			cin >> bal;
              	checking[accNumC] = new Checking(accNumC+1,bal);
              	accNumC++;
              	
}              	
cout << checking[1]<<"\n";
cout << checking[2]<<"\n";
cout << checking[3]<<"\n";
    system("pause");
    return 0;
}


account.h(checking inherits from 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
// Class automatically generated by Dev-C++ New Class wizard

#ifndef ACCOUNT_H
#define ACCOUNT_H

// No description
class Account
{
	public:
		// class constructor
		Account(int, double);
		// class destructor
	   ~Account();
		void operator+=(double);
        void operator-=(double);
        
         void setAccountNum(int);
         int getAccountNum();
         void setbalance(double);
         double getbalance();
	private:
            int AccountNum;
            double balance;	
};

#endif // ACCOUNT_H 


account.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
/ Class automatically generated by Dev-C++ New Class wizard

#include "account.h" // class's header file

// class constructor
Account::Account(int accnum, double accbalance)
{
setAccountNum(accnum);                  
setbalance(accbalance);
}

void Account::setAccountNum(int num)
{
  AccountNum = num;
}
int Account::getAccountNum()
{
  return AccountNum;                      
}
void Account::setbalance(double sbalance)
{
    balance = sbalance;               
} 
double Account::getbalance()
{
  return balance;
}                   
                      
void Account::operator+=(double num)
{
   double accountbalance;
   accountbalance = getbalance();                        
   accountbalance += num;
   setbalance(accountbalance);

}

void Account::operator-=(double num)
{
   double accountbalance;
   accountbalance = getbalance();                        
   accountbalance -= num;
   setbalance(accountbalance);

}
                                                
// class destructor
Account::~Account()
{
	// insert your code here
}


checking.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Class automatically generated by Dev-C++ New Class wizard

#ifndef CHECKING_H
#define CHECKING_H

#include "account.h" // inheriting class's header file

// No description
class Checking : public Account
{
	public:
		// class constructor
		Checking(int, double);
		// class destructor
		~Checking();
};

#endif // CHECKING_H 


checking.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Class automatically generated by Dev-C++ New Class wizard

#include "checking.h" // class's header file

// class constructor
Checking::Checking(int num, double bal): Account(num, bal)
{
	// insert your code here
}

// class destructor
Checking::~Checking()
{
	// insert your code here
} 
Last edited on
closed account (zwA4jE8b)
cout<<checking[1] is outputting the address of the class object at 1.

your vector is made of class pointers. If you want to show what is being pointed to then use the dereference operator or the -> operator
Topic archived. No new replies allowed.