GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Oct-2007, 07:05
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Help With Class Inhertance


I need help accessing some numbers from the public class. I have not defined the intergers anywere becuase i dont know where. If you can help me anything you be appriciated. Sorry for bad spelling

CPP / C++ / C Code:
#include <iostream>
#include <string>

class Maths {
      public:
             void addition();
             void subtraction();
             };
             
      class addition : public Maths {
            public:
            //NEED HELP HERE
                   };
      
      class subtraction : public Maths {
            public:
                   //NEED HELP HERE
                   };          
      
      int main () {
          
          int x;
          int y;
          std::cout <<"Enter the value of x;
          std::cout <<"Enter the valu of y;
          ///do the addition function here.
          ///do the subtraction function here.
          
          return 0;
          }
      
      
  #2  
Old 22-Oct-2007, 10:31
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Help With Class Inhertance


Quote:
Originally Posted by doom1992
I need help accessing some numbers from the public class. I have not defined the intergers anywere becuase i dont know where. If you can help me anything you be appriciated. Sorry for bad spelling
You need to tell a bit more about what you want. What integers are you talking about? Do you want to make the integers data members to the classes?

Before you go any further, consider your class hierarchy.

You have a base class Maths and derived class Addition. Now in your base class you declare a method addition(). Do you think that makes sense? If you have addition() in your base Maths class, why have a class Addition at all?

In my opinion Maths as a class is way too broad and abstract. Mathematics involves a lot more than just regular addition and division etc, things I wouldn't ever get a clue about. (I've tried, and failed.)

Here's what I would suggest:

Rename your Maths class to BinaryOperators or something. Then think about what is common with all binary operators. Two operands, right? So in your BinaryOperators class you would create two data members for two numbers.

You would NOT, however, create methods for addition and subtraction as you do in your example. In your example, the addition class has operators for addition and subtraction, but that doesn't really make any sense does it. Addition can't subract.

So, your classes could look like something as follows.

CPP / C++ / C Code:
class BinaryOperators
{
public:
    // maybe a constructor here? Maybe make this an abstract base class?

private:

    int operand1;
    int operand2;

};

class Addition : public BinaryOperators
{
public:
    
    // methods here

private:

    // doesn't probably need more data members?
};

class Subtraction : public BinaryOperators
{
    // ...
};
__________________
Music, programming, endless learning..
  #3  
Old 22-Oct-2007, 10:58
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: Help With Class Inhertance


thanks but that not wt im afgter, i will figure it out some how.
  #4  
Old 22-Oct-2007, 11:48
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

Re: Help With Class Inhertance


if you want to have public access to public members (not usually recommended):

somefile.cpp
CPP / C++ / C Code:
class CMyNums {
public:
  int i;
  int j;
};

int main() {
 CMyNums objMyNums;
  objMyNums.i = 253; //some integer
  objMyNums.j = 234; //same deal
  return 0;
}

Generally when you make classes though you try and use information hiding, or encapsulation, or whatever you wanna call it by preventing direct access to member variables, but providing public methods (functions) to work with those private variables. Those are often called accessor and mutator methods.

somefile.cpp
CPP / C++ / C Code:
class CMyNums {
private:
  int i;
  int j;
public:
  void setI(int newi) { i = newi; } 
  void setJ(int newj) { j = newj; }
  int getI() { return i; } //returns a copy of I
  int getJ() { return j; } //returns a copy of J
};

int main() {
  CMyNums objMyNums;
  objMyNums.setI(14);  //internaly set i in CMyNums to 14
  objMyNums.setJ(objMyNums.getI()); //gets 14 from i and puts 14 in j
}

I didn't compile any of that or whatever but it should demonstrate the basic uses
  #5  
Old 22-Oct-2007, 11:59
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: Help With Class Inhertance


thanks m8, i now have a wider insite to this.
  #6  
Old 22-Oct-2007, 20:06
davis
 
Posts: n/a

Re: Help With Class Inhertance


Quote:
Originally Posted by doom1992
CPP / C++ / C Code:
class Maths {
      public:
             void addition();
             void subtraction();
             };


Here's a hint. If you are designing "functions" that take no parameters and return nothing, they are usually worthless. In otherwords, they tend to represent bad design choices. I'm not saying that these are your choices, but it is something to be considered.

You need to learn a lot more about OOP. Your base class is specifying behavior for your derived classes...but you define operations named "addition" but how is that useful in a class named Subtraction? Likewise, how is the operation "subtraction" useful in a class named Addition?

As Kimmo mentions, there are probably some useful ABCs to be implemented here...but what will we expect from the base? Overloaded operators? Aren't we talking about simple mathematics? I don't see any "natural" ABC from what it appears you're trying to implement. If there was one, I'd expect it to be a prototypical, but I'm not sure what it would be. I can only guess.

Without a stated purpose for what this is, I'm going to have to say that everywhere you see:

NEED HELP HERE

...in your code, you need help there as well as both above and below those lines.


:davis:
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes and allocating memory BlueFireCO. C++ Forum 13 26-Jul-2007 20:31
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 15:11
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 12:31
a tester class and then some. postage Java Forum 1 06-May-2006 15:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.