Need help with inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flash
    New Member
    • Nov 2006
    • 14

    Need help with inheritance

    I need to complete derived class and main class:
    derived class should have data member: major(string), grades: an array of integrs (size 5).
    it should have methods to:
    1-set and get the major.
    2-insert a grade in the grades array.
    3-compute the gpa of the 5 courses.
    THEN:
    MAIN METHOD THAT :
    1-Creat a diploma student.
    2-et the name of the student to a value that program gets from the user.
    3-sets itt gender and age after getting them from the user.
    4-sets the major a value that the user gets interactive.
    5-sets 5 grades to value that user inters interactively.
    6-displays the name of students his gender,his age and major.
    7-computes the GPA of this student.
    creates a second diploma student whoes name sami and age 21,his major math ,he has the same grade s as the fist student.Use message passing to assign grades to the second student.
    8-Display the grades of the first student and second student in a form of table of two rows.

    =============== =============== ==========
    #include <iostream>
    using namespace std;

    class student
    {
    protected:
    int id;
    string name;
    char gender;
    int age;
    public :

    student (){name=" ";gender=' ';age=0;id=0;}
    void setid(int s){id=s;}
    void setName(string n){name=n;}
    void setGender(char m){gender=m;}
    void setAge(int g){age=g;}
    int getid(){return id;}
    string getName(){retur n name;}
    char getGender(){ret urn gender;}
    int getAge(){return age;}

    };

    class dpstudent : public student{
    protected:
    string major;
    int grade[5];

    public:
    dpstudent(){ for(int i=0;i<5;i++)gra de[i]=0;major=' ';}
    void setmajor(string j){major=j;}
    void setGrade(int ag[]){ for(int i=0;i<5;i++) grade[i]=ag[i];}
    int getGrade(int i){return grade[i];}
    string getmajor(){retu rn major;}
    int computegpa(){in t m=0;int gpa=0;
    for(int i=0;i<5;i++) m=m+grade[i];
    gpa=m/5;
    return gpa;}
    };
    int main(){
    int grade[5];
    string name;
    char m;
    int age;
    string major;
    dpstudent f;
    cout<<"enter the name of frist student"<<endl;
    cin>>name;
    f.setName("name ");
    cout<<"enter the gender"<<endl;
    cin>>m;
    f.setGender('m' );
    cout<<"enter the age"<<endl;
    cin>>age;
    f.setAge(age);
    cout<<"enter the major"<<endl;
    cin>>major;
    f.setmajor("maj or");
    for(int i=0;i<5;i++){
    cout<<"enter the grade: ";
    cin>>grade[i];
    }
    for( i=0;i<5;i++)
    cout<<grade[i]<<endl;
    cout<<"the gpa of the 5 courses is"<<f.computeg pa()<<endl;
    return 0;
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    not sure what your problem is as you did not say

    however, noticed a couple of things in the last few lines of your program - you did not define i in the for() and you did not call setGrade() to set up the grade array ready for computegpa(). See lines marks // ** in the code below
    Code:
    for(int i=0;i<5;i++)   //** added int
    cout<<grade[i]<<endl;
    
    f.setGrade(grade);     // ** added line
    cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
    return 0;
    }

    Comment

    • flash
      New Member
      • Nov 2006
      • 14

      #3
      Originally posted by horace1
      not sure what your problem is as you did not say

      however, noticed a couple of things in the last few lines of your program - you did not define i in the for() and you did not call setGrade() to set up the grade array ready for computegpa(). See lines marks // ** in the code below
      Code:
      for(int i=0;i<5;i++)   //** added int
      cout<<grade[i]<<endl;
      
      f.setGrade(grade);     // ** added line
      cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
      return 0;
      }
      Thanks
      could u try this program in microsoft visual c++ 6.0 (1998).
      it is not working well.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by flash
        Thanks
        could u try this program in microsoft visual c++ 6.0 (1998).
        it is not working well.
        I don't have Visual C - I ran the program using DEV-C++, Cygwin g++ and Borland CBuilder 5 OK


        this is a run (input in bold)
        enter the name of frist student sam
        enter the gender m
        enter the age 23
        enter the major maths
        enter the grade: 30
        enter the grade: 40
        enter the grade: 50
        enter the grade: 60
        enter the grade: 70
        30
        40
        50
        60
        70
        the gpa of the 5 courses is50

        what happens when you run the program?
        what data are you using? - note that the name must be one word, i.e. Sam not Sam Jones.

        and here is the complete code
        Code:
        #include <iostream>
        using namespace std;
        
        class student
        {
        protected:
        int id;
        string name;
        char gender;
        int age;
        public :
        
        student (){name=" ";gender=' ';age=0;id=0;}
        void setid(int s){id=s;}
        void setName(string n){name=n;}
        void setGender(char m){gender=m;}
        void setAge(int g){age=g;}
        int getid(){return id;}
        string getName(){return name;}
        char getGender(){return gender;}
        int getAge(){return age;}
        
        };
        
        class dpstudent : public student{
        protected:
        string major;
        int grade[5];
        
        public:
        dpstudent(){ for(int i=0;i<5;i++)grade[i]=0;major=' ';}
        void setmajor(string j){major=j;}
        void setGrade(int ag[]){ for(int i=0;i<5;i++) grade[i]=ag[i];}
        int getGrade(int i){return grade[i];}
        string getmajor(){return major;}
        int computegpa(){int m=0;int gpa=0;
        for(int i=0;i<5;i++) m=m+grade[i];
        gpa=m/5;
        return gpa;}
        };
        int main(){
        int grade[5];
        string name;
        char m;
        int age;
        string major;
        dpstudent f;
        cout<<"enter the name of frist student"<<endl;
        cin>>name;
        f.setName("name");
        cout<<"enter the gender"<<endl;
        cin>>m;
        f.setGender('m');
        cout<<"enter the age"<<endl;
        cin>>age;
        f.setAge(age);
        cout<<"enter the major"<<endl;
        cin>>major;
        f.setmajor("major");
        for(int i=0;i<5;i++){
        cout<<"enter the grade: ";
        cin>>grade[i];
        }
        for(int i=0;i<5;i++)   //** added int
        cout<<grade[i]<<endl;
        
        f.setGrade(grade);     // ** added
        cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
        return 0;
        }

        Comment

        • flash
          New Member
          • Nov 2006
          • 14

          #5
          Originally posted by horace1
          I don't have Visual C - I ran the program using DEV-C++, Cygwin g++ and Borland CBuilder 5 OK


          this is a run (input in bold)
          enter the name of frist student sam
          enter the gender m
          enter the age 23
          enter the major maths
          enter the grade: 30
          enter the grade: 40
          enter the grade: 50
          enter the grade: 60
          enter the grade: 70
          30
          40
          50
          60
          70
          the gpa of the 5 courses is50

          what happens when you run the program?
          what data are you using? - note that the name must be one word, i.e. Sam not Sam Jones.

          and here is the complete code
          Code:
          #include <iostream>
          using namespace std;
          
          class student
          {
          protected:
          int id;
          string name;
          char gender;
          int age;
          public :
          
          student (){name=" ";gender=' ';age=0;id=0;}
          void setid(int s){id=s;}
          void setName(string n){name=n;}
          void setGender(char m){gender=m;}
          void setAge(int g){age=g;}
          int getid(){return id;}
          string getName(){return name;}
          char getGender(){return gender;}
          int getAge(){return age;}
          
          };
          
          class dpstudent : public student{
          protected:
          string major;
          int grade[5];
          
          public:
          dpstudent(){ for(int i=0;i<5;i++)grade[i]=0;major=' ';}
          void setmajor(string j){major=j;}
          void setGrade(int ag[]){ for(int i=0;i<5;i++) grade[i]=ag[i];}
          int getGrade(int i){return grade[i];}
          string getmajor(){return major;}
          int computegpa(){int m=0;int gpa=0;
          for(int i=0;i<5;i++) m=m+grade[i];
          gpa=m/5;
          return gpa;}
          };
          int main(){
          int grade[5];
          string name;
          char m;
          int age;
          string major;
          dpstudent f;
          cout<<"enter the name of frist student"<<endl;
          cin>>name;
          f.setName("name");
          cout<<"enter the gender"<<endl;
          cin>>m;
          f.setGender('m');
          cout<<"enter the age"<<endl;
          cin>>age;
          f.setAge(age);
          cout<<"enter the major"<<endl;
          cin>>major;
          f.setmajor("major");
          for(int i=0;i<5;i++){
          cout<<"enter the grade: ";
          cin>>grade[i];
          }
          for(int i=0;i<5;i++)   //** added int
          cout<<grade[i]<<endl;
          
          f.setGrade(grade);     // ** added
          cout<<"the gpa of the 5 courses is"<<f.computegpa()<<endl;
          return 0;
          }




          AFTER LINKING (MICROSOFT VISUAL C++):
          Compiling...
          asss.cpp
          c:\documents and settings\m8au\d esktop\assssss\ asss.cpp(50) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> >' (or there
          is no acceptable conversion)
          c:\documents and settings\m8au\d esktop\assssss\ asss.cpp(59) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> >' (or there
          is no acceptable conversion)
          c:\documents and settings\m8au\d esktop\assssss\ asss.cpp(65) : error C2374: 'i' : redefinition; multiple initialization
          c:\documents and settings\m8au\d esktop\assssss\ asss.cpp(61) : see declaration of 'i'
          Error executing cl.exe.

          asss.obj - 3 error(s), 0 warning(s)

          Comment

          Working...