C++ Oop Program, Help Needed

Converting structured to OOP

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2254 Views - Last Post: 11 January 2005 - 10:31 AM Rate Topic: -----

#1 rezanin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-January 05

C++ Oop Program, Help Needed

Post icon  Posted 11 January 2005 - 07:41 AM

Hey all devheads,

i have this one piece of coursework which is really annoying me, i was hoping someone on this board could give me a push in the right direction. all help would be much appreciated.

I have to convert a structured program to a object oriented program using encapsulation and inheritance. I have a general jist of OOP, but just cant seem to get it together to do the coding required. I've been told it can only hold member functions (except for the main function which should only contain the code to create the first object), and it cant use global variables. I've attached the coding so you can freely browse through it.

I hope someone out there can help me with this, hopefully once i get to grips with C++ i can start helping the next lot of newbies!

Cheers, Rezanin

Attached File(s)

  • Attached File  task2.zip (3.19K)
    Number of downloads: 123


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Oop Program, Help Needed

#2 Amadeus  Icon User is offline

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

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

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 08:32 AM

Well, I'll take a look tonight, I'm at work right now so I can't really do anything. Converting to an OOP style program is not all that difficult, however. I haven't browsed your code yet, but have you started the conversion?
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

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

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

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 08:41 AM

When I do get home, it might be easier if there was anything in that zip file. Am I the only one that gets nada?
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 08:55 AM

I'm gonna give this a shot...

You're going to want to create a class with your AddNew, Show, Read... etc. Methods (functions) in it. I'm not sure the best way to create more than one class for your program since the main object is the Amplifier. Unless there is something else other than Amplifier (like a sub type of amplifier or something like that)

When you instantiate the class, any variable that is created in 1 method is automatically available in another function in the same class.

If you do decide to make more than 1 class to break your object down in to smaller parts or have other methods that can use the object in the superclass, then inheritance will make those variables available to the subclass. This keeps you from creating global variables.

I'm not 100% awake yet so that probably didn't make much sense, but I'm having trouble figuring out how you could create more than 1 class from your program since you really only have the 1 object... the amplifier.
Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 08:56 AM

Amadeus, on Jan 11 2005, 09:41 AM, said:

When I do get home, it might be easier if there was anything in that zip file. Am I the only one that gets nada?

Save it to your desktop first instead of hitting open... I had the same problem.
Was This Post Helpful? 0
  • +
  • -

#6 Aries  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-January 05

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:15 AM

I'm a self-tought programmer so I don't know all the fancy vocabulary that they teach you in school that have to do with programming, I have done some OOP projects like a rpg so I know a thing or two about OOP, can anyone explain to me what encapsulation is?
Was This Post Helpful? 0
  • +
  • -

#7 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4503
  • View blog
  • Posts: 28,410
  • Joined: 21-March 01

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:16 AM

I guess the answer kind of depends on what your trying to do, are you aiming for a class with methods way of doing it? because i saw in your code you have kind of the linked-list structure going on, are you just trying to take that to the next level with the requirements you stated?
Was This Post Helpful? 0
  • +
  • -

#8 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:23 AM

Yeh, what Josh said... I looked at your code a little more and it looks like you are already using structures to your advantage so has your instructor specified a certain way they want you to do the program?
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

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

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

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:23 AM

I've taken a brief look at the code, and your program sets up well for conversion to an OOP style program. The first thing you'll want to do is take the struct itself, and declare it as a class. The class declaration should be in one of your text books. Try to start with that, it is very similar to declaring a structure. The next step is to implement your current functions as methiods within that class.

To start you off, use something like this
class OpAmps{
  char TypeNumber[20];
  unsigned int NumberOfPins;
  float SlewRate;
  float Distortion;
  unsigned long Price;

  int AddNew(OpAmps *);
  int Save(const OpAmps *, unsigned long = 0);
  int Read(OpAmps *, unsigned long&);
  int Show(const OpAmps *, unsigned long = 0);
  unsigned long Remove(OpAmps *, unsigned long = 0);
};


If you need help making your functions into methods, let us know.
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

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

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

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:25 AM

Aries, on Jan 11 2005, 11:15 AM, said:

I'm a self-tought programmer so I don't know all the fancy vocabulary that they teach you in school that have to do with programming, I have done some OOP projects like a rpg so I know a thing or two about OOP, can anyone explain to me what encapsulation is?

Encapsulation is essentially the ability to gather related information and functionality into a class. Click here for a good starting example.
Was This Post Helpful? 0
  • +
  • -

#11 Aries  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-January 05

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:27 AM

So I guess the question about encapsulation wont be answered? =P
Edit: Oops, browser kinda slow didn't see that response, nevermind the question then.

This post has been edited by Aries: 11 January 2005 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#12 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:28 AM

Aries, on Jan 11 2005, 10:15 AM, said:

I'm a self-tought programmer so I don't know all the fancy vocabulary that they teach you in school that have to do with programming, I have done some OOP projects like a rpg so I know a thing or two about OOP, can anyone explain to me what encapsulation is?

I'm a newbie at OOP, but I think the best way to describe encapsulation is hiding data. In an object oriented world, you make a request and get an answer, how the program arrived at that answer and the data it used to get there is really none of your business. Encapsulation is hiding the data within some sort of process to prevent the rest of the program from seeing or manipulating it.
Was This Post Helpful? 0
  • +
  • -

#13 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:30 AM

skyhawk133, on Jan 11 2005, 10:28 AM, said:

Aries, on Jan 11 2005, 10:15 AM, said:

I'm a self-tought programmer so I don't know all the fancy vocabulary that they teach you in school that have to do with programming, I have done some OOP projects like a rpg so I know a thing or two about OOP, can anyone explain to me what encapsulation is?

I'm a newbie at OOP, but I think the best way to describe encapsulation is hiding data. In an object oriented world, you make a request and get an answer, how the program arrived at that answer and the data it used to get there is really none of your business. Encapsulation is hiding the data within some sort of process to prevent the rest of the program from seeing or manipulating it.

Hahaha, ok then... I'ma just sit my little butt down... Amadeus link totally contradicted what I said so nevermind :-)
Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

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

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

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:34 AM

No, you're not wrong at all skyhawk, not really...encapsulation is often represented as hiding data, essentially giving you an object that produces the desired result given the correct input, like a toaster...bread goes in, toast comes out, what happens inside is really hidden. This link explains it pretty well.
Was This Post Helpful? 0
  • +
  • -

#15 Aries  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-January 05

Re: C++ Oop Program, Help Needed

Posted 11 January 2005 - 09:41 AM


class OpAmps
{
private:
  char TypeNumber[20];
  unsigned int NumberOfPins;
  float SlewRate;  // in V/ms
  float Distortion;  // in percent
  OpAmps* Next;  // link to the next OpAmp in the list
public:
	/* Member functions that work with the files */
	int AddNew(OpAmps *);
	int Save(const OpAmps *, unsigned long = 0);
	int Read(OpAmps *, unsigned long&);
	int Show(const OpAmps *, unsigned long = 0);
	unsigned long Remove(OpAmps *, unsigned long = 0);
	/*********************************************/
	/* Functions that will check the value that the user
    wants to place into the private variables of the class
    if correct then the private variables value will be replaced
    with the user value */
    /* Return value would be bool; true if the replacing value is valid
           false if the replacing value isnt valid */
	bool setTypeNumber(char *TypeNumber);
	bool setNumberOfPins(int NumberOfPins);
	bool setSlewRate(int SlewRate);
	bool setDistortion(int Distortion);
	bool setPrice(int Price);
	/***********************/
	/* Functions that will return the value of the private variable in the class */
	char *getTypeNumber();
	unsigned int getNumberOfPins();
	float getSlewRate();
	float getDistortion();
	unsigned long getPrice();
	/************************************************************************/
};  



Hopefully this code is right if i got the right idea of encapsulation.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2