Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

inheritance to create two new classes

June 11, 2014 Posted by: Hassan Views: 1861

Make two classes that inherit from the Car class:   FreightCar and PassengerCar. Each class will need a default constructor, a copy constructor, and a constructor that has five parameters. Only one more function will be built in each class; all the rest will be inherited. No additional member data will be added.

The kind of cars for the three classes will be: 

Car:   business, maintenance, other 

FreightCar:   box, tank, flat, otherFreight 

PassengerCar:   chair, sleeper, otherPassenger 

Use an enum to keep the kind The output function can use the KIND_ARRAY values to print the string corresponding to the enum valueBuild a new member function setKind. in the Car::setUpCar function Change private to protected in the Car class only.Create setKind functions for the FreightCar and PassengerCar classes that are similar to the setKind function for the Car class, but with different values. Create two new global function named buildFreightCar and buildPassengerCar, similar to the buildCar function.

The file for this problem should contain (without headers, of course):

Type        order  ARR  number  kind         loaded destination
Car          car1  CN   819481  maintenance  false  NONECar          

car2  SLSF  46871 business     true   MemphisCar          

car3  AOK     156 tender       true   McAlesterFreightCar  

car4  MKT  123456 tank         false  Fort WorthFreightCar  

car5  MP    98765 box          true   Saint LouisFreightCar  

car6  SP   567890 flat         true   ChicagoFreightCar  

car7  GMO    7878 hopper       true   MobilePassengerCar

car8  KCS    7893 chair        true   Kansas CityPassengerCar

car9  PAPX    145 sleeper      true   TucsonPassengerCar

car10 GN      744 combine      false  NONE

Best Answer

Main class



// Inheritance to create two new classes.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include "Car.h"
#include "FreightCar.h"
#include "PassengerCar.h"
 
#include <fstream>
 
int main()
{
 
    string line;
    ifstream carsFile ("cars.txt");
    cout<<"Type    ARR     number   kind          loaded      destination\n"; 
    if (carsFile.is_open())
    {
        while (carsFile.good() )
        {
		          getline (carsFile,line);
				  char* cstr = new char [line.size()+1];
				  strcpy (cstr, line.c_str());
				  char* splitter= strtok(cstr,"|");
				  int count=0;
 
		          string Type;
				  string order;
                  string ARR;
                  int number;
                  string kind;
                  string loaded;
                  string destination;
 
                  while (splitter != NULL)
                  {
					  if(count==0){
					    Type=splitter;
 
					  }
					  if(count==1){
					    order=splitter;
 
					  }
					  if(count==2){
					    ARR=splitter;
 
					  }
					  if(count==3){
					    number=atoi(splitter);
 
					  }
					  if(count==4){
					    kind=splitter;
 
					  }
					  if(count==5){
					    loaded=splitter;
 
					  }
 
					  if(count==6){
					    destination=splitter;
 
					     count=-1;
					  }
                      splitter = strtok (NULL, "|"); 	
					  count++;
                  }	
 
				  if(strcmp(Type.c_str(),"Car")==0){
				     Car Cartemp(order,ARR,number,loaded,destination);
					 Cartemp.setKind(kind);
                    Cartemp.setUpCar();
 
				  }
				  if(strcmp(Type.c_str(),"FreightCar")==0){
				     FreightCar FreightCartemp(order,ARR,number,loaded,destination);
					 FreightCartemp.setKind(kind);
                     FreightCartemp.setUpCar();
				  }
				  if(strcmp(Type.c_str(),"PassengerCar")==0){
				     PassengerCar PassengerCartemp(order,ARR,number,loaded,destination);
					 PassengerCartemp.setKind(kind);
                     PassengerCartemp.setUpCar();
				  }
 
		}
        carsFile.close();//close file
	}
	else{ cout << "No such file\n\n";}
 
 
	//delay
	system("pause");
 
	return 0;
}



#include "StdAfx.h"
#include "Car.h"
#include <iomanip>
 
 
 
Car::Car(void)
{
	this->Type="";
    this->ARR="";
    this->number=0;
    this->loaded="";
    this->destination="";
}
 
Car::Car(const Car &obj){
	*this=obj;
}
Car::~Car(void)
{
}
 
Car::Car(string newType,string newARR,int newnumber,string newloaded,string newdestination){
     this->Type=newType;
     this->ARR=newARR;
     this->number=newnumber;
     this->loaded=newloaded;
     this->destination=newdestination;
}
void Car::buildCar(string newType,string newARR,int newnumber,string newloaded,string newdestination){
     this->Type=newType;
     this->ARR=newARR;
     this->number=newnumber;
     this->loaded=newloaded;
     this->destination=newdestination;
}
void Car::setUpCar(){
	cout<<this->Type<<"\t"<<this->ARR<<"\t"<<this->number<<"\t"<<left<<setw(15)<<KIND_ARRAY[this->kind]<<"\t"<<this->loaded<<"\t"<<"\t"<<this->destination<<"\n";
}
void Car::setKind(string kind){
	this->kind=other;
	if(strcmp(kind.c_str(),"business")==0){
		this->kind=business;
	}
	if(strcmp(kind.c_str(),"maintenance")==0){
		this->kind=maintenance;
	}
 
}
 
#pragma once
#include <iostream>
#include <string>
#include "Kind.h"
using namespace std;
 
class Car
{
protected:
	string Type;
	string ARR;
	int number;
	Kind kind;
	string loaded;
	string destination;
 
public:
	//default constructor
	Car(void);
	//a copy constructor
	Car(const Car &obj); 
	//constructor that has five parameters
	Car(string newType,string newARR,int newnumber,string newloaded,string newdestination);
	~Car(void);
	void buildCar(string newType,string newARR,int newnumber,string newloaded,string newdestination);
	void setKind(string kind);
	void setUpCar();
 
 
};

Other code in zip file

Output

Arrays

Download this example

Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe