C++ Crash Course: a fast-Paced Introduction



Yüklə 7 Mb.
Pdf görüntüsü
səhifə27/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   23   24   25   26   27   28   29   30   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

xxxix
N O T E
 
If you’re not familiar with x86 assembly, refer to The Art of Assembly Language
2nd Edition, by Randall Hyde and Professional Assembly Language by 
Richard Blum.
Upgrading to Super C
Modern C++ compilers will accommodate most of your C programming 
habits. This makes it easy to embrace a few of the tactical niceties that the 
C++ language affords you while deliberately avoiding the language’s deeper 
themes. This style of C++—let’s call it Super C—is important to discuss for 
several reasons. First, seasoned C programmers can immediately benefit 
from applying simple, tactical-level C++ concepts to their programs. Second, 
Super C is not idiomatic C++. Simply sprinkling references and instances of 
auto
around a C program might make your code more robust and readable, 
but you’ll need to learn other concepts to take full advantage of it. Third, in 
some austere environments (for example, embedded software, some operat-
ing system kernels, and heterogeneous computing), the available tool chains 
have incomplete C++ support. In such situations, it’s possible to benefit from 
at least some C++ idioms, and Super C is likely to be supported. This section 
covers some Super C concepts you can apply to your code immediately.
N O T E
 
Some C-supported constructs won’t work in C++. See the links section of this book’s 
companion site, https://ccc.codes.
Function Overloading
Consider the following conversion functions from the standard C library:
char* itoa(int value, char* str, int base);
char* ltoa(long value, char* buffer, int base);
char* ultoa(unsigned long value, char* buffer, int base);
These functions achieve the same goal: they convert an integral type to 
a C-style string. In C, each function must have a unique name. But in C++ 
functions can share names as long as their arguments differ; this is called 
function overloading. You can use function overloading to create your own 
conversion functions, as Listing 3 illustrates.
char* toa(int value, char* buffer, int base) {
--
snip
--
}
char* toa(long value, char* buffer, int base) 
--
snip
--
}
char* toa(unsigned long value, char* buffer, int base) {
--
snip
--
}


xl
An Overture to C Programmers
int main() {
char buff[10];
int a = 1; 
u
long b = 2; 
v
unsigned long c = 3; 
w
toa(a, buff, 10);
toa(b, buff, 10);
toa(c, buff, 10);
}
Listing 3: Calling overloaded functions
The data type of the first argument in each of the functions differs, so 
the C++ compiler has enough information from the arguments passed into 
toa
to call the correct function. Each 
toa
call is to a unique function. Here, 
you create the variables 
a
u

b
v
, and 
c
w
, which are different types of 
int
objects that correspond with one of the three 
toa
functions. This is more con-
venient than defining separately named functions, because you just need to 
remember one name and the compiler figures out which function to call.

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   23   24   25   26   27   28   29   30   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin