1. Answer the following short questions: a. What is the primary benefit of using
ID: 3581940 • Letter: 1
Question
1. Answer the following short questions:
a. What is the primary benefit of using virtual functions?
b) Discuss the differences between working with text files, and working with binary files, paying particular attention to the different methods for reading data from and writing data to a file and the int parameter needed in binary operations.
c) Describe the two relationships between classes that we have discussed and used. Hint: they both start with a short verb, and end with “-a”.
d) Describe the three different ways used to represent C-strings. How do we denote the end of a C-string?
Explanation / Answer
Answer:-
a.)
In object-oriented programming, when a derived class inherits from a base class, an object of the derived class may be referred to via a pointer or reference of the base class type instead of the derived class type. If there are base class methods overridden by the derived class, the method actually called by such a reference or pointer can be bound either 'early' (by the compiler), according to the declared type of the pointer or reference, or 'late' (i.e., by the runtime system of the language), according to the actual type of the object referred to.
Virtual functions are resolved 'late'. If the function in question is 'virtual' in the base class, the most-derived class's implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. If it is not 'virtual', the method is resolved 'early' and the function called is selected according to the declared type of the pointer or reference.
Virtual functions allow a program to call methods that don't necessarily even exist at the moment the code is compiled.
In C++, virtual methods are declared by prepending the virtual keyword to the function's declaration in the base class. This modifier is inherited by all implementations of that method in derived classes, meaning that they can continue to over-ride each other and be late-bound. And even if methods owned by the base class call the virtual method, they will instead be calling the derived method.
b.) The Text file is intended to be edited - or at least editable - by humans but often processed by other types of programs, such as programming language code or configuration files. They may use human-readable grammars such as a programming language grammar or something like XML. What makes them "human readable" is that text file editors and text file grammars typically avoid characters that don't map to alphanumeric characters, punctuation, or white space. Text files are chock full of bytes that do follow some rules.Text files have "lines" of data (a sequence of bytes, one after the other) that remains inside the printable range of Decimal 32.
The Binary-format files contain bytes that aren't intended to map to human-interesting characters. The bytes in them are intended to be used by computer programs. One important class of binary files is program executables, which have a specific format that can be "read" by the program loader when the program starts up, and contain machine-language instructions that are executed to run the program. With a binary file you have no concept of "records" - neither repeating fix-length data records, nor a stream of data that follows agreed-upon rules/restrictions for data ranges/values which then permit us to implement variable-sided records (more on this below).
Binary files are chock full of bytes that have values ranging all the way from 0...255. There are no guarantees with respect to the data following any easily-predictable rules.
d.)Method 1:
Method 2:
Denote the end of a C-string
'' represents the end of the string. It is also referred as String terminator & Null Character.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.