0 Replies - 560 Views - Last Post: 20 October 2010 - 10:46 AM Rate Topic: -----

#1 infanity  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 27-April 09

Downcast & Linked List Help

Posted 20 October 2010 - 10:46 AM

Hi guys. I am working on a project due immediately and i am stuck.

Okay so the code has two lists, posts and plates, and now changed to a pointer to geometric solid. Removed list<plate> and list <post> and now list<geometric_solid*>. A plate and post has its attributes which are height, width etc.

//list<plate> plates;
//list<post> posts;
list<geometric_solid*> solids;

A plate is a geometric solid
A post is a geometric solid.


Here is the .h file
#include <list>
#include "commons.h"
#include "plate.h"
#include "post.h"

using namespace std;

class part
{
 private:
  //list<plate> plates;
  //list<post>  posts;
  list<geometric_solid*> solids;
 public:
  void add_plate(float t, float w, float l, unit_enum u);
  void add_post(float r, float h, unit_enum u);
  void save_to_file(char* file_name);
  void restore_from_file(char* file_name);
  void debug_print_solids();
};




Now here is the problem. Now that I removed both lists and now its in one, now the program has to get the value of what the pointer is pointing to, it has to check which it is, plate or post. Then, downcast and print out. I need to know where the downcast code has to be included. Im not sure if its after

fout.open(file_name);
if( fout.is_open() )

before the program gets the attributes of the object. And also would it be dynamic_cast or static_cast. HELP!!!!

Here is the .cc file

#include <iostream>
#include <fstream>
#include "part.h"
#include "plate.h"
#include "post.h"
#include "oracle.h"
#include "geometric_solid.h"

using namespace std;

void part::add_plate(float t, float w, float l, unit_enum u)
{
  dimension x(t,u), y(w,u), z(l,u);
  plate     p(x, y, z);
  //plates.push_back(p);
  solids.push_back(p);
}

void part::add_post(float r, float h, unit_enum u)
{
  dimension x(r,u), y(h,u);
  post      p(x, y);
  //posts.push_back(p);
  solids.push_back(p);
}

void part::save_to_file(char* file_name)
{
  ofstream fout;

  fout.open(file_name);
  if( fout.is_open() )
    {
      //list<plate>::iterator which_plate;
      list<geometric_solid*>::iterator which_plate;
      //for(which_plate=plates.begin(); which_plate!=plates.end(); which_plate++)
      for(which_plate=solids.begin(); which_plate!=solids.end(); which_plate++)
	{
	  fout << "plate ";
	  fout << which_plate->get_thickness().get_value() << ' ';
	  fout << oracle::unit_enum_to_string(which_plate->get_thickness().get_unit()) << ' ';
	  fout << which_plate->get_width().get_value() << ' ';
	  fout << oracle::unit_enum_to_string(which_plate->get_width().get_unit()) << ' ';
	  fout << which_plate->get_length().get_value() << ' ';
	  fout << oracle::unit_enum_to_string(which_plate->get_length().get_unit()) << endl;
	}

      //list<post>::iterator which_post;
      list<geometric_solid*>::iterator which_post;
      //for(which_post=posts.begin(); which_post!=posts.end(); which_post++)
      for(which_post=solids.begin(); which_post!=solids.end(); which_post++)
	{
	  fout << "post ";
	  fout << which_post->get_radius().get_value() << ' ';
	  fout << oracle::unit_enum_to_string(which_post->get_radius().get_unit()) << ' ';
	  fout << which_post->get_height().get_value() << ' ';
	  fout << oracle::unit_enum_to_string(which_post->get_height().get_unit()) << endl;
	}
    }
  fout.close();
}

void part::restore_from_file(char* file_name)
{
  ifstream   fin;
  string     line;
  string     plate_or_post, unit1, unit2, unit3;
  double     value1, value2, value3;
  dimension  dim;
  plate      plate_to_add;
  post       post_to_add;

  fin.open(file_name);
  while( !fin.eof() )
    {
      fin >> plate_or_post >> value1 >> unit1 >> value2 >> unit2;
      if( fin.eof() )      // could be done differently.
      	break;
      if( plate_or_post.compare("plate")==0 )
	fin >> value3 >> unit3;

      if( plate_or_post.compare("plate")==0 )
	{
	  dim.set_value(value1);
	  dim.set_unit(oracle::string_to_unit_enum(unit1));
	  plate_to_add.set_thickness(dim);

	  dim.set_value(value2);
	  dim.set_unit(oracle::string_to_unit_enum(unit2));
	  plate_to_add.set_width(dim);

	  dim.set_value(value3);
	  dim.set_unit(oracle::string_to_unit_enum(unit3));
	  plate_to_add.set_length(dim);

	  //plates.push_back(plate_to_add);
	  solids.push_back(plate_to_add);
	}
      else
	{
	  dim.set_value(value1);
	  dim.set_unit(oracle::string_to_unit_enum(unit1));
	  post_to_add.set_radius(dim);

	  dim.set_value(value2);
	  dim.set_unit(oracle::string_to_unit_enum(unit2));
	  post_to_add.set_height(dim);

	  //posts.push_back(post_to_add);
	  solids.push_back(post_to_add);
	}
    }
}

void part::debug_print_solids()
{
  //list<plate>::iterator which_plate;
  list<geometric_solid*>::iterator which_plate;
  //for(which_plate=plates.begin(); which_plate!=plates.end(); which_plate++)
  for(which_plate=solids.begin(); which_plate!=solids.end(); which_plate++)
    cout << "plate with id " << which_plate->get_id() << endl;

  //list<post>::iterator which_post;
  list<geometric_solid*>::iterator which_post;
  //for(which_post=posts.begin(); which_post!=posts.end(); which_post++)
  for(which_post=solids.begin(); which_post!=solids.end(); which_post++)
    cout << "post  with id " << which_post->get_id() << endl;
}



This post has been edited by infanity: 20 October 2010 - 10:54 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1