From: "Christina Weber" Newsgroups: comp.os.msdos.djgpp Subject: function overloading and inheritance help Lines: 176 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Message-ID: Date: Sun, 25 Aug 2002 23:20:10 GMT NNTP-Posting-Host: 24.29.120.144 X-Complaints-To: abuse AT rr DOT com X-Trace: twister.nyc.rr.com 1030317610 24.29.120.144 (Sun, 25 Aug 2002 19:20:10 EDT) NNTP-Posting-Date: Sun, 25 Aug 2002 19:20:10 EDT Organization: Road Runner - NYC To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com /* i need help with this program I am doing. I am very confused with function overloading and inheritance. this is what the program is supposed to do: a person will be prompted for either a print ad or web ad. if it is a print ad, it is .50 per character and if its a web ad, it is .25 per character. the person can then enter the ad and the fee will be calculated. also, there is a derived class for an international post. if the ad is INT, there is an extra $40 for the ad. at the end, the advertisement will be shown on screen along with the fee of the advertisemnet. it sounds very easy to me, but i'm just so confused with correct syntax of function overloading and how to implement them. any help is very appreciated. thank you. */ #include #include #include class JobPost //a class declaration for job post ad online or print { friend JobPost operator + (JobPost, JobPost&); /* will return the sum of two ads for the two objects passed and returned an object with the new cost */ friend int operator > (JobPost&, JobPost&); /* returns 1 for true, 0 for false. This function will support a statement like if Object1 > Object2, note the attribute compared is the adptr */ friend ostream& operator << (ostream&, JobPost&); //this function will support a statement like cout << Obj1; protected: int numpost; char posttype[1]; static double webrate; static double printrate; char *adptr; double adprice; //will hold the price for the ad public: JobPost(char pt[], char *ptr) { strcpy(posttype, pt); adptr = new char[strlen(ptr)+1]; strcpy(adptr,ptr); //use this adptr to do comparison } calcfee() { if (posttype='w') adprice = strlen(adptr)*webrate; else (posttype='p') adprice = strlen(adptr)*printrate; } /* based on the post type, function will calculate the fee for the ad. Online and print cost .25 cents and .50 cents per character respectively. */ }; double JobPost::webrate=.25; //per character double JobPost::printrate=.50; //per character /* this function is not a class member function but it returns a class object when called. */ JobPost operator + (const JobPost& obj1, const JobPost& obj2) { JobPost Temp_JobAd; TempAd.adprice = obj1.adprice + obj2.adprice; return Temp_JobAd; } JobPost::ostream& operator << (ostream& os, JobPost& obj1) { os << "Post Type is " << obj1.posttype; os << "The price is " << obj1.adprice; os << "The ad is " << obj1.adptr; return os; } friend int operator > (const JobPost& obj1, const JobPost& obj2) { if (Obj1.adptr > Obj2.adptr) return 1; else return 0; JobPost GetAd() /* returns a job post object by prompting for posttype web(w) or print(p), the ad itself which will be stored in the adptr. */ cout << "Web Type = w\n"; cout << "Print Type = p\n"; cout << "Please enter w or p for ad type"; cin >> adtype; strcpy(posttype,adtype); strcpy(adptr,ad); { char ad[4000]; /* prompt and instantiate the object using the values acquired and return the object */ cout << "Enter the advertisement"; cin >> ad; JobPost post (adtype,ad); return post; } //i'm not sure what to do here or if this actually goes here class internationalPost : public JobPost { public: internationalPost(char pt1[], char *ptr1); JobPost::calcfee(); }; void main() { cout << setprecision(2); cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); /* create 1-3 objects by envoking the GetAd method and write the code to use the above functions like calcfee, the three operator overloading. I will create the first object. You should write the code to get the object fee by calling the appropriate function. Create the other two objects. Then envoke the operator overloading functions to compare two objects, sum two objects and display a whole object using the cout statement. */ JobPost obj1=GetAd(); //my first object. calcfee(); JobPost obj2=GetAd(); JobPost obj3=GetAd(); JobPost::JobPost operator + (JobPost, JobPost&); JobPost::operator > ( JobPost&, JobPost&); JobPost::ostream& operator << (ostream&, JobPost&); } __________________________________________________________________ christina ICQ#: 6550798 Current ICQ status: + More ways to contact me __________________________________________________________________