2 Replies - 391 Views - Last Post: 12 October 2009 - 06:18 PM Rate Topic: -----

#1 RyanK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-September 09

Basic Classes Help

Post icon  Posted 12 October 2009 - 03:50 PM

I need a default class constructor that sets all values to 0. I also need a constructor with a maxcars set to 200. I then need to display this constructor and let the users make changes to it. This is what I have so far.

// Implementation file for class Radio
#include <iostream>
#include "Train.h"
using namespace std;

// constructors:
Train::Train()
{
	GrainCars = 0;
	CoalCars = 0;
	TankCars = 0;
	MaxCars = 0;
	CarsLeft = 0;
	NumEngines = 0;
}
Train::Train(int MC, int CL) // sets maximum cars at start
{
	GrainCars = 0;
	CoalCars = 0;
	TankCars = 0;
	MaxCars = MC;
	CarsLeft = CL;
	NumEngines = 0;
}

// utility functions:
void Train::displayTrain()
{
	cout << "Total Grain Cars: " << GrainCars << endl;
	cout << "Total Coal Cars: " << CoalCars << endl;
	cout << "Total Tank Cars: " << TankCars << endl;
	cout << "Max Cars: " << MaxCars << endl;
	cout << "Engines Used: " << NumEngines << endl; 
}

// mutators:
bool Train::SetNumGrainCars(int G, int MaxCars)
{
	if ((G < 0) || (G > MaxCars))
		return false;
	GrainCars = G;
	return true;
}
bool Train::SetNumCoalCars(int C, int MaxCars)
{
	if ((C < 0) || (C > MaxCars))
		return false;
	CoalCars = C;
	return true;
}
bool Train::SetNumTankCars(int T, int MaxCars)
{
	if ((T < 0) || (T > MaxCars))
		return false;
	TankCars = T;
	return true;
}

// accessors:
int Train::GetNumGrainCars()
{
	return GrainCars;
}
int Train::GetNumCoalCars()
{
	return CoalCars;
}
int Train::GetNumTankCars()
{
	return TankCars;
}


// definition file for class Train
#ifndef TRAIN_H
#define TRAIN_H
#include <iostream>
using namespace std;

class Train
{
public:
	// constructors:
	Train();
	Train(int MC, int CL);

	// accessors:
	int getGrainCars();
	int getCoalCars();
	int getTankCars();

	// mutators:
	bool setGrainCars(int G);
	bool setCoalCars(int C);
	bool setTankCars(int T);

	// utility functions:
	void displayTrain();

private:
	int GrainCars;
	int CoalCars;
	int TankCars;
	int MaxCars;
};
#endif


Is This A Good Question/Topic? 0
  • +

Replies To: Basic Classes Help

#2 Enter_Kratos  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: Basic Classes Help

Posted 12 October 2009 - 04:20 PM

From

Quote

I then need to display this constructor and let the users make changes to it.
I gather you mean you want to alter the data set in the constructor after construction? You would not do this in another constructor as this would only create a new instance of your class with user submitted data, leaving the original instance as it is. You will need to write functions to set assign the values submitted to the existing class instance. You can access member methods and variables by using these operators "->" , "."
I'll let you research a bit yourself ;p
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,783
  • Joined: 20-September 08

Re: Basic Classes Help

Posted 12 October 2009 - 06:18 PM

This may help get you started ...

#include "train.cpp" // ok to compile like this ... just to get project started

int main()
{
    Train t( 200 );
    t.display();
    system("pause");
    
    cout << endl;
    
    int n = 50;
    if( t.setGrainCars( n ) ) cout << n << " Grain Cars added ok." << endl;
    if( t.setGrainCars( n ) ) cout << n << " Grain Cars added ok." << endl;
    n=2;
    if( t.setCoalCars( n ) )  cout << n << " Coal Cars added ok." << endl;
    if( t.setCoalCars( n ) )  cout << n << " Coal Cars added ok." << endl;
    n=55;
    if( t.setTankCars( 55 ) ) cout << n << " Tank Cars added ok." << endl;
    if( t.setTankCars( 55 ) ) cout << n << " Tank Cars added ok." << endl;

    t.display();
    system("pause");
}


// train.cpp

#include "Train.h"

// constructors:
Train::Train(int MC)
{
    GrainCars = 0;
    CoalCars = 0;
    TankCars = 0;
    MaxCars = MC;
}

// private member functions
int Train::getCars()
{
    return GrainCars + CoalCars + TankCars;
}
int Train::getRoom()
{
    return MaxCars-getCars();
}

// utility functions:
void Train::display()
{
    cout << "Total Grain Cars : " << GrainCars << endl;
    cout << "Total Coal Cars  : " << CoalCars << endl;
    cout << "Total Tank Cars  : " << TankCars << endl;
    cout << "Engines Needed   : " << getEngines() << endl;
    cout << "Max Cars         : " << MaxCars << endl;
    cout << "May add          : " << getRoom() << " cars." << endl;
}

// mutators:
bool Train::setGrainCars(int G)
{
    if(G < 0 || G > getRoom()) return false;

    GrainCars += G;
    return true;
}
bool Train::setCoalCars(int C)
{
    if(C < 0 || C > getRoom()) return false;

    CoalCars += C;
    return true;
}
bool Train::setTankCars(int T)
{
    if(T < 0 || T > getRoom()) return false;

    TankCars += T;
    return true;
}
bool Train::setMaxCars(int MC)
{
    if(MC < 0 || MC < getCars()) return false;

    MaxCars = MC;
    return true;
}

// accessors:
int Train::getGrainCars()
{
    return GrainCars;
}
int Train::getCoalCars()
{
    return CoalCars;
}
int Train::getTankCars()
{
    return TankCars;
}


// train.h // assume need 1 Engine for up to each 100 cars ...

#ifndef TRAIN_H
#define TRAIN_H

#include <iostream>
using namespace std;

class Train
{
public:
    // constructors:
    Train(int MC=0);

    // accessors:
    int getGrainCars();
    int getCoalCars();
    int getTankCars();
    int getMaxCars();

    // mutators:
    bool setGrainCars(int G);
    bool setCoalCars(int C);
    bool setTankCars(int T);
    bool setMaxCars(int M);

    // utility functions:
    void display();

private:
    int GrainCars;
    int CoalCars;
    int TankCars;
    int MaxCars;
    // private member functions:
    int getCars();
    int getRoom();
    int getEngines() { return getCars()/100 +1; }
};

#endif

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1