TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTOPHER BUTLER
from: DANIEL JONES
date: 1997-08-02 10:03:00
subject: C++ Confusion

CB> There are a few parts of C++ that confuse me, and I was wondering if
CB> people could explain them to me (not so much what they do, but how t
CB> done):
CB> 1) the ? : thing
CB> an example I spotted in a message in here: regs.x.bx == 0xffff ? 2 :
The ?: is called the ternary operator. It's really quite simple.
x = a ? y : z;
can be rewritten as 
if(a)
    x = y;
else
    x = z;
If a is true, the operator returns y. If a is not true, it returns z.
Note that you don't have to use it to assign a value.  You can also do
something like this:
printf("%s", a ? "a is true" : "a is false");
CB> 2) templates
templates are patterns that the compiler can use to generate a function.
A quick example:
template 
T Square(T t)
{
    return t * t;
}
Here, we tell the compiler how to build a function to square a number.
The T is a placeholder for whatever type of argument we actually use.
We tell the compiler "Take whatever type of variable is passed in,
multiply it by itself, and return the same type of variable." Note that 
declaring a template doesn't create a function.  It's only when we
actually use the function that the compiler refers back to the pattern
to learn how to build it.
int i, j = 5;
i = Square(j); //Compiler builds a Square() func accepting an int
long l, m = 10;
l = Square(m); //Compiler builds a Square() func accepting a long
int a, b = 15;
a = Square(b); //Compiler doesn't build a function.  One accepting an
               //int already exists.
Of course, there's lots more to templates than this.  Entire books have
been written about them.
CB> 3) deriving classes
Deriving classes is inheriting the properties of another class.
class NewClass : public OldClass
{
    //definition
}
OldClass is a class which has member variables and functions.  NewClass
has those same variables and functions, plus any new ones you add in the
class definition.  (Depending upon the access specifiers used, ie
public, protected and private, some of those variables and functions may
not be directly accessible.)
Regards,
Daniel              ddjones@pinn.net
---
 þ RM 1.31 1604 þ "What the hell was THAT?"--Mayor of Hiroshima.Aug.6th.'45
---------------
* Origin: Selective Source Virginia Beach, VA (757)471-6776 (1:275/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.