Skip to content
Question

Q.Write a user-defined function TotalPrice() in C++ to read each object of a binary file STOCK.DAT, and display the Name from all such records whose Price is above 150. Assume that the file STOCK.DAT is created with the help of objects of class Stock, which is defined below: class Stock { char Name[20]; float Price; public: char* RName() { return Name; } float RPrice() { return Price; } };

(OR)
A binary file DOCTORS.DAT contains records stored as objects of the following class: class Doctor { int DNo; char Name[20]; float Fees; public: int *GetNo() { return DNo; } void Show() { cout<<Dno<<" * " <<Name<< " * " <<Fees<<endl;} }; Write definition for function Details(int N) in C++, which displays the details of the Doctor from the file DOCTORS.DAT, whose DNo matches with the parameter N passed to the function.
CBSECBSE Class XII Board 2019Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): TotalPrice() reads STOCK.DAT and prints the Name of every item with Price > 150.

Part (b): Details(int N) reads DOCTORS.DAT and shows the doctor whose DNo equals N.

Both functions use binary file input with filtering: open with ios::binary, read whole objects with read((char*)&obj, sizeof(obj)) in a while loop (the stream is false at end of file), test a condition, and output the matches.

Part (a)

The Stock class exposes RName() and RPrice(). Read each object and print the name when the price is above 150.

void TotalPrice() {
    ifstream fin("STOCK.DAT", ios::binary);
    Stock S;
    while (fin.read((char*)&S, sizeof(S))) {
        if (S.RPrice() > 150)
            cout << S.RName() << endl;
    }
    fin.close();
}
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.