Why do you not place a space between a unary operator and its operand
It looks better:
-3.2 * y + -x
- 3.2 * y + - x
Can you set a string equal to a variable like string=variable+variable
Only if the variable are string variables. C++ does not convert numbers
to strings.
What about this nested if...
if(x<10)
if(y<10)
cout<< "The answer is less than 10";
else
cout<< "The number is greater than 10";
First match the else with the previous unmatched "if". And insert braces and
indents in the resulting if-else:
if(y<10)
{
cout<< "The answer is less than 10";
}
else
{
cout<< "The number is greater than 10";
}
Next put {} around the above and reassemble the whole thing
if(x<10)
{
if(y<10)
{
cout<< "The answer is less than 10";
}
else
{
cout<< "The number is greater than 10";
}
}
Now we can figure out what it "means" or does, may be an activity chart
may help
I also find that putting the possibilities
in a table helps me understand complex logic:
Table
| Condition
|
|---|
| x<0 | T | T | F
|
| y<0 | T | F | -
|
| Action
|
| Output <0 | X | - | -
|
| Output >0 | - | X | -
|
(Close Table)
Is there a way to put a function in loop and have it coming out not satisfying the condition
While-loops repeat until a condition is false. If you want the reverse
use the "not" Boolean operator like this
while( not ( condition ) ) ...
If you want the condition to repeat the loop to depend on something
that happens in the loop.... then use a variable to remember what happens
like this:
int condition=1;
while (condition)
{
...
if(....)
condition=0;
...
}
Using a Boolean variable (next chapter) helps clarify this.... so
do Boolean expressions (using not, and, or). For more see
[ FAQ.html#loop-and-a-half pattern ]
in the Frequently Asked Questions.
What is the difference between a controlled repetition and a counter controlled repetition
The presence of a counter -- a variable that is initialized before the loop,
tested at the start of each repetition, and either incremented or
decremented at the end of each repetition.
When defining a function for a class when are two colons used
Inside the class you don't need the colons -- the compiler can see which class
the function belongs to. Outside the class you need the colons to tell
the compiler which class the function belongs to.
Is it better to keep all class definitio0n in one header file or in multiple header files
As the problem gets more complex and solution demands more and more classes
the better it is to have multiple files -- one per class. A rough guide,
when you find yourself repeatedly scrolling up and down the file from class
to class... it would be wise to spit up the file.
What is the importance of attributes
Attributes reflect the properties of real objects. Most problems involve object with many properties... and so solution that have many attributes.
How do indicate the initial value of an attribute in UML
name : type = initialValue
How do indicate the initial value of an attribute in UML
You have to put the initialization into a constructor.
How many classes should you have
On for each type of object in the problem. You need to think about what
differences are important and so need to appear in code.
How many classes can you have
There is no limit.
How many attributes can you
As many as you want. No limit.
Do attributes in the model reflect the variables in the program
They represent the named data field in the classes.
Why does the book always use the ATM example
(1) To show you a realistic project from soup to nuts. (2) Thinking
up a new example is difficult. (3) So you don't have to learn about
a new situation in each chapter.
How do develop an error message when the wrong data is input
Very carefully! You end up with a lot of if-then-else logic
and some loops that ask the user to try again.
How is UML Boolean is equivalent to the bool C++ type
By design. The difference in name comes from C and a liking for
short names in typed code.
To types of data are equivalent if the same operations give similar results.
How is Boolean used in source code
(1) Conditions return the two Boolean value: true and false.
(2) You declare Boolean variables like this
bool ok;
(3) We can combine Booleans like this
Table| Example | Meaning
|
|---|
| not(A) | negation, opposite value, true<->false
|
| (A)and(B) | conjunction, true only if both are true.
|
| (A)or(B) | disjunction, true if one is true or if both
|
(Close Table)
What is a Boolean attribute
A Boolean attribute is a true/false property of an object. An example
for a Pet or a Person might be:
alive : Boolean
and we would code this as
bool alive;
inside class Pet or class Person... or even both