Class 2015 C + + 12th Week Practice Project operator overloading (i)

Source: Internet
Author: User

"Project 1-implementing operator overloading in a plural class" reference answer
(1) Use the member function of the class to define the plural class overloaded operator + 、-、 *,/, so that it can be used for the subtraction of a complex number

classComplex { Public: Complex () {real=0; imag=0;} Complex (DoubleRDoublei) {real=r; imag=i;} Complexoperator+(ConstComplex &c2); Complexoperator-(ConstComplex &c2); Complexoperator*(ConstComplex &c2); Complexoperator/(ConstComplex &c2);voidDisplay ();Private:DoubleRealDoubleImag;};//define member functions below//define the main () function for testing belowintMain () {Complex C1 (3,4), C2 (5,-Ten), C3;cout<<"c1="; C1.display ();cout<<"c2=";    C2.display (); C3=C1+C2;cout<<"c1+c2=";    C3.display (); C3=C1-C2;cout<<"c1-c2=";    C3.display (); C3=C1*C2;cout<<"c1*c2=";    C3.display (); C3=C1/C2;cout<<"c1/c2="; C3.display ();return 0;}

(2) Use the friend function of the class, not the member function, to complete the overloading of the above mentioned operators again;
(3) Define a fully defined class (which can be released as a standalone product and become a "foundation project" in many projects). This class is based on (2), extending the function of the + 、-、 * and/operator so that it can be operated with a double type of data. Set complex C; Double D; The result of C+d and D+c is "add the complex with C as the real Part D", and the other-, *,//operators are similar.

Operator overloading in the project 2-time class reference answer
Implements an operator overload in the time class.

classctime{Private:unsigned  Short intHour//Time    unsigned  Short intMinute//min    unsigned  Short intSecond//Sec Public: CTime (intH=0,intm=0,ints=0);voidSetTime (intHintMints);voidDisplay ();//Two purpose comparison operator overloading    BOOL operator> (CTime &t);BOOL operator< (CTime &t);BOOL operator>= (CTime &t);BOOL operator<= (CTime &t);BOOL operator= = (CTime &t);BOOL operator! = (CTime &t);///Two overloading of the add and subtract operators    //Returns the time, minutes, and seconds specified by T    //Example T1 (8,20,25), T2 (11,20,50), t1+t2 for 19:41:15BUILDoperator+ (CTime &t); BUILDoperator-(CTime &t);//contrast + understandingBUILDoperator+(ints);//Returns the time after S secondsBUILDoperator-(ints);//Returns the time before S seconds    overload of the//two-mesh assignment operatorCTime &operator+ = (CTime &c); CTime &operator-= (CTime &c); CTime &operator+=(ints);//Returns the time after S secondsCTime &operator-=(ints);//Returns the time before S seconds    //The overload of the unary operatorBUILDoperator++(int);//Post + +, next secondCTime &operator++();//Front + +, next secondCTimeoperator--(int);//back--, one secondCTime &operator--();//Front--, one second};

Tip 1: Not all comparison operations overload functions are complex

//比较运算返回的是比较结果,是bool型的true或false//可以直接使用已经重载了的运算实现新运算,例如果已经实现了 > ,则实现 <= 就可以很方便了……bool CTime::operator// 判断时间t1<=t2{    if (*thisreturnfalse;    returntrue;}

It can even be as concise as the following code:

bool CTime::operator <= (CTime &t){return !(*this > t)}

Tip 2: Not all compound assignment operation overloading functions need to be complex

//可以直接使用已经重载了的加减运算实现//这种赋值, 例如 t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值CTime &CTime::operator+=(CTime &c){    *this=*this+c;    return *this;}

Tip 3: Please make your own main () function for testing, some results do not rely on the display () function, promote the single step to view the results

"Project 3-Implementation of array class operations" reference solution
To design the array class arrays, in order to implement the functions required in the test function, make up the functions (constructs, destructors) and operator overloads of the related functions. Note the usage of the reference.
Implement the policy tip: You can add comments to the statement in the test function, uncomment the sentence, add the corresponding function, to gradually achieve all the functions, to avoid the difficulties of overall consideration.

classarray{Private:int*List;//For storage of dynamically allocated array memory first address    intSize//array size (number of elements) Public://member function declaration};//Require the test function to run the correct and reasonable results:intMain () {inta[8]= {1,2,3,4,5,6,7,8};intb[8]= {Ten, -, -, +, -, -, -, the}; Array Array1 (A,8), Array3,array4;ConstArray Array2 (b,8);    Array4=array3=array1+array2;    Array3.show (); Array4.resize ( -); array4[8]= About;cout<<array4[8]<<endl;cout<<array2[3]<<endl;return 0;}

"Construction of the project 4-string class" reference solution
Write a class that can handle the string, whose data members are as follows:

class String  {  public:   ...//需要的成员函数(若需要的话,声明友元函数)private:      char *p;   //指向存储的字符串     int len;   //记录字符串的长度  };  

Please construct the add and subtract operations of the string class. where S1 + S2 connects two strings, S1-S2 is the connection that removes the trailing spaces of S1 and the leading spaces of the S2.
Tip: There are pointer members, which should be noted at design time. This, you know.

Class 2015 C + + 12th Week Practice Project operator overloading (i)

Related Article

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.