• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

Passing variables between classes

i am writing a program to test the capacitance of capacitors. i have done a class structure and written a few functions but am having trouble getting variables to pass among the classes. the classes low and high capacitor inherit from capacitor. im low capacitor i pass the attribute refI, work it in the class and return DK. i am having trouble getting DK to show in the capacitor class. or anywhere for that matter. i expect i will have a few more problems in the future relating top this program but i will offer more points accordingly.

here is my code

--------------------------------------------------------------------------------------------------------------------------------------------------
/*
  Name: Sean Barton
  Date: 03/11/04 20:35
*/

#include <iostream.h>

//-----------------------------------------------

class Capacitor {
private:
   
    int input;
    float refI;


public:
   
    Capacitor();
    void getK();

};

class highCapacitor : Capacitor  {
private:
    float refI;
    float DK;
   
public:
   
    highCapacitor();     //Constructor
    float calcK();

};

class lowCapacitor : Capacitor {
private:
    float DK;
    float refI;


public:
 
    lowCapacitor();     //Constructor
    float calcK(float refI);
      
};

class Interface {

public:
    Interface();      //Constructor
    void displayMenu();
    void displayData();
};
//-------------------------------------------------------------
Capacitor::Capacitor()
{
    cout << "Capacitor Constructor Called" << endl;
    input=0;
   
}

void Capacitor::getK()
{
  highCapacitor high;
  lowCapacitor low;
  cout<<"is the capacitor 1.Low or 2.High: "<<endl;
 
  cout<<"Selection: ";

  cin>> input;
  switch ( input )
  {
    case 1:  
       
    cout << "please enter the refractive index of the low capacitor: ";
    cin >> refI;

    while ( refI<=0.5 || refI>=1.5 ) // While refractive index is less 0.5 OR bigger that 1.5
      {
          cout<<"please enter a value between 0.5 and 1.5:";
              cin>>refI;
      }
       
    low.calcK(refI);
    break;
 
    case 2:      
       
    cout << "please enter the refractive index of the high capacitor: ";
    cin >> refI;

    while ( refI<=1.6 || refI>=2.5 ) // While refractive index is less 1.6 OR bigger that 2.5
      {
          cout<<"please enter a value between 1.6 and 2.5:";
              cin>>refI;
      }              
         
    high.calcK();
    break;
   
    default:          
    cout<<"Error, bad input, quitting\n";
    break;
  }  


}

//-----------------------------------------------

Interface::Interface()
{
   
    cout << "Interface Constructor Called" << endl;
}

void Interface::displayMenu()
{
      char sample[30];
    cout<<"Input name for test:"<<endl;
      cin>>sample;
      Capacitor capacity;
      capacity.getK();
}

void Interface::displayData()
{
    cout<<"data gets returned to here"<<endl;
}
//-----------------------------------------------



highCapacitor::highCapacitor()
{
   
cout<<"Constructor Called"<<endl;

}


float highCapacitor::calcK()
{
    DK=2*refI;  
    return DK;
}

//-----------------------------------------------


lowCapacitor::lowCapacitor()

{
    cout<<"constructor called"<<endl;

   
}

float lowCapacitor::calcK(float refI)
{
  DK=refI*refI;
  cout<<DK;
  return DK;
}
//-----------------------------------------------

int main()
{
    Interface screen;
    screen.displayMenu();
    screen.displayData();

cin.get();

return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------

please reply asap as this is bugging me

SeanBarton
0
SeanBarton
Asked:
SeanBarton
  • 9
  • 8
1 Solution
 
stefan73Commented:
Hi SeanBarton,
The low & high capacitors of yours never get the refl value from the parent class.

Cheers!

Stefan
0
 
stefan73Commented:
Either declare
 float refI;

as

protected:
 static float refI;

(not good from OO point of view)

or use the parent class as factory pattern for the 2 siblings - here's a rough sketch:

class Capacitor {
...
public:
    static Capacitor* createCapacitor(float input_refI){
        if(input_refI > ...)
            return new highCapacitor(input_refI);
        else
            return new lowCapacitor(input_refI);
    }

with both sibling classes having the appropriate constructor. You can then do:

Capacitor* ideal_capacitor = Capacitor.createCapacitor(your_refI);

...and calculate whatever you want with them. Don't forget to make the calculate method virtual:

(in the parent)
virtual float calcK()=0; // Abstract method

(in the siblings)
virtual float calcK(); // Concrete method


 
0
 
SeanBartonAuthor Commented:
how so? high is not yet hooked up but i know low is. i pass it when i say low.getK(....);

is that not it?

Sean
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SeanBartonAuthor Commented:
this is good stuff but im really looking to see why it isnt working, not as such paste this block of code in and it will work.

i am interested in just a quick way of passing the result DK to capacitor class. i am planning on doing this throughout the program but not necessarity through inheritance.

how would i pass the variable DK back if it were just a class to a class. maybe using return?

cheers
0
 
stefan73Commented:
SeanBarton,
> how so? high is not yet hooked up but i know low is. i pass it when i
> say low.getK(....);

That's procedural programming. I don't know if OO is a requirement for you.

I think you want something like "pass me back the right capacitor type for the capacity I give you"
The closest OO construct for this kind of behaviour is a class factory pattern. It keeps changes for implementing a new type of capacitor local and totally transparent to the caller.

Recommended reading: Bruce Eckel, "Thinking in Patterns"
http://64.78.49.204/TIPatterns-0.9.zip

> how would i pass the variable DK back if it were just a class to a class. maybe using return?
By a getter method:

float getDK(){ return DK;}
0
 
SeanBartonAuthor Commented:
well oo isnt a requirement but it is suggested. how would i have done it without oo then?

i have d/lded the reading you suggested and i already have the return.

the return you suggested at the end, ok i have that but when it returns how does it return and where to?

the return is where i think my main problem is... i put in a cout before the return which means the calculation is going ok but i dont know what the return does.

by typing return dk; where does it store the value? or does it just output it to screen? what i need to do is save the value in dk to a variable in capacitor.

any ideas?
i have upped the points to 100 and will give them to you asap
0
 
stefan73Commented:
SeanBarton,
> well oo isnt a requirement but it is suggested. how would i have done
> it without oo then?

In C-style, without classes. Read input, choose one of two calculation methods based on capacity, output result. Very straightforward.

> the return you suggested at the end, ok i have that but when it returns how does it return and where to?

That's for you to decide ;)

Basically, your object is fed with some data, performs calculations and waits for someone to pick up the result. Or more OO, an object is created and initialized with properties (capacities, maximum voltage, temperatures, etc.) The outside world can now communicate with your Capacitor object. You could ask  it for properties, like getDK(), or have it calculate something from its internal state (such as weight, or the colour ring combination of a resistor).

0
 
SeanBartonAuthor Commented:
well ok... how can i make the value stored in dk be placed into a variable in capacitor?

create a float in capacitor called dkreturn and in the return statement in lowcapacitor type "return dkreturn;"... is that right?

Sean
0
 
stefan73Commented:
SeanBarton,
No, that's not necessary. You're already storing the value.

Stefan
0
 
SeanBartonAuthor Commented:
ok then when i have passed control of the program back to capacitor. how do i access that information? i know its called DK in lowcapacitor but so far if i put cout<<DK somewhere in capactiro i get an error or some negative number with letter after it.

you have been very helpful, its just this concept i cant grasp.
i wil close the question and give you the points as soon as i manage to call the value DK from capacitor

cheers

Sean
0
 
stefan73Commented:
You'd store something in DK, then simply do
retrurn DK;

It seems you still lack basic language concepts. Why don't you read a C++ tutorial first?

This is one of my favourites, Bruce Eckel's "Thinking in C++"
http://64.78.49.204/TICPP-2nd-ed-Vol-one.zip
http://64.78.49.204/TICPP-2nd-ed-Vol-two.zip

It's not exactly a beginner's book, but very clearly written.
0
 
SeanBartonAuthor Commented:
ok ill read those and ill read the other one you have guven me....i havent been doing c++ for long but i have done quite a lot of java. ony recently has oo been introduced to me so its a bit of a mess.

but off the top of your head is there a way to call DK from capacitor using a cout or similar? cheers

Sean
0
 
stefan73Commented:
SeanBarton,
> how do i access that information?

You're not using the information calcK returns:

float highCapacitor::calcK()
{
    DK=2*refI;  
    return DK;
}

But you're calling it with

high.calcK();

That's fine from C++'s point of view - it's an implicit cast to (void):

(void)  high.calcK();

But you probably want something like

float K;

...

K =   high.calcK();
cout << "calcK returned: " << K;



Stefan
0
 
stefan73Commented:
(that's the same as in Java)
0
 
SeanBartonAuthor Commented:
thankyou very much. that was what i didnt get. i have raised the points to 150.im sure this will be ok with you.

cheers

Sean Barton
0
 
SeanBartonAuthor Commented:
i did java last semester. but were being taught both languages at once as well as c and vba. not nice.. cheers for ur help
0
 
stefan73Commented:
Welcome
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now