1. Answer the following short questions: a. What is the primary benefit of using
ID: 3581802 • 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
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream file ("example.txt");
if (file.is_open())
{
file << "This isan example line. ";
file << "This is another example line. ";
file.close();
}
else cout << "Unable to open file";
return 0;
}
reading data from a file we usually use cin:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream file ("example.txt");
if (file.is_open())
{
while ( getline (file,line) )
{
cout << line << endl;
}
file.close();
}
else cout << "Unable to open file";
return 0;
}
in binary files :
ofstream ios::out //used to write on files
ifstream ios::in //used to read from files
fstream ios::in | ios::out // used for both reading and writing
answer to d):in c the strings are also represented in the same way as arrays but the only difference is they are of char type
by using arrays:
char c[]="abcd" ;
char c[20]="abcd";
char c[]={'a','b','c','d',''};
char c[5]={'a','b','c','d',''};
by using pointers"
char *c="abcd";
we denote the end of the string by using a null charecter "" which is appended at the end of each string
answer to c)the relationships between classes are 3 types
1.association
2.aggregation
3.generalization
note:but with hint i am a bit confused if you give some more clarification i will write it
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.