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

#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)

#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.cpp

// 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

// 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

// 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
}

may be more code than you need to solve problem but i figured id post it all to be safe

In order to be able to print using << with a custom type (class), you need to overload the << operator of the std::ostream class. And since you are using some class inheritance, it is probably advisable to use a virtual print() function for printing the content of your class (the most derived one). You could do the following:

// In the declaration of the Account class:
class Account  {
  //...
  public:
    virtual ~Account(); //never forget to make the base-class destructor virtual.

    virtual void print(std::ostream& out) const;
  //..
};

// Then, in account.cpp:

void Account::print(std::ostream& out) const {
  out << "Account: " << num << " Balance: " << bal << std::endl; //or whatever way you want it to look.
};

// Then, if you want a custom printing for Checking, add in declaration of Checking:

class Checking : public Account {
  //...
  public:
    void print(std::ostream& out) const; //this overrides the base-class implementation.
};

// Then, in checking.cpp:

void Checking::print(std::ostream& out) const {
  out << "Checking "; //add some customization.
  Account::print(out); //call the base-class version of print().
};

//finally, somewhere like in account.h or in main.cpp:
// overload the << operator for a Account object
std::ostream& operator <<(std::ostream& out, const Account& obj) {
  obj.print(out); //call the virtual print function.
};

// And then, in main(), you will need to dereference the pointer before using <<.
int main() {
  //...
  cout << *(checking[0]) << endl; //remember that C++ indices start from 0, not 1.
  cout << *(checking[1]) << endl;
  cout << *(checking[2]) << endl;
  //..
};

I tried to do what you said
keep getting errors in account.h on line 14
variable or field print declared void
print declared as a virtual field
expected ; before ( token

Edited 5 Years Ago by bkoper16: n/a

oh
here is 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

Following what mike said,

// And then, in main(), you will need to dereference the pointer before using <<.
int main() {
  //...
  cout << *(checking[0]) << endl; //remember that C++ indices start from 0, not 1.
  cout << *(checking[1]) << endl;
  cout << *(checking[2]) << endl;
  //..
};

, you would need to call your getbalance function from *(checking[0]), since it gives you the first object, *(checking[1]) giving you the second object, and so on. Also, you can make your get functions const, since they don't change the state of your object.

This article has been dead for over six months. Start a new discussion instead.