问题1定义一个分数类fraction(分数),定义运算符“<<”,以分数形式输出结果(如2/3)。写出使用该类的完整程序。
问题2定义一个字符串类string,使其至少具有内容(contents)和长度(length)两个数据成员,并具有显示字符串、求字符串长度、给原字符串后添加一个字符串等功能。
请大侠们不吝指教
#include<iostream.h>
class number{
friend ostream &operator<<(ostream &, const number &);
friend istream &operator>>(istream &, number&);
private:
int numerator;
int denominator;
};
ostream &operator<<(ostream& output, number& num)
{
output<<num.numerator<<"/"<<num.denominator;
return output
}
istream &operator>>(istream &input, number &num)
{
input>>num.numerator;
input.ignore(1);
input>>num.denominator;
return input;
}
这是第一题的答案,主函数我就不写了,你自己加上吧
还有我连>>也顺便帮你重载了,如果你觉得没必要,就把他给删了
我没有调试过你自己调试一下,这可以使你对程序更加了解
(其实是我比较懒,不过错的地方应该不是很多)
第二题也很简单,只是写起来比较麻烦
自己在想想吧,睡觉先。。。。。。。。
[此贴子已经被作者于2004-06-28 00:12:46编辑过]
#include<iostream> #include<cstdlib> using namespace std;
class Fraction { private: int numerator; int denominator; public: Fraction(int n, int d) { numerator=n; if(d) denominator = d; } friend ostream & operator<<(ostream &, const Fraction &); };
ostream & operator<<(ostream & output, const Fraction & theFraction) { output<<theFraction.numerator<<"/"<<theFraction.denominator; return output; } int main() { int n = 2; int d = 3; Fraction myFraction(n, d); cout<<myFraction<<endl; system("pause"); return 0; }
#include<iostream> #include<cstring> #include<cstdlib> using namespace std;
class String { private: char * str; // pointer to string int len; // length of string enum{ CINLIM = 80}; // cin input limit public: String(const char * s); // constructor String(); // default constructor String(const String &); // copy constructor ~String(); // destructor int length() const { return len;} // overloaded operator methods String & operator=(const String &); String & operator=(const char *); char & operator[](int i); const char & operator[](int i) const; // friend function friend ostream & operator<<(ostream & os, const String & st); friend istream & operator>>(istream & is, String & st); };
// class methods String::String(const char * s) // construct String from C string { len = strlen(s); // set size str = new char[len +1]; // allot storage strcpy(str, s); // initialize pointer } String::String() // default constructor { len = 4; str = new char[1]; str[0] = '\0'; // default string } String::String(const String & st) { len = st.len; // same length str = new char[len + 1]; // allot space strcpy(str, st.str); // copy string to new location } String::~String() // destructor { delete [] str; } // assign a String to a String String & String::operator =(const String & st) { if(this == &st) return *this; delete [] str; len = st.len; str = new char[len +1]; strcpy(str, st.str); return * this; } // assign a C string to a String String & String::operator =(const char * s) { delete [] str; len = strlen(s); str = new char[len +1]; strcpy(str,s); return *this; } // read-write char access for non-const String char & String::operator [](int i) { return str[i]; } // read-only char access for const String const char & String::operator [](int i) const { return str[i]; } // String output ostream & operator<<(ostream & os, const String & st) { os<<st.str; return os; } istream & operator>>(istream & is, String & st) { char temp[String::CINLIM]; is.get(temp, String::CINLIM); if(is) st = temp; while(is && is.get() != '\n') continue; return is; } int main() { String name; cout<<"What's your name?\n"; cin>>name; cout<<name.length();
system("pause"); return 0; }