C++/Classes and Objects
Expert: Joseph Moore - 7/29/2009
QuestionHi, I wrote a program to calculate the hypotenuse of given right triangle and its 2 sides using classes, here is the code:
#include <iostream>
#include <cmath>
using namespace std;
class Triangle {
double base, height;
public:
Triangle (double i, double j) { i = base; j = height; }
double hypot() { return sqrt(base * base + height * height); }
double area() { return base * height / 2.0; }
};
int main() {
Triangle one (4.0, 5.0);
Triangle two (10.23, 6.0);
cout << "The hypotenuse of Traingle one is: " << one.hypot() << "\n";
cout << "The area of Triangle one is: " << one.area() << "\n";
cout << "The hypotenuse of Triangle two is: " << two.hypot() << "\n";
cout << "The area of Triangle two is: "<< two.area() << "\n";
return 0; }
Unfortunately, strange numbers and characters just keep coming up, can you tell me why this happened and how can I fix it?
why does the following program display very wierd characters when I debug it while in the book, it is suppose to be some other characters?
#include <iostream>
using namespace std;
union u_type {
u_type (short int a) { i = a; }
u_type (char x, char y) { ch[0] = x; ch[1] = y; }
void showchars() {
cout << ch[0] << " ";
cout << ch[1] << "\n"; }
short int i;
char ch[2];
};
int main() {
u_type u (1000);
u_type u2 ('X', 'Y');
cout << "u as integer: " << u.i << "\n";
cout << "u as chars: " << u.ch;
u.showchars();
cout << "u2 as integer: " << u2.i << "\n";
cout << "u2 as chars: " << u.ch;
u2.showchars();
return 0; }
what is a unary, binary, ternary operations, how to differentiate them?
Also, I've noticed that I have a different style of programming than others. For example, I use
#include <iostream>
using namespace std;
while others use
#include <iostream.h>
using namespace std;
I understand that there is no fundamental difference between the 2, but sometimes, I just can't understand the code. Can you give me how the other codes differ?
thanks so much
AnswerHi again, Angela.
The problem with your triangle code is that you're assigning base to i and height to j, rather than i to base and j to height. It's the subtle difference between:
i = base; j = height;
and
base = i; height = j;
Essentially, you're leaving base and height uninitialized. Easy mistake to make.
-----
In your second program, you are treating the 2-character array as a string, which won't work. A string must have a NULL terminator to let the system know where the string ends. Because there is no NULL terminator, you end up printing out garbage memory. Remove the part where you send u.ch to the cout statement and simply let the showchars function work its magic:
cout << "u as integer: " << u.i << "\n";
cout << "u as chars: ";
u.showchars();
cout << "u2 as integer: " << u2.i << "\n";
cout << "u2 as chars: ";
u2.showchars();
-----
Unary operators are operators that take in one parameter. The ++ operator is an example of a unary operator. Binary operators require two parameters. Most operators in C++ are binary operators, such as your standard + operator. Ternary operators require three parameters, and there aren't many of these. In fact, to my knowledge, there's only one. It's a handy little operator, too. It's a shortcut for and if/else statement:
b == 1 ? a = 5 : a = 7;
The above statement is equivalent to:
if (b == 1)
a = 5;
else
a = 7;
-----
Coding style is extraordinarily subjective. Pretty much every professional programmer has a different coding style, and pretty much every professional programmer is dead-set that their coding style is the best. Consider the following:
if (b == 1) {
a = 5;
}
if (b == 1) a = 5;
if (b == 1) { a = 5; }
if (b == 1)
{
a = 5;
}
if (1 == b) {
a = 5;
}
if (1 == b) a = 5;
if (1 == b) { a = 5; }
if (1 == b)
{
a = 5;
}
Every single one of the statements above means the exact same thing. I can objectively say that some are easier to debug than others (one-line if statements are impossible to break on), but I can't say that one is *better* than another. I know what my preference is (I like the very last one), but I won't argue with someone that my way is better (even if i do have reasons for liking my method). It's just one of those things.
Your specific example, of <iostream.h> versus <iostream> is... well, it's complicated. All I can say is that it's generally preferable to include <iostream>. For an in-depth explanation, check out this article:
http://members.gamedev.net/sicrane/articles/iostream.html
As always, if you have any further questions, just let me know!