1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Inheritance

Discussion in 'C++' started by zachry00, Feb 29, 2012.

  1. zachry00

    zachry00 New Member

    Joined:
    Jul 20, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    pls give me some examples so i could master it...:confused::confused::confused:

    thanks by the way..
     
  2. zachry00

    zachry00 New Member

    Joined:
    Jul 20, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    this is one example can you try to troubleshoot it... thanks

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<string>
    #include<cstdlib>
    using namespace std;
    
    void outputline(int, const string, const string,int, int);
    void showRecords();
    void inputRecords();
    int main(){
    
    int c;
    start:
    cout<<"Press:"
    <<"\n[1] Show Records"
    <<"\n[2] Input Records"<<endl;
    cin>>c;
    
    switch(c){
    case 1:
    showRecords();
    cout<<endl;
    goto start;
    break;
    case 2:
    inputRecords();
    system("cls");
    goto start;
    break;
    default:
    goto exit;
    }
    
    
    exit:
    cin.ignore();
    cin.get();
    }
    
    void showRecords(){
    ifstream inClientFile("Student.txt", ios::in); //open file for output
    
    int studentid,units,tuition;
    string name, course; 
    //print header
    cout<<left<<setw(15)<<"Student ID"<<setw(17)<<"Student Name"<<setw(10)<<"Prog Code"<<setw(10)<<"Units"<<setw(10)<<"Tuition"<<endl;
    
    //output the content of the file by calling the function
    while(inClientFile>>studentid>>name>>course>>units>>tuition)
    outputline(studentid, name, course,units,tuition);
    
    }
    
    void inputRecords(){
    
    //open the file for input
    ofstream outClientFile("Student.txt", ios::app);
    
    //check if the file exists
    if (!outClientFile)
    {
    cerr<< "File could not be opened.";
    exit (1);
    }
    //prompt the user to enter data
    cout<<"Enter the 10-digit student number, name, program code,no. of units"<<endl;
    cout<<"Enter end of file to end input."<<endl<<"?";
    
    int studentid,units,tuition;
    string name, course;
    //user will enter data
    
    tuition=units*250;
    while(cin>>studentid>>name>>course>>units>>tuition)
    {outClientFile << studentid <<" "<< name <<" "<< course<<" "<<units<<" "<<tuition<<endl;break;}
    }
    
    //this will iterate until there's data to read
    void outputline(int studentid, const string name, const string course, int units, int tuition)
    {
    cout<<left<<setw(15)<<studentid<<left<<setw(17)<<name<<left<<setw(10)<<course<<setw(10)<<units<<setw(10)<<tuition<<endl;
    }
     
  3. zachry00

    zachry00 New Member

    Joined:
    Jul 20, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    :confused:am i right?
     
  4. johnBMitchell

    johnBMitchell New Member

    Joined:
    Feb 17, 2011
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    CA, USA
    Home Page:
    Inheritance is sub class of main class and mostly it defines by class in common program. Your example is true but you can use class because it is easy to write and also understand. As example we define “class A†then enter “public:†into it, Now can define “class B : public Aâ€. This is called inheritance.
     

Share This Page