OOPS Question

Hi Experts,
            This may be a walk in the park for many of you. I wanted to know how efficiently Object Oriented concepts can be used to the following requirement.
Problem
As a part of a larger system, a class
    Archive
is required that will accept whole number parameters to a method called
    Add
which collects the number for later use. The class Archive should also provide a method
    Show
that displays the collection of numbers on the standard console output.  Object Oriented Concepts and attention to detail should be demonstrated by the Class.

Even though the solution is a basic one, I want you to give attention to every detail and exhibit the possible Object Oriented Concepts giving attention to all the details.

Thank you very much in advance,
Curtis.
dinedineAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
Hi Kdo,

Sorry.  I thought you were asking how efficient it would be when written as OOP instead of a more traditional approach.

Most C++ libraries have their own collection or list classes.  You may want to use one of them or write your own.  And extending one of the classes may fit best.  Still, here's a foundation for what you'll probably want.


The Add() method does all of the heavy lifting.  Before coding it, you'll need to make some decisions.  Do you store duplicate values?  Do you want the list sorted?  etc.  Depending on your answers you may be able to use the std::vector class to store the integers.

If not, use a variant of the class in the example.  When you need to insert a value into the list, check to make sure that the array is not full.  If it is full, double its size (copying the old list to the new one), and continue.


Good Luck,
Kent

class TArchive
{
  private:
    int *List;
    int Count;
    int Limit; 
  public:
    TArchive ();
    ~TArchive ();
    Add ();
    List;
}; 
TArchive::TArchive ()
{
  Count = 0;
  Limit = 1000;
  List = new int [Limit];
} 
TArchive::~TArchive ()
{
  delete [] List;
} 
TArchive::Add (int NewValue)
{
} 
TArchive::List ()
{
}

Open in new window

0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi dinedine,

Object oriented programming is often much more efficient that it first appears.  To understand this, it helps to think of what OOP actually is from a programming viewpoint.  In C++, what you're proposing could easily look something like this:

Class myclass
{
};

myclass *MyObject;

  MyObject = new myclass;

  MyObject->Add (1);


Programming the identical functionality in conventional C could easily look like this:

typedef struct
{
} mystruct

mystruct *MyObject;

  MyObject = (mystruct *)malloc (sizeof (mystruct));
  Add (MyObject, 2);



In essence, they are the same, as the Add() method in the first example implicitly passes the 'this' pointer.  At the assembly level, both Add() implementations deal with the same number of parameters so sill produce very, very similar object code.


Good Luck,
Kent
0
 
dinedineAuthor Commented:
Hey,
        I got your viewpoint. But, whats going on in my mind is, i don't want to miss any details in the given requirement....  Here's the problem again.....

" As a part of a larger system, a class "Archive" is required that will accept whole number parameters to a method called "Add" which collects the number for later use. The class "Archive" should also provide a method "Show" that displays the collection of numbers on the standard console output. Object Oriented Concepts and attention to detail should be demonstrated by the Class. "

For example, its given that,  " As a part of a larger system ". Does that need to be reflect a change in the implementation. or what else is main in this trivial question.

Reply me if something rings a bell.

Thank you so much for your time and consideration Kent
Curtis
0
All Courses

From novice to tech pro — start learning today.