Thread: Trying to get a subclass example to work...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    46

    Trying to get a subclass example to work...

    I'm using code::blocks.
    the code below notes:
    Fract.cpp must be compiled and linked into
    the project, or else Fraction frunction defs must be
    copied into this file.

    Does anyone know how to do the former? I tried adding the Fract.cpp with the main.cpp. I get a compiler error that "Fract.h file or directory does not exist."

    Code:
    // Exercise 17.01.02
    // Adds set_float() funct. and tests it.
    //
    #include <iostream>
    
    #include "Fract.h" // Declaration of Fraction class must
                       // be placed in Fract.h.
    using namespace std;
    
    // Note: Fract.cpp must be compiled and linked into
    // the project, or else Fraction frunction defs must be 
    // copied into this file.
    
    class FloatFraction : public Fraction {
    public:
        FloatFraction() {} // Default constr:
                           // F.F. inherits no contr’s!
    
        FloatFraction(double x) {set_float(x);}    // NEW CONSTR.
    
        double get_float() {
           double x = get_num();
           return x / get_den();
        }
        
        void set_float(double x) {             
             int n = static_cast<int>(x * 100.0);
             set( n, 100 ); 
        } 
    };
    
    int main() {
        FloatFraction fract1(0.75), fract2(1.5), fract3(0.333);
    
        cout << "Value of fract(0.75) is " << fract1 << endl;
        cout << "and its get_float val is: " << fract1.get_float();
        cout << endl;
     
        cout << "Value of fract(1.5) is " << fract2 << endl;
     
        cout << "Value of fract(.333) is " << fract3 << endl;
    
        system("PAUSE");
        return 0;
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    "Fract.h file or directory does not exist."
    Then it simply doesn't.
    If you have a reason to believe that it should exist, verify that it is in the same folder as main.
    If you get errors after that, your environment variables are broken.

    See the following example to see if you did the same:

    1.File Class.h =>Contains you base class declaration. This should be included in main.cpp and Class.cpp both, and suitable guarded.
    2.File Class.cpp => Should contain the definitions of the functions of the base class. Compile this with the main program or add it to the project to be done automatically.
    3.main.cpp => Home of int main() and anything else you might want to add.
    Last edited by manasij7479; 08-26-2011 at 02:39 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Quote Originally Posted by manasij7479 View Post
    Then it simply doesn't.
    1.File Class.h =>Contains you base class declaration. This should be included in main.cpp and Class.cpp both, and suitable guarded.
    2.File Class.cpp => Should contain the definitions of the functions of the base class. Compile this with the main program or add it to the project to be done automatically.
    3.main.cpp => Home of int main() and anything else you might want to add.
    1. This is where I'm confused. You're right there is no file Fract.h. Is this something we have to create? Is this something "code wise" we have to add to main.cpp and fract.cpp?

    2.They do and I did locate Fract.cpp in the directory where main.cpp resides and then added it to my project.

    Head hurts

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You're right there is no file Fract.h. Is this something we have to create?
    Now, I'm confused..How do you expect to use Fract.h if it is not there?

    I'll guess that this is about a lesson in inheritance.
    So, here it goes:
    You've to create Fract.h with the declaration of the fraction class, and put the corresponding definitions in the .cpp file.

    The main looks ok.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It should be noted that this comment is false...
    Quote Originally Posted by tabl3six View Post
    Code:
        FloatFraction() {} // Default constr:
                           // F.F. inherits no contr’s!
    
        FloatFraction(double x) {set_float(x);}    // NEW CONSTR.
    Both of those constructors do in fact call the default constructor of the Fraction class that it inherits from.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by manasij7479 View Post
    I'll guess that this is about a lesson in inheritance.
    So, here it goes:
    You've to create Fract.h with the declaration of the fraction class, and put the corresponding definitions in the .cpp file.
    Inheritance has nothing to do with header files, actually.

    It is only by convention that programmers often create a header file and a source file corresponding to a particular class. This is done for the convenience of programmers. The compiler does not care, one way or the other. The only thing needed is that each class, each of its member functions, and each of its static members are defined exactly once across all the source files in a project.

    However, if you are going to tell the preprocessor to #include "Fract.h", then Fract.h must exist.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by grumpy View Post
    Inheritance has nothing to do with header files, actually.

    It is only by convention that programmers often create a header file and a source file corresponding to a particular class. This is done for the convenience of programmers. The compiler does not care, one way or the other. The only thing needed is that each class, each of its member functions, and each of its static members are defined exactly once across all the source files in a project.

    However, if you are going to tell the preprocessor to #include "Fract.h", then Fract.h must exist.
    I know inheritance has nothing to do with header files, but in the example he posted, the base class was in a separate file, as you said..for convenience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. subclass attribute
    By Farnaz in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2011, 02:32 PM
  2. Problem with subclass
    By BlackSlash12 in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2007, 10:26 AM
  3. DNS on a subclass C network.
    By Kennedy in forum Tech Board
    Replies: 0
    Last Post: 11-14-2006, 12:19 PM
  4. Subclass and subtype.
    By Mikro in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2004, 11:28 PM
  5. subclass
    By dayknight in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2004, 03:39 PM