Please disable adblock to view this page.

← Go home

File Operations, Derived Class & Virtual Class illustration (C++)

April 24, 2016
Published By : Pratik Kataria
Categorised in: ,

/*
Design a C++ base class consisting of the data members such as name of the student, 
rollnumber and subject. The derived class consists of the data members subject code,
internal assessment and university examination marks. Construct a virtual base 
class for the item name of the student and roll number. The program should have 
the facilities. i) Build a master table ii) List a table iii) Insert a new entry
iv) Delete old entry v) Edit an entry vi) Search for a record
*/



#include<iostream>
#include<stdio.h>
#include<fstream>
#include<cstdlib>
using namespace std;

class base_student
{
    protected:
    
    char stud_name[50],subj[20];
    int roll_num;
    public:
    
    virtual void getData();
    virtual void putData();

};

class derived_marks : protected base_student
{

    int subj_code;
    double int_assm, uni_marks;
    
    public:
    void getData();
    void putData();
    int getrollno();
};

void base_student::getData()
{

    cout<<"Enter Name–Subject–RollNum \n\t"<<endl;
    cin>>stud_name>>subj>>roll_num;

}

void base_student::putData()
{

    cout<<"The Student Details Are: "<<endl;
    cout<<"\n Student Name: "<<stud_name;
    cout<<"\n Roll Number: "<<roll_num;
    cout<<"\n Subject: "<<subj;

}

void derived_marks::getData()
{

    base_student::getData();
    cout<<"Enter SubjectCode–Internal Assessment–University Marks \n\t"<<endl;
    cin>>subj_code>>int_assm>>uni_marks;

}

void derived_marks::putData()
{

    base_student::putData();
    cout<<"\n Subject Code: "<<subj_code;
    cout<<"\n Internal Assessment: "<<int_assm;
    cout<<"\n University Marks: "<<uni_marks<<endl;

}

int derived_marks::getrollno()
{
    return (base_student::roll_num);
}

int main()
{
    int i,ch,n,r,flag=0;
    ofstream ofd;
    ifstream ifd;
    derived_marks d1,d2;
    cout<<"Enter number of records: ";
    cin>>n;
    derived_marks d[10];
    while(1)
    {
        cout<<"\nMenu: \n1)Enter Records \n2)Display Records \n3)Insert \n4)Search \n5)Delete \n6)Update \n7)Exit"<<endl;
        cin>>ch;
        switch(ch)
        {
            case 1:
            
                ofd.open("student_db.txt",ios::out|ios::trunc);
                for(i=0;i<n;i++)
                {
                    d[i].getData();
                    ofd.write((char*)&d[i],sizeof(d[i]));
                }
                ofd.close();
                break;
            
            case 2:
            ifd.open("student_db.txt",ios::in);
            while(ifd.read((char*)&d[i],sizeof(d[i])))
            {
                d[i].putData();
            }
            ifd.close();
            break;
            
            case 3:
                ofd.open("student_db.txt",ios::app);
                d2.getData();
                ofd.write((char*)&d2,sizeof(d2));
                ofd.close();
                break;
            
            case 4:
                cout<<"\nEnter Roll number you want to search: ";
                cin>>r;
                ifd.open("student_db.txt",ios::in);
                while(ifd.read((char*)&d1,sizeof(d1)))
                {
                    if(d1.getrollno() == r)
                    {
                    cout<<"\nRecord found!"<<endl;
                    d1.putData();
                    goto stop;
                }
                
                }
                cout<<"\nRecord does not exist!"<<endl;
                stop:
                ifd.close();
                break;
            
            case 5:
                cout<<"\nEnter Roll number you want to delete: ";
                cin>>r;
                ofd.open("temp.txt",ios::out|ios::trunc);
                ifd.open("student_db.txt",ios::in);
                while(ifd.read((char*)&d1,sizeof(d1)))
                {
                    if(d1.getrollno() == r)
                    {
                        cout<<"\nRecord has been deleted!"<<endl;
                        flag =1;
                    }
                    else
                    {
                        ofd.write((char*)&d1,sizeof(d1));
                    }
                    
                }
                if(flag==0)
                {
                    cout<<"Record does not exist!"<<endl;
                }
                remove("student_db.txt");
                rename("temp.txt", "student_db.txt");
                ifd.close();
                ofd.close();
                break;
            
            case 6:
                cout<<"\nEnter Roll number you want to update: ";
                cin>>r;
                ofd.open("temp.txt",ios::out|ios::trunc);
                ifd.open("student_db.txt",ios::in);
                while(ifd.read((char*)&d1,sizeof(d1)))
                {
                    if(d1.getrollno() == r)
                    {
                        d2.getData();
                        ofd.write((char*)&d2,sizeof(d2));
                        cout<<"\nRecord has been updated!"<<endl;
                        flag =1;
                    }
                    else
                    {
                        ofd.write((char*)&d1,sizeof(d1));
                    }
                    
                }
                if(flag==0)
                {
                    cout<<"Record does not exist"<<endl;
                }
                remove("student_db.txt");
                rename("temp.txt", "student_db.txt");
                ifd.close();
                ofd.close();
                break;
            
            default:
                exit(0);
        
        }
    }
    return 0;
}

View Article Page
Download

Pratik Kataria is a budding programmer, web designer and developer.