|
 |
|
 |
10-27-2005, 09:05 PM
|
#1 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Another problem to tackle?
Just wanted to start a new thread to make it easier to manage. Can I have another problem I can try to work on, to try to better gather my understanding of C++. I think I'm pretty comfortable with functions and what not, I might be able to do some simple struct/class stuff, nothing too advanced, but I'd like to try something else on. I'll take any suggestions I can get.
|
|
|
10-27-2005, 09:41 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
write the previus problem as a class solution, with a friend istream &operator>>(istream &is, class_name &c) and friend ostream &operator<<(ostream &os, class_name c) to handle the read_in and print_out for the class.
Heres a short class example:
Code:
#include <iostream>
#include <string>
class removed{
private:
std::string orig_line; /* orriginal line from the user */
std::string del_line; /* orig_line after word has been removed */
public:
/* remove the string represented by s from orig_str */
bool remove(std::string &s){
std::cout << "Removing \"" << s << "\" from \"" << orig_line << "\"" << std::endl;
/* use s as a string (or char*) and remove any occurences from orig_line
* store the result in del_line and return true for success else return false
*/
};
/* read in from is to the orig_line */
friend istream &operator>>(istream &is, removed &r){
std::cout << "Please give the orriginal line of text: ";
std::cout.flush();
std::getline(is, r.orig_line);
return is;
};
/* print the final del_line with the word removed */
friend ostream &operator<<(ostream &os, removed r){
os << "The line: \"" << r.orig_line << "\" << std::endl;
os << "After deletion: \"" << r.del_line << "\" << std::endl;
return os;
};
};
int main()
{
removed r;
std::string word;
std::cin >> r;
std::cout << "Give word to be removed: ";
std::cout.flush();
std::cin >> word;
if(r.remove(word))
std::cout << r;
else
std::cout << "Error removing word \"" << word "\"" << std::endl;
return 0;
}
same goes as the first time, carefully read teh code and try to understand what is going on.
Note: none of the code above has been tested, it is written directly into the composer window, so it may or may not have syntactic errors.
|
|
|
10-27-2005, 09:57 PM
|
#3 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
I'll get on it tomorrow, it's getting kind of late and my brain is tired from that last example, but I'll see what I can do. Right off the bad I think I'm going to have to do some reading on how friends of a class work (access to the private data within the method), and the istream & ostream are going to be overloaded since we can't act on our "r" data type since it's a user-defined one, right? I think I remember seeing something like this with an example of adding roman numerals together. Anyways, sometime tomorrow I'll try to post what I came up with. Thanks redhead
|
|
|
10-27-2005, 10:05 PM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Quote:
|
the istream & ostream are going to be overloaded since we can't act on our "r" data type since it's a user-defined one, right?
|
Yes, since we require the standard in stream to send what ever is there into our removed class instance r, we are overloading the usual istream>>/ostream<< operators.
The questions about how the friend functions interact, should be covered in my example, atleast so the read-in; print-out shouldn't cause you any trouble..
But, while you're reading the code you should understand what is going on..
|
|
|
10-27-2005, 10:52 PM
|
#5 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
17 C:\Dev-cpp\main2.cpp ISO C++ forbids declaration of `istream' with no type
17 C:\Dev-cpp\main2.cpp `istream' is neither function nor member function; cannot be declared friend
and so on....I think it's not letting me accept these operators, I'll see what I can do about making the code work but i'm going to have to start it from scratch so I can understand what's going on, but i really need sleep  , I'll post my solution sometime tomorrow, thanks Red
|
|
|
10-28-2005, 03:29 AM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Maybe it should be:
Code:
friend std::istream &operator<<(std::istream &is, removed &r){
...
friend std::ostream &operator<<(std::ostream &os, removed r){
...
Unless you've added using namespace std; at the top..
|
|
|
10-28-2005, 07:16 AM
|
#7 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Yes that was the problem, for simplicity I've decided to use namespace std for now as to not complicate myself with these tasks, I am finding it too difficult to follow my errors with your control ideology, perhaps I will find use for it later on. Anyways, I'll rework the code and let you know what I come pu with
|
|
|
10-28-2005, 07:53 AM
|
#8 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Well I've re-done the layout as to make it easier for me to follow and whatnot, I only have left to do the remove function, which I should be able to implement the same science as the previous example, as we are still able to directly manipulate our r.orig_line and r.del_line, right? Here is my code so far:
Code:
#include <iostream>
#include <string>
using namespace std;
/*************************************************************************************
Class: removed
Purpose: The user will input a line of text, at which time he will be asked
to remove a word from the phrase. We will then proceed to removing
the word.
Methods: remove: it will perform the same task as our previous remove function
istream &operator>> : we must overload the input stream so that we can handle
our removed (r) data type on the lhs
ostream &operator<< : we must overload the output stream also, so that we can
manipulate r as a rhs variable.
**************************************************************************************/
class removed{
private:
string orig_line; //our string before mod
string del_line; //our new string after it has been altered
public:
friend istream &operator>>(istream &is, removed &r); //overloaded input
friend ostream &operator<<(ostream &os, removed &r); //overloaded output
bool remove(string &s); //same as last example, we'll take a referenced string to manipulate with
};
//What the Input Stream will be doing
istream &operator>>(istream &is, removed&r){
cout << "Please enter a line of text:";
cout.flush();
getline(is, r.orig_line);
return is;
}
//What the Output Stream will be doing
ostream &operator<<(ostream &os, removed&r){
os << "The line: " << r.orig_line << endl;
os << "The new line: " << r.del_line << endl;
return os;
}
bool removed::remove(string &s){
/* the coding for the remove will go here*/
cout << "Removing " << s << " from " << orig_line << " " << endl;
}
/* Function main */
int main()
{
removed r;
string word;
cin >> r;
cout << "What word would you like to remove: ";
cout.flush();
cin >> word;
if (r.remove(word))
cout << r;
else
cout << "Error removing word " << word << endl;
return 0;
}
Also, I've noticed that a few of the members here (including yourself) color code their code, is this done by manual process or can you get a script for it?
|
|
|
10-28-2005, 07:55 AM
|
#9 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
One other question I forgot to ask, I've read that writing code in-line is not a good idea for anything larger than 1-2 lines because of the obvious redundancy in trying to figure out what your data type is actually trying to encapsulate, and also because it will add increasing amounts of size to your *.exe as opposed to writing (scoped methods?) I think that's what they're called. Just wondering about it.
|
|
|
10-28-2005, 06:05 PM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
A modern compiler will inline for you automatically if needed. Inlining doesn't guarantee faster code. It only is a *hint* to the compiler that code can be optimized.
For now, more importantly, one should wonder WHAT a class "removed" is...
If you want to make a class that encapsulates specialized string manipulation algorithms, then give the class a proper name.
__________________
|
|
|
10-28-2005, 06:21 PM
|
#11 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Hmm, before you go any further, can you explain me what's wrong with my code DESIGN-WISE?
Code:
#include <iostream>
using namespace std;
int add_it(const int a, const int b)
{
std::cout << "Start Calculating."<<endl;
return a+b;
}
void show_array(int* array, std::size_t size)
{
for(std::size_t i = 0; i < size; ++i)
{
std::cout<<array[i]<<std::endl;
}
}
int main()
{
return 0;
}
__________________
|
|
|
10-28-2005, 06:57 PM
|
#12 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Quote:
|
Originally Posted by Valmont
Hmm, before you go any further, can you explain me what's wrong with my code DESIGN-WISE?
Code:
#include <iostream>
using namespace std;
int add_it(const int a, const int b)
{
std::cout << "Start Calculating."<<endl;
return a+b;
}
void show_array(int* array, std::size_t size)
{
for(std::size_t i = 0; i < size; ++i)
{
std::cout<<array[i]<<std::endl;
}
}
int main()
{
return 0;
}
|
1) firstly, you specified using namespace std, and provided to do std::cout and std::size_t everywhere.
2) in the for function, you are comparing size_t (which i think is a unsigned short initialized at 0 against a variable size which is also not initialized)
3) I don't believe the functions are related, or you are not showing any signs by leaving out both function calls in main. Those functions may as well not be there.
4) with a 1 liner for loop, I think it is preferrable to not use {} to encapsulate just the one line of code.
That's all I can gather from it...Am I on the right track?
|
|
|
10-28-2005, 07:06 PM
|
#13 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
I guess the other thing I missed was that with the names you have also, it's very easy to mistake (and not know) the purpose of the code, or where it is going at all. I guess i should avoid names such as array, size, word, a, b, i, etc, ? And make it something which will make sense in the future should I ever need to come back to the code and ever figure out what I was doing.
|
|
|
10-29-2005, 03:47 AM
|
#14 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
In the code above, both the functions are too stressed. You want functions to do as little as possible. Let them do one thing and one thing only. The warning message doesn't belong in "add_it()" so I moved it out of the function and place it somewhere else. The second function is trickier. Using the for-loop to loop through arrays for display reasons. Such a loop doesn't belong in a class that you are currently developing. So my original function was way too stressed as well: it did too much. Instead, lets split it up. Now we have a function that is responsible for fetching the element and one function needs to be written to control the display. And the latter surely doesn't belong in your class later. Perhaps a different "console_view" class or to keep it simple in simple cases, in "main()".
Code:
#include <iostream>
#include <cstddef>
using namespace std;
int add_it(const int a, const int b)
{
return a+b;
}
int get_element(int* array, unsigned idx)
{
return array[idx];
}
int main()
{
std::cout << "Start Calculating."<<endl;
std::cout << add_it(4,3) << std::endl;
return 0;
}
Now observe YOUR code that you were making for redhead. You'll be able to be your own judge for a gread deal now. Make the changes and then finalize redhead's homework. Make him proud and never say I was here  .
P.S.
You want functions to do as little as possible.
__________________
Last edited by Valmont; 10-29-2005 at 08:01 AM.
|
|
|
10-29-2005, 08:36 AM
|
#15 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Quote:
|
Originally Posted by Valmont
You want functions to do as little as possible.The second function is trickier. Using the for-loop to loop through arrays for display reasons.
|
Should I include only functionality within a function (that sentence somehow makes more sense now  ), and anything that is related to program flow I should extract out of my functions, and place it somewhere in main? And the next step being make sure main is readable and not throw 120 lines into there, but only the meat and potatoes?
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 02:22 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|