Library of Classes

Pages: 1234567
No, I'm not. I'm not confused. I was trying to help. But got an F as a grade for helping. Thank you for the F.
How is there an error on line 7 though?? I admit I don't get that part.
Your only allowed to declare constants in the class definition, but that would be counter productive to your origional goal. Which by the way I see may be out of reach as of yet.
Darn it!!!!
Ok, now I'm at home. A lot of catching up, I see. :-)

Agreed, this is too darn interesting! I have never done it because of its complexity and lack of time! And here I am investing whatever little time I have.

Ok, so there are two mainstreams here: Mine, polymorphic oriented, and Computergeek01's + mcqueen, template oriented. Let's make a small proof of concept:

Let's each write some very basic classes that allow to: Sum two distances: One in in meters and the other one in feet; define a time class that can handle conversion between seconds and hours, and one speed class that can be produced by the division of Distance object by a Time object.

And we can see which model is better. Does it sound OK?
I'm leaving in about an hour but I'll see what I can come up with

EDIT: I just want to be clear that I only think we need templates for the prefix class, past that I wouldn't be using them too much.
Last edited on
Round 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
enum
{
    feet,
    inches,
    yard,    
};

int FEET = feet;   /*Concept on how to deal with different inputs to the constructor*/
int INCH = inches;
int YARD = yard;

template<class Unit>
class Pre
{
    public:
        Unit Kilo(Unit unit) {return (unit * 1000);}   //Examples because my last post about this
        Unit Mega(Unit unit) {return (unit * 1000000);} //was a bit scattered

};


template<class D>
class Dist: public Pre<D>
{
    private:
        D metre;
        
    public:
          Dist()            {metre = 0;}
          Dist(D m)         {metre = m;}
          Dist(D m, int); /*Defined Later*/
        
        D dist()            {return metre;}
        D distEN()          {return ((D) (metre * 3.2808399));}
        
        D operator+(Dist add)
            {Dist<D> d = metre + add.metre;}   
};    
    

template<class D>
Dist<D>::Dist(D m, int arg)
{
    metre = m;
    
    switch(arg)
    {
        case feet: metre = (D) metre/3.2808399; break;
        case inches: metre = (D) ((metre/12) / 3.2808399); break;
        case yard: metre = (D) metre * 1.0936133; break;
    }   
    
}

It needs a specilized template for integers because of the unit conversions or else it throws a warning but it works to my satisfaction.
Last edited on
Round 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
enum
{
    second,
    minute,
    hour
};

template<class TC>
class TimeClass: public Pre<TC>
{
    private:
        TC secs;
        
    public:
        TimeClass() {secs = 0;}
        TimeClass(TC num) {secs = num;}
        TimeClass(TC num, int);
        
        TC seconds() {return secs;}
        TC minutes() {return (secs / 60);}
        TC hours()   {return (minutes() / 60);}

};

template<class TC>
TimeClass<TC>::TimeClass(TC num, int arg)
{
    
    switch(arg)
    {
        case second:   secs = num;       break;
        case minute:   secs = num * 60; break;
        case hour:     secs = ((num * 60) * 60); break;
    }
}

Basically the same as the first but with a few changes in the formula etc. All of the simularities are the kind of stuff I would work toward putting in the Base_Class.

I won't have time to write the third one tonight but I'll get back to you as soon as I can.
Last edited on
Part 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum
{
    feet, meters ;
};
int Feet=feet ;
int Meters=meters ;
template <Distance>
cout<<"Enter in feet: " <<endl ;
cin>> Feet ;
cout<<"Enter in meters: " <<endl ;
cin>> Meters ;
double sum(double FEET, double METERS)
{
   Metertofeet=METERS*3/10 ;
   InFeet=FEET+Metertofeet ;
   Feettometer=FEET*10/3 ;
   InMeters=Feettometer+METERS ;
   cout<<FEET <<" feet + " <<METERS <<" meters = " <<InFeet <<" feet and " <<InMeters <<" meters." <<endl ;
}
sum(Feet, Meters) ;
//I now think we don't really need class just to add them 

Part 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int timetype
cout<<"Start in seconds or hours? 1-seconds, 2-hours." <<endl ;
cin>> timetype ;
switch (timetype)
{
   case 1:
      double seconds ;
      cout<<"Enter seconds: " <<endl ;
      cin>> seconds ;
      double Hours=seconds/60/60 ;
      cout<<"This is " <<Hours <<" hours." <<endl ;
   case 2:
      double hours ;
      cout<<"Enter hours: " <<endl ;
      cin>> hours ;
      double Seconds=hours*60*60 ;
      cout<<"This is " <<Seconds <<" seconds." <<endl ;
}
Mine aren't that good and I am stupid. I cannot create a working good idea for this project. But some of these ideas are basic and should not need use of class like seconds to hours. It's better not to use class for that function as I did not. Please think harder on your ideas though and the best way to execute them: class, or not with class?
class or not with class???

And as you see not with class can also be shorter as well.
Last edited on
Here's a better idea:
Create a program/library of classes that can calculate miles per gallon instead of distance, how many gallons per mile?
2. Ill tell you tomorrow i've got to go. Please think about what i've said so far. Not to ruin the party ,but with these basicish ideas/traps we are falling into, it isn't that great of a project, SO
LETS GET OUT OF THE TRAP!!!
^Are you doing that on purpose?

You appear to...fail.
Possible First Part:
1
2
3
4
5
6
7
8
9
10
#include <cstdlib>
#include <iostream>

class cost_per_tank/miles
{
   public:
      double miles_driven, miles_per_gallon, gallons_in_tank, cost_of_gallon ;
      miles_per_gallon=miles_driven/gallons_in_tank ;
      cost_of_tank/miles=cost_of_gallon*miles_per_gallon*gallons_in_tank ;
} MILESCLASS ;
Needs to be altered I know!!!
I'm...pretty sure you have no idea what's going on now. Maybe you should just listen instead.
No I do, now stop bugging me before I report you!
Most of this stuff, seconds to hours hours=seconds*60*60,REALLY? BASIC!!! NEED HARD STUFF! I MAY SAY I'M STUPID BUT I'M NOT! JUST FEEL LIKE IT WHEN IM AROUND YOU PEOPLE WHO KNOW MORE THAN THERE'S TO KNOW ABOUT C++!!! I KNOW WHAT THERE'S TO KNOW FOR THE MOST PART, BUT I DON'T OVER-KNOW LIKE YOU NERDS! EVEN THOUGH I'M AND EVERYONE AT MY SCHOOL IS A NERD ANYWAY!!!
stop bugging me before I report you!


>_> <_<  ̄\(-_-)/ ̄
I NEVER BUGGED YOU, YOU WEREN'T EVEN IN THIS AND YOU JUST CAM ON AND STARTING BUGGING ME NOW I WILL REPORT YOU, SO STOP BUGGING ME AND LYING!!!
Pages: 1234567