Common C Questions : TURBO C/C++ - Getting Started Answers to Common Questions
http://ingenuitydias.blogspot.com/2016/01/common-c-questions-turbo-cc-getting.html
Questions:What potential problems can arise from typecasting a base class pointer into a derived class pointer so that the derived class's member functions can be called?
TURBO C/C++: Installer and Installation
TURBO C/C++: Integrated EnvironmentVariables Pointer Array And Files
Arrays Linked List Sorting Wild Pointer
Questions:What's the difference between the keywords STRUCT and CLASS?
Answers:The members of a STRUCT are PUBLIC by default, while in CLASS,they default to PRIVATE. They are otherwise functionally equivalent.
[next]Questions:I have declared a derived class from a base class, but I can't access any of the base class members with the derived class function.
Answers:Derived classes DO NOT get access to private members of a base class.In order to access members of a base class, the base class members must be declared as either public or protected. If they are public, then any portion of the program can access them. If they are protected, they are accessible by the class members, friends, and any derived classes.
Questions:How can I use the Paradox Engine with C++?,
Answers:Because the Paradox Engine functions are all compiled as C functions you will have to assure that the names of the functions do not get "mangled" by the C++ compiler. To do this you need to prototype the Engine functions as extern "C". In the pxengine.h header file insert the following code at the lines indicated.
/* inserted at line # 268 */
#ifdef __cplusplus
extern "C" {
#endif
/* inserted at line # 732, just before the final #endif */
#ifdef __cplusplus
}
#endif
Questions:I have a class that is derived from three base classes. Can I insure that one base class constructor will be called before all other constructors?
Answers:If you declare the base class as a virtual base class, its constructor will be called before any non-virtual base class constructors. Otherwise the constructors are called in left-to-right order on the declaration line for the class.
Questions:Are the standard library I/O functions still available for use with the C++ iostreams library?
Answers:Yes, using
#include <stdio.h>
functions such as printf() and scanf() will continue to be available.
Questions:When debugging my program in Turbo Debugger, I notice that none of my inline functions are expanded inline. Instead, they are all done as function calls.
Answers:Whenever you compile your program with debugging information included,no functions are expanded inline. To verify that your inline functions are indeed expanding inline, compile a module with the -S flag of the command-line compiler, then examine the .asm file that is generated.
[next]Questions:In C++, given two variables of the same name, one local and one global,how do I access the global instance within the local scope?
Answers:Use the scope (::) operator.
int x = 10;
for(int x=0; x < ::x; x++)
{
cout << "Loop # " << x << "\n"; // This will loop 10 times
}
Questions:Will the following two functions be overloaded by the compiler, or will the compiler flag it as an error? Why?
void test( int x, double y); & int test( int a, double b);
Answers:The compiler will flag this as a re-declaration error because neither return types nor argument names are considered when determining unique signatures for overloading functions. Only number and type of arguments are considered.
Questions:If I pass a character to a function which only only accepts an int,what will the compiler do? Will it flag it as an error?
Answers:No. The compiler will promote the char to an int and use the integer representation in the function instead of the character itself.
Questions:I was trying to allocate an array of function pointers using the new operator but I keep getting declaration syntax errors using the following
syntax: new int(*[10])(); What's wrong?
Answers:The new operator is a unary operator and binds first to the int keyword producing the following: (new int) (*[10])(); You need to put parentheses around the expression to produce the expected results: new (int (*[10]());
Questions:What are inline functions? What are their advantages? How are they declared?
Answers:An inline function is a function which gets textually inserted by the compiler, much like macros. The advantage is that execution time is shortened because linker overhead is minimized. They are declared by using the inline keyword when the function is declared:inline void func(void) { cout << "printing inline function \n"; } or by including the function declaration and code body within a class:
class test
{
public:
void func(void) { cout << "inline function within a class.\n"}
};
Questions:If I don't specify either public or private sections in a class, what is the default?
Answers:A class will default all members to private if neither public nor private sections are declared.
Questions:What does the _seg modifier do?
Answers:Using _seg cause a pointer to become a storage place for a segment value, rather than an offset ( or a segment/offset ). For instance, if "int _seg *x" contains the value 0x40, then when you use "*x", the value pointed to will be at segment 0x40, offset 0. If you add a value to the pointer, the value is multiplied by the size of the pointer type. That new value is used as an offset, and is combined with the segment value contained in the pointer. For instance,
int _seg *x;
int value;
x = (int _seg *)0x40;
value = *(x + 20);
value is assigned the value of the integer at 0x40:0x28
(Remember, 20 * sizeof(int) = 40 = 0x28).
Questions: How can I output hex values in upper case using the iostream libraries?
Answers: Syntactically this is allowable. There is always a possibility of a base pointer actually pointing to a base class. If this is typecast to a derived type, the method being called may not exist in the base class. Therefore, you would be grabbing the address of a function that does not exist.
TURBO C/C++: Installer and Installation
TURBO C/C++: Integrated EnvironmentVariables Pointer Array And Files
Arrays Linked List Sorting Wild Pointer
Questions:What's the difference between the keywords STRUCT and CLASS?
Answers:The members of a STRUCT are PUBLIC by default, while in CLASS,they default to PRIVATE. They are otherwise functionally equivalent.
Answers:Derived classes DO NOT get access to private members of a base class.In order to access members of a base class, the base class members must be declared as either public or protected. If they are public, then any portion of the program can access them. If they are protected, they are accessible by the class members, friends, and any derived classes.
Questions:How can I use the Paradox Engine with C++?,
Answers:Because the Paradox Engine functions are all compiled as C functions you will have to assure that the names of the functions do not get "mangled" by the C++ compiler. To do this you need to prototype the Engine functions as extern "C". In the pxengine.h header file insert the following code at the lines indicated.
/* inserted at line # 268 */
#ifdef __cplusplus
extern "C" {
#endif
/* inserted at line # 732, just before the final #endif */
#ifdef __cplusplus
}
#endif
Questions:I have a class that is derived from three base classes. Can I insure that one base class constructor will be called before all other constructors?
Answers:If you declare the base class as a virtual base class, its constructor will be called before any non-virtual base class constructors. Otherwise the constructors are called in left-to-right order on the declaration line for the class.
Questions:Are the standard library I/O functions still available for use with the C++ iostreams library?
Answers:Yes, using
#include <stdio.h>
functions such as printf() and scanf() will continue to be available.
Questions:When debugging my program in Turbo Debugger, I notice that none of my inline functions are expanded inline. Instead, they are all done as function calls.
Answers:Whenever you compile your program with debugging information included,no functions are expanded inline. To verify that your inline functions are indeed expanding inline, compile a module with the -S flag of the command-line compiler, then examine the .asm file that is generated.
[next]Questions:In C++, given two variables of the same name, one local and one global,how do I access the global instance within the local scope?
Answers:Use the scope (::) operator.
int x = 10;
for(int x=0; x < ::x; x++)
{
cout << "Loop # " << x << "\n"; // This will loop 10 times
}
Questions:Will the following two functions be overloaded by the compiler, or will the compiler flag it as an error? Why?
void test( int x, double y); & int test( int a, double b);
Answers:The compiler will flag this as a re-declaration error because neither return types nor argument names are considered when determining unique signatures for overloading functions. Only number and type of arguments are considered.
Questions:If I pass a character to a function which only only accepts an int,what will the compiler do? Will it flag it as an error?
Answers:No. The compiler will promote the char to an int and use the integer representation in the function instead of the character itself.
Questions:I was trying to allocate an array of function pointers using the new operator but I keep getting declaration syntax errors using the following
syntax: new int(*[10])(); What's wrong?
Answers:The new operator is a unary operator and binds first to the int keyword producing the following: (new int) (*[10])(); You need to put parentheses around the expression to produce the expected results: new (int (*[10]());
Questions:What are inline functions? What are their advantages? How are they declared?
Answers:An inline function is a function which gets textually inserted by the compiler, much like macros. The advantage is that execution time is shortened because linker overhead is minimized. They are declared by using the inline keyword when the function is declared:inline void func(void) { cout << "printing inline function \n"; } or by including the function declaration and code body within a class:
class test
{
public:
void func(void) { cout << "inline function within a class.\n"}
};
Questions:If I don't specify either public or private sections in a class, what is the default?
Answers:A class will default all members to private if neither public nor private sections are declared.
Questions:What does the _seg modifier do?
Answers:Using _seg cause a pointer to become a storage place for a segment value, rather than an offset ( or a segment/offset ). For instance, if "int _seg *x" contains the value 0x40, then when you use "*x", the value pointed to will be at segment 0x40, offset 0. If you add a value to the pointer, the value is multiplied by the size of the pointer type. That new value is used as an offset, and is combined with the segment value contained in the pointer. For instance,
int _seg *x;
int value;
x = (int _seg *)0x40;
value = *(x + 20);
value is assigned the value of the integer at 0x40:0x28
(Remember, 20 * sizeof(int) = 40 = 0x28).
[next]
Questions: Can I statically allocate more than 64K of data in a single module?
Answers:Yes. Far data items are now supported:
char far array1[60000L];
char far array2[60000L];...
For arrays larger than 64k use: char huge array3[100000L]
Questions: Why do I get a "Type name expected" error on my definition of a friend class in my new class?
char far array2[60000L];...
For arrays larger than 64k use: char huge array3[100000L]
Questions: Why do I get a "Type name expected" error on my definition of a friend class in my new class?
Answers:You need to let the compiler know that the label you use for your friend class is another class. If you do not want to define your entire class, you can simply have "class xxx", where xxx is your label.
Questions: How can I output hex values in upper case using the iostream libraries?
Answers:You need to set the state of the stream using setf(). For example,
#include <iostream.h>
int main(void)
{
cout << hex;
cout << "\nNot upper-case : " << 255;
cout.setf(ios::upper-case);
cout << "\nUppercase : " << 255;
return 0;
}
Questions: What is the "this" pointer?
Answers: "this" is a local variable in the body of a non-static member function.It is a pointer to the object for which the function was invoked. It cannot be used outside of a class member function body.
Questions: Why does a binary member function only accept a single argument?
Answers: The first argument is defined implicitly.
Questions: What is a friend member function?
int main(void)
{
cout << hex;
cout << "\nNot upper-case : " << 255;
cout.setf(ios::upper-case);
cout << "\nUppercase : " << 255;
return 0;
}
Questions: What is the "this" pointer?
Answers: "this" is a local variable in the body of a non-static member function.It is a pointer to the object for which the function was invoked. It cannot be used outside of a class member function body.
Questions: Why does a binary member function only accept a single argument?
Answers: The first argument is defined implicitly.
Questions: What is a friend member function?
Answers: Declaring a friend gives non-members of a class access to the non-public members of a class.