2013-Level C + + article 9 weeks (Spring) engineering-operator overloading (two)

Source: Internet
Author: User

Course Homepage:http://blog.csdn.net/sxhelijian/article/details/11890759, complete teaching program and resources Link


"Program reading" Reading program "simple C + + Student Information Management system", to find out the occurrence of the structure function, friend function, operator overloading, static number member syntax phenomenon appeared position, detailed experience of its use method. Flexible application of methodologies and techniques in future designs

Item 1-3 copies the code for the 8th week directly, adding the required operator overload definition, and posts the task as the 9th week separately.


"Item 1" is based on item 1 of week 8th
(1) Redefining the-,-c operator is equivalent to 0-c.
(2) Define overloads of the << and >> operators in the complex class to implement input and output. Transformation of the original program in the display of the results of the operation, so that the program read more natural.



"Item 2" is based on item 2 of week 8th
(1) define the self-increment and decrement operators for time objects

The overload of the CTime operator is operator++ (int);//post + +, next second CTime operator++ ();//front + +, next second, front and back return value is not the same ctime operator--(int);//post--, The previous second CTime operator--();//front--, one second
(2) define the << and >> operator overloads in the time class, realize the input and output of the times, and transform the display mode of the operation results in the original program, making the program read more naturally.


"Item 3" is based on item 3 of week 8th
(1) Define the fraction of the first operation + and-, respectively, to represent the fractional positive and negation, the "bitwise negation operator" ~ Overloading the calculation of the inverse of the fractional.
(2) define the << and >> operator overloads in the fractional class, realize the input and output of the fractions, and transform the display mode of the operation results in the original program, making the program read more naturally.



"Project 4" establishes a vector class myvector, declaring for example the following, please complete the definition of the class

Class Myvector//define vector class {public:myvector (int m);  constructors, which collectively have a vector of m elements, with element values preset to 0 myvector (const myvector &AMP;V); Copy constructor ~myvector ();    Destructors: Releasing storage space occupied by dynamic arrays friend IStream &operator>> (IStream &input, Myvector &d);    Friend Ostream &operator<< (ostream &output, const myvector &d); Friend Myvector operator+ (const myvector &d1,const myvector &AMP;D2);//vector addition, the elements in the corresponding position add friend Myvector operator-(co NST myvector &d1,const myvector &d2);//vector subtraction. Subtract the element from the corresponding position bool operator== (const myvector &d);//Infer whether the two matrices are equal.       That is, all the elements in the corresponding position are equal private:int *array;          Array is a dynamic array pointer to the vector that occupies the starting address of the memory int num;    The number of elements in the vector};int main () {myvector D1 (5), D2 (5);    cout<< "Input d1--";    cin>>d1;    cout<< "Input d2--";    cin>>d2;    cout<< "d1=" <<d1<<endl;    cout<< "d2=" <<d2<<endl;    cout<< "d1+d2=" <<d1+d2<<endl;   cout<< "d1-d2=" <<d1-d2<<endl; cout<< "D1" << (D1==D2)? 

"= =": "! =") << "D2" <<endl; return 0;}


"Project 5" design a one-dimensional equation class to find the solution of the equation as ax+b=0.
For example: when entering 3x-8=0. The solution of the output equation is x=2.66667;
Again such as: when input 5s+18=0, the solution of the output equation is s=-3.6;
Class cequation{private:double A;     Unknown coefficient double b;     Constant term char unknown; Symbol public:cequation representing unknown (double aa=0,double bb=0); friend IStream &operator >> (IStream &in,cequation & amp;e); friend Ostream &operator << (ostream &out,cequation &e);d ouble Solve (); Char Getunknown ();}; int main () {cequation e;cout<< "Please enter the equation (format: ax-b=0,a, B is constant, X is the letter representing the unknown):";cin>>e;   In two Tests, the equations for the input 3x-8=0 and 50s+180=0cout<< are: "The solution of the <<e;cout<<" equation is: "<<e.getunknown () <<" = " <<e.solve () <<endl; Two Tests, respectively output x= ... and S=...e.solve ();}




==================== Helijian csdn Blog column ================= |== It Student Growth Guide Column column category folder (not regularly updated) ==| |== C + + Classroom Online Column The course teaching link (sub-course grade) ==| |== I wrote the book-"The reverse of the university-to the positive energy of IT students" ==| ===== for it rookie runway, and students enjoy a happy and passionate university =====



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

2013-Level C + + article 9 weeks (Spring) engineering-operator overloading (two)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.