|
In object-oriented programming, why is combination better than inheritance in code reuse, for example, given the coordinates of two points to calculate the distance between two points, should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?
|
|
|
|
|
The question comes down to is distance fundemental to point or is it a function executed and that is up to how you intend to use it.
The general rule is function over inheritance and it's clearly covered by wikipedia
Composition over inheritance - Wikipedia[^]
If point has shapes that derive from it and distance is the closest point between the two shapes then we can provide a clear implementation of how it must work.
The only way to reconcile the closest point between two complex shapes will be to run each geometry (line,arc,spline etc) of each shape against the other shape or scanline the two shapes in a minimal box that includes both shapes. Either of those two processes will require these functions
1.) reset or seek so a geometry index will be the next out (seek_geometry(i))
2.) retrieve the next geometry from the shape (read_geometry(i))
3.) check if this geometry was last, if not back to 2 we go (check_geometry_end).
Essentially the geometry callback function(s) will be inherited from point but the distance will be another object because it will take two objects and run the process. Distance can then evolve to take a list of objects or different sorts of objects.
It is easy to see in that situation distance is not really a function of a shape it is a function between two shapes. However it could also be between a shape and something else such as a bounding box or grid or something yet to be thought of. So from a flexibility and reuse point of view you code for functionality and make point and distance two different objects.
In the simplest example it would make little difference but the functionality coding would make it easier to extend, reuse and more flexible. Ultimately you are more likely to reuse the more flexible code.
In vino veritas
modified 31-Mar-19 12:21pm.
|
|
|
|
|
元昊 潘 wrote: ...should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class? Is a vs. Has a
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Please help me with the solutions for this questions, am a beginner in C and am still learning the ropes of c programming:
1.write a method that accepts an array of integers as its parameter and returns the largest value.
2.given the <u>enum</u> below, write a method that accepts Gender as a parameter and returns the string "Sports" for male, "reading" for female. Otherwise return "acting".
3.Given the following marks results int [] marks = {23,14,32,15,17,35,19,12,21};
-write a for loop that will find the lowest mark
-write a while loop that will find the lowest marks
-write a do....while loop that will find the highest marks
the programming language to be used is c
|
|
|
|
|
Please explain exactly what help you are asking for. But do not expect someone to do your work for you. If you do not understand the assignment then you should discuss it with your teacher.
|
|
|
|
|
|
|
Member 14203327 wrote: Please help me... What do you have so far? Around these parts, posted code usually results in critiqued code.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
this is the code that i had but when i run it, it gives me a 0(zero) as the answer
#include<stdio.h>
//program for finding the lowest value in an array using a for loop
int main (){
int marks [] = {20,30,40,50,60}, a = 0, b = 1 , lowest ;
for (marks[a]<marks[b];b++;){
lowest = marks[a];
a++;
}
printf("the lowest number in the array is %d", lowest );
return 0;
}
|
|
|
|
|
Member 14203327 wrote: int marks [] = {20,30,40,50,60}, a = 0, b = 1 , lowest ;
for (marks[a]<marks[b];b++;){
Let's a look at your for loop. In C the for loop is defined as for( init; condition; increment) . Where init is the initial condition of the loop, condition is the end loop condition - that is if the condition expression returns true false, or non-zero, and increment is an expression that defines the next step of the loop. Lets look at your for loop:
init : marks[a] < marks[b] that's effectively a No-Op, it does evaluate to true, but it does not define the starting condition of the loop
condition : b++ I expect you know that in C false is zero and true is any non-zero value. Therefore this expression says return the value of b (= 1), then increment b, so now b = 2. At this point, the end-condition of the loop is 1, or true. that means that that the body of the loop (the portion between { and } never gets executed.. b will continue to be incremented until it gets to INT_MAX (2147483647), at which point it will roll over to INT_MIN (-2147483646) and continue incrementing until it reaches zero. However, once the value of b reaches 5 then the expression lower = marks[a] we get the dreaded undefined behavior, which, if the computer sets itself on fire, is perfectly acceptable, according to the C standard.
increment: empty expression - no action performed. That's not necessarily wrong, there's plenty of times that you might write a for loop without a increment expression. This is not one of those times.
Here's a template for what you should do for your for loop - I'll assume that you do not have C99 or C11, so you need to declare your loop variables outside the for loop:
int a;
int lowest = marks[0]
for( your code here;
your code here;
your code here) {
your code here
}
I hope this helps you understand where you went wrong and how to fix it.
Update: Doh Got the description of the condition 100% completely backwards. Please re-read and note strikeouts, corrections in italics follwing errors
modified 30-Mar-19 13:02pm.
|
|
|
|
|
thank you, it helped me to write the program properly
|
|
|
|
|
Member 14203327 wrote: for (marks[a]<marks[b];b++;){ Among other things, this may be part of your problem. A for() loop has three components: initialization, condition, increment. Any of them can be optional. In the initialization section, you have a condition. In the condition section, you have an increment.
A more straight-forward approach might look something like:
initialize lowest to some arbitrary high number (or assign it to the 1st number in marks, and then just loop 4 times)
loop 5 times
if the current number is less than lowest, re-assign lowest to it
repeat
print
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
|
After you figure out how to set-up the for () loop , add some debugging "scaffolding" in few places to see what your code is actually doing in each iteration of the (for) loop.
Perhaps adding some basic comments would also help.
Leaving variables intentionally uninitialized ( lowest ) is bad habit to get into, it will bite you when things get more complex.
Happy learning.
#define DEBUG
int main (){
int marks [] = {20,30,40,50,60}, a = 0, b = 1 , lowest ;
for (marks[a]<marks[b];b++;){
#ifdef DEBUG
printf (" a = %d ", a);
#endif
....
lowest = marks[a];
a++;
}
printf("the lowest number in the array is %d", lowest );
return 0;
}
<pre lang="c++">
|
|
|
|
|
So, I have this snippet of code that formats an html table data cell:
if(aSensor[i].sensorSwitchState == lowState) {
client->print("<td style='width:30px; background-color:lawngreen'>");
} else {
client->print("<td style='width:30px; background-color:red'>");
}
I have each background-color:<colorname> hardcoded and the subsequent printing output is as I was expecting. However, I would like to replace the <colorname> entry - lawngreen and/or red - with a const (or #define) so that the <colorname> entry is replaced with the color name.
I've tried
#define GREEN lawngreen
...
client->print("<td style='width:30px; background-color:GREEN'>")
and that doesn't work.
I've tried
const char acolor[]="lawngreen";
client->print("<td style='width:30px; background-color:acolor'>")
and that doesn't work.
I know there has to be a way to do this, but I can't seem to figure it out. Any ideas how I go about making a declaration that will let me insert the <color name=""> in the appropriate place would be greatly appreciated.
TNX
|
|
|
|
|
Try
#define GREEN "lawngreen"
client->print("<td style='width:30px; background-color:" GREEN "'>");
|
|
|
|
|
This
#define GREEN "lawngreen"
client->print("<td style='width:30px; background-color:" GREEN "'>") should work.
There might be more elegant ways, depending on print function definition.
|
|
|
|
|
To both K5054 and Cpallini:
Thanks, that gets the job done!!
It turns out I had tried
#define GREEN "lawngreen"
but then wrote the code escaping the quotes:
client->print("<td style='width:30px; background-color:\" GREEN \"'>");
I had also tried
client->print("<td style='width:30px; background-color:"GREEN"'>");
but that didn't work. Its the space after the leading quote that does the trick.
client->print("<td style='width:30px; background-color:" GREEN"'>");
The trailing quote doesnt need a space before it.
Gotta think on that one a bit (why the quotes and their spacing is important but escaping the quotes is not)
|
|
|
|
|
In C/C++ consecutive string literals are catenated together
cout << "hello " "world" << endl;
cout << "hello world" << endl; both produce the same thing. I don't know why the C++11 standard requires a space between a literal and a string macro, but it probably has something to do with ambiguous parsing when no space is present.
As for the escaped quotes, if you mean
client->print("<td style='width:30px; background-color:\" GREEN \"'>"); recall that \" embeds a double quote in a string, much like \n or \t embeds a new-line or a tab. If that's not what you were referring to, then maybe you could explain it better?
|
|
|
|
|
Frankly, I had forgotten the concatenation example you show.
As for your escaped quotes, yes, I was referring to the use of the \ to allow the explicit use of the the following ". I thought that since the contents of the print statement were already delineated by leading and trailing quotes, that I had to use the \ to permit the use of additional quotes between those two "bookend" quotes. Thanks for setting me straight.
Its been a good day - I learned something. Thank you!!
|
|
|
|
|
I Searched on internet for any library done by c++ for scan image but cannot found
any library for c++ as open source free
can any one help me to give me any source code or links by c++ for scan image by scanner
then get direct to computer
|
|
|
|
|
|
I am trying to make a app that will record the exact duration, and the sequence of keystrokes. it need to be able to record with very precise timing because I am planing on using it to create a way to record a game follow path. For example a driving game that earns you money for just diving around in free roam and if you follow a specific path you maximize the amount of money you earn. but i don't know what i need for the coding. Is there a way to code something like that?
|
|
|
|
|
Recording keystrokes is frowned upon, and illegal without the users' consent. That being said, you could use a keyboard-hook. That does record keyboard-strokes, not characters.
Not one of the easiest things to do, and not something I'm inclined to help with for cheating-purposes.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
well it is going to be an app so they will give the consent and i will not see the key strokes
|
|
|
|
|