C++ Class Question

What should handle the data?

Page 1 of 1

3 Replies - 1711 Views - Last Post: 26 August 2005 - 06:54 AM Rate Topic: -----

#1 cryptic_blade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-August 05

C++ Class Question

Posted 25 August 2005 - 04:22 PM

I am working on the design for a new application and want to make the program fully object oriented. I have built and used C++ classes before, but never with a database driven application. Should classes handle reading and writing their property data to the database table themselves, or should they rely on an outside “DataHandler” custom class or function specific to the application.

If I hard code specific database/table/query information into a class’s structure, it no longer become portable because it is “bound” to database. I want to be able to reuse my classes and I fear that writing storage routines “inside” the class definition will render my classes less portable. How do most of you implement class usage when class data need to be stored in databases and later re-populated into a new instance of the class?

Example:

I want to build a ficticious object oriented program that allows the user to enter in the year, make, and model of their favorite sports car. Then that data is saved to a database table. The user also has the ability to search the “cars” database table and display a list of all previously saved cars. I also need to be able to reuse the classes in other programs without needed to reconstruct the database and tables used in the first program. It is possible that I may need to build another program that deals with cars, but doesn't store the car information in a database.

I already know I need a “Car” class, but should the Car class take care of reading and writing to the database directly through methods I expose? If I build-in database storage into the class I can’t reuse the class in another application without creating a database with a “car” table. What to do???

Class: Car
Property1: Year
Property2: Make
Property3: Model
Method1: getCar(carID) <- populate object with database record data?
Method2: setCar(Year, Make, Model) <- update database record with object data?

Hope I made my question clear enough.

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Class Question

#2 Videege  Icon User is offline

  • rvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: C++ Class Question

Posted 25 August 2005 - 04:42 PM

Well clearly, if you intend to reuse your data handler code then you need to develop an outside class (perhaps a parent data handler or template class that could be inherited to suit specific needs if you have to).
Was This Post Helpful? 0
  • +
  • -

#3 proghelper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-August 05

Re: C++ Class Question

Post icon  Posted 26 August 2005 - 06:38 AM

cryptic_blade, on Aug 25 2005, 04:22 PM, said:

I already know I need a “Car” class, but should the Car class take care of reading and writing to the database directly through methods I expose? If I build-in database storage into the class I can’t reuse the class in another application without creating a database with a “car” table. What to do???

The best is to make the Car class independent of the database query. There should be a seperate class to read and write to the database, say a DatabaseHandlerClass. This class should deal with generic database logic. Then there can be another class that uses the DatabaseHandlerClass functionality to query for car specific stuff and create, update and read Car objects.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: C++ Class Question

Posted 26 August 2005 - 06:54 AM

I would respectfully suggest that the database handler class could also take care of the car specific queries and updates...it is just a matter of writing the class methods generic enough to accept the required parameters...for example, the methods would need to accept table name, number of fields, possibly an array of the field names, etc...If the methods can accept those parameters, they can be used to work with any data/table in the database.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1