Classes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Classes

 
0
  #41
Apr 17th, 2005
>Wich do you consider being a good (affordable) compiler?
If you want an IDE then Dev-C++ is both free and good. The latest VC++ is only $99US for the standard edition, and Borland Builder is also $99US. Other options are Borland 5.5, which is free, or Comeau, probably the most conforming C++ compiler available (available for $50US).

>Would you have done this in a separate function or like I did in void print()?
It depends on your needs. What you have is a valid solution.

>Is there a way that I could alter this code so that I could write
>_ (1_2 <-- underneath eachother) in a relative simple way?
Unless something like this is acceptable then no:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<< 1 <<"\n-\n"<< 2 <<endl;
  8. }
>Is there a way to decrease the amount 'cout' in main but still have the possibility to write a destinctive name?
Can you rephrase that?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Classes

 
0
  #42
Apr 17th, 2005
>It depends on your needs. What you have is a valid solution
Then I'll leave it as it is.

>Unless something like this is acceptable then no:
Then I'll leave it as it is.

>Can you rephrase that?
Is there a way that when I write the operator I'm using in main it automatically writes a piece of code that explains what I'm going to do?

Like for instance when I type: c = fract1 * fract2; when executed, I would get automatically: Multiplied: - 1/2.

This way, I don't have to write cout's everytime I use a version of the operators.
Could this be done by writing the couts in the memberfunctions themselves?

I was thinking of grouping the text into one member function and depending on wich operator is used in main, call that particular piece of text?

Does that make sence :-|
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Classes

 
0
  #43
Apr 17th, 2005
>Could this be done by writing the couts in the memberfunctions themselves?
Yes, though it's generally a better idea to avoid output in value producing member functions. A fraction class can be useful, and you may not want operator* to write to cout if you use the class in future projects.

Though a clean solution is to think polymorphically from the start:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class original_test {
  6. public:
  7. original_test ( int init ): data ( init ) {}
  8. public:
  9. virtual void print() const { cout<< data; }
  10. private:
  11. int data;
  12. };
  13.  
  14. class new_test: public original_test {
  15. public:
  16. new_test ( int init ): original_test ( init ) {}
  17. public:
  18. virtual void print() const { cout<<"Print: "; original_test::print(); }
  19. };
  20.  
  21. int main()
  22. {
  23. original_test t1 ( 10 );
  24. new_test t2 ( 20 );
  25.  
  26. t1.print(); cout<<endl;
  27. t2.print(); cout<<endl;
  28. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Classes

 
0
  #44
Apr 17th, 2005
Polymorphism, hmmm...polymorphism oh... yep found it --> Chapter 7 Section 4 :cheesy:

Guess It'll have to wait abit before I get there, but, great to hear there is a way to avoid so many cout's

Well, I'm starting Chapter 5 now, Templates, pointers and datastructures :!:

Thanks for the help Narue, going to read now about 40 pages, after that, it's exercise time again

Class solved.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Classes

 
0
  #45
Apr 17th, 2005
>Well, I'm starting Chapter 5 now, Templates, pointers and datastructures
Templates work too :
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct noverbose {
  6. static void get() {}
  7. };
  8.  
  9. struct verbose {
  10. static void get() { cout<<"Get: "; }
  11. };
  12.  
  13. template <typename policy = noverbose>
  14. class test {
  15. public:
  16. test ( int init ): data ( init ) {}
  17. public:
  18. virtual void print() const { policy::get(); cout<< data; }
  19. private:
  20. int data;
  21. };
  22.  
  23. int main()
  24. {
  25. test<> t1 ( 10 );
  26. test<verbose> t2 ( 20 );
  27.  
  28. t1.print(); cout<<endl;
  29. t2.print(); cout<<endl;
  30. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5565 | Replies: 44
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC