operator overloading
hello every one ;
I have a little problem
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 54 55 56 57 58 59
|
// englconv.cpp
// conversions: Distance to meters, meters to Distance
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
const float MTF; //meters to feet
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0), MTF(3.280833F)
{ } //constructor (one arg)
Distance(float meters) : MTF(3.280833F)
{ //convert meters to Distance
float fltfeet = MTF * meters; //convert to float feet
feet = int(fltfeet); //feet is integer part
inches = 12*(fltfeet-feet); //inches is what's left
} //constructor (two args)
Distance(int ft, float in) : feet(ft),
inches(in), MTF(3.280833F)
{ }
void getdist() //get length from user
{
cout << "\nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
}
void showdist() const //display distance
{ cout << feet << "\'-" << inches << '\"'; }
operator float() const //conversion operator
{ //converts Distance to meters
float fracfeet = inches/12; //convert the inches
fracfeet += static_cast<float>(feet); //add the feet
return fracfeet/MTF; //convert to meters
}
};
////////////////////////////////////////////////////////////////
int main()
{
float mtrs;
Distance dist1 = 2.35F; //uses 1-arg constructor to
//convert meters to Distance
cout << "\ndist1 = "; dist1.showdist();
mtrs = static_cast<float>(dist1); //uses conversion operator
//for Distance to meters
cout << "\ndist1 = " << mtrs << " meters\n";
Distance dist2(5, 10.25); //uses 2-arg constructor
mtrs = dist2; //also uses conversion op
cout << "\ndist2 = " << mtrs << " meters\n";
//dist2 = mtrs; //error, = won't convert
return 0;
}
|
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
|
// strconv.cpp
// convert between ordinary strings and class String
#include <iostream>
using namespace std;
#include <string.h> //for strcpy(), etc.
////////////////////////////////////////////////////////////////
class String //user-defined string type
{
private:
enum { SZ = 80 }; //size of all String objects
char str[SZ]; //holds a C-string
public:
String() //no-arg constructor
{ str[0] = '\0'; }
String( char s[] ) //1-arg constructor
{ strcpy(str, s); } // convert C-string to String
void display() const //display the String
{ cout << str; }
operator char*() //conversion operator
{ return str; } //convert String to C-string
};
////////////////////////////////////////////////////////////////
int main()
{
String s1; //use no-arg constructor
//create and initialize C-string
char xstr[] = "Joyeux Noel! ";
s1 = xstr; //use 1-arg constructor
// to convert C-string to String
s1.display(); //display String
String s2 = "Bonne Annee!"; //uses 1-arg constructor
//to initialize String
cout << static_cast<char*>(s2); //use conversion operator
cout << endl; //to convert String to C-string
return 0; //before sending to << op
}
|
line 56 in the first
the one argument constructor(for converting) isn't working but
in line 29 in the second
it is working properly why??
Last edited on
this is a small question!
Topic archived. No new replies allowed.