Chapter 11 : Virtual Functions
Chapter 12 : Streams and Files
ASSIGNMENT # 4
CSC241-OBJECT ORIENTED PROGRAMMING (OOP)
SUBMITTED BY:
DANISH RIASAT
SUBMITTED TO:
MR. ALI USMAN
REGISTRATION NO.
FA22-BCS-219
Department of Computer Science
COMSATS University Islamabad, Sahiwal Campus
Chapter 11
Question #1
#include <iostream> void getdata(){
#include <string> publication::getdata();
using namespace std; cout << "Enter playing
class publication{ time(in minutes): ";
private: cin >> time;
string title; }
float price; void putdata()
public: {
virtual void getdata(){ publication::putdata();
cout << "\nEnter title: cout << "\nPlaying time(in
"; minutes): " << time;
cin >> title; }
cout << "Enter price: "; };
cin >> price; int main(){
} publication* pubarr[100]; //array of ptrs
virtual void putdata(){ to pubs
cout << "\n\nTitle: " << int n = 0; //number of pubs in array
title; char choice; //user’s choice
cout << "\nPrice: " << do {
price; cout << "\nEnter data for book
} or tape (b/t)? ";
}; cin >> choice;
class book : public publication{ if( choice=='b' ){ //make book
private: object
int pages; pubarr[n] = new
public: book; // put in array
void getdata(){ }
publication::getdata(); else{ //make tape object
cout << "Enter number pubarr[n] = new tape; //
of pages: "; put in array
cin >> pages; }
} pubarr[n++]->getdata(); //get
void putdata(){ data for object
publication::putdata(); cout << " Enter another (y/n)?
cout << "\nPages: " << "; //another pub?
pages; cin >> choice;
} }
}; while( choice =='y'); //cycle until not ‘y’
class tape : public publication{ for(int j=0; j<n; j++){ //cycle thru all
private: pubs
pubarr[j]->putdata();//print data
float time; for pub
public: }
cout << endl;
return 0;
}
Output
Question #4
#include <iostream> void getdata() override {
#include <string> publication::getdata();
using namespace std; cout << "Enter playing time(in minutes): ";
cin >> time;
class publication { }
private:
string title; void putdata() override {
float price; publication::putdata();
cout << "\nPlaying time(in minutes): " <<
public: time;
virtual void getdata() { if (isOversize()) {
cout << "\nEnter title: "; cout << "\nOversize";
cin >> title; }
cout << "Enter price: "; }
cin >> price;
} bool isOversize() const override {
return time > 90;
virtual void putdata() { }
cout << "\n\nTitle: " << title; };
cout << "\nPrice: " << price;
} int main() {
publication* pubarr[100]; // array of ptrs to
virtual bool isOversize() const { pubs
return false; // Default implementation for int n = 0; // number of pubs in array
the base class char choice; // user’s choice
} do {
}; cout << "\nEnter data for book or tape
(b/t)? ";
class book : public publication { cin >> choice;
private: if (choice == 'b') { // make book object
int pages; pubarr[n] = new book; // put in array
} else { // make tape object
public: pubarr[n] = new tape; // put in array
void getdata() override { }
publication::getdata(); pubarr[n++]->getdata(); // get data for
cout << "Enter number of pages: "; object
cin >> pages; cout << " Enter another (y/n)? "; // another
} pub?
cin >> choice;
void putdata() override { } while (choice == 'y'); // cycle until not ‘y’
publication::putdata(); for (int j = 0; j < n; j++) { // cycle thru all
cout << "\nPages: " << pages; pubs
if (isOversize()) { pubarr[j]->putdata(); // print data for pub
cout << "\nOversize"; }
} cout << endl;
} return 0;
}
bool isOversize() const override {
return pages > 800;
}
};
class tape : public publication {
private:
float time;
public:
Output
Chapter 12
Question #1
#include <iostream> // open it for append
#include <fstream> // for file streams file.open("DIST.DAT", ios::binary |
using namespace std; ios::app | ios::out | ios::in );
class Distance { // English Distance class do{ // data from user to file
private: cout << "\nDistance";
int feet; dist.getdist(); // get a distance
float inches; // write to file
public: file.write( (char*)&dist,
Distance() : feet(0), sizeof(dist) );
inches(0.0) // constructor (no args) cout << "Enter another distance
{} (y/n)? ";
Distance(int ft, float in) : cin >> ch;
feet(ft), inches(in) // constructor (two args) }
{} while(ch=='y'); // quit on ‘n’
void getdist(){ // get length file.seekg(0); // reset to start of file
from user // read first distance
cout << "\n Enter feet: file.read( (char*)&dist, sizeof(dist) );
"; int count = 0;
cin >> feet; while( !file.eof() ){ // quit on EOF
cout << " Enter inches: cout << "\nDistance " << +
"; +count << ": "; // display dist
cin >> inches; dist.showdist();
} file.read( (char*)&dist,
void showdist(){ // display sizeof(dist) ); // read another distance
distance }
cout << feet << "\'-" << cout << endl;
inches << "''"; return 0;
} }
};
int main(){
char ch;
Distance dist; // create a Distance object
fstream file; // create input/output file
Output
Question #2
#include <iostream> while (getline(sourceFile, line)) {
#include <fstream> if (!(destFile << line << endl)) {
#include <string> cerr << "Error: Cannot write to
using namespace std; destination file '" << destinationFileName << "'"
int main() { << endl;
// Source and destination file names sourceFile.close();
const char* sourceFileName = "dan.txt"; destFile.close();
const char* destinationFileName = "f.txt"; return 1; // Exit with an error code
// Open the source file }
ifstream sourceFile(sourceFileName); }
if (!sourceFile.is_open()) {
cerr << "Error: Cannot open source file '" // Close the files
<< sourceFileName << "'" << endl; sourceFile.close();
return 1; // Exit with an error code destFile.close();
}
cout << "File copy successful." << endl;
// Open the destination file
ofstream destFile(destinationFileName); // Wait for user input
if (!destFile.is_open()) { cout << "Press Enter to exit...";
cerr << "Error: Cannot open destination cin.get();
file '" << destinationFileName << "'" << endl;
sourceFile.close(); // Close the source file return 0; // Exit successfully
return 1; // Exit with an error code }
}
// Copy the contents of the source file to the
destination file
string line;
Output
Before Copying
After Copying
Question #3
#include <iostream>
#include <fstream>
using namespace std;
int main() {
const char* filename = "Untitled3.cpp";
ifstream file(filename, ios::binary | ios::ate);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << :endl;
return 1;
}
streamsize size = file.tellg();
cout << "Size of file " << filename << ": " << size << " bytes" << endl;
file.close();
return 0;
}
Output
Question #4
#include <iostream>
#include <fstream> outFile.close(); // Close the ofstream object
#include <string>
using namespace std; // Open file for reading
int main() { ifstream inFile("employee_data.txt");
ofstream outFile("employee_data.txt",
ios::app); // Open file for appending if (!inFile) {
cerr << "Error opening file for reading." <<
if (!outFile) { endl;
cerr << "Error opening file for writing." << return 1;
endl; }
return 1;
} // Read and display all data from the file
string line;
char choice; cout << "\nEmployee Data from File:\n";
do { while (getline(inFile, line)) {
string firstName, middleInitial, lastName; cout << line << std::endl;
unsigned long employeeNumber; }
// Prompt user for input inFile.close(); // Close the ifstream object
cout << "Enter first name: ";
cin >> firstName; return 0;
cout << "Enter middle initial: "; }
cin >> middleInitial;
cout << "Enter last name: ";
cin >> lastName;
cout << "Enter employee number: ";
cin >> employeeNumber;
// Write data to file using formatted I/O
outFile << firstName << ' ' << middleInitial
<< ' ' << lastName << ' ' << employeeNumber
<< std::endl;
// Ask user if they want to enter more data
cout << "Do you want to enter more data?
(y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
Output