multiple definition, undefined reference to

This is a discussion on multiple definition, undefined reference to within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm having trouble with a project of mine, I keep getting theese undefined reference to and multiple definitions of-errors... ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6

    multiple definition, undefined reference to

    Hi! I'm having trouble with a project of mine, I keep getting theese undefined reference to and multiple definitions of-errors... Could someone please take a look at it and help me with it?

    my code:
    main.cpp
    Code:
    #include <iostream>
    #include "student.h"
    #include "calc.h"
    #include "calc.cpp"
    using std::cin;
    using std::cout;
    using std::endl;
    
    bool menu();
    
    int main(int argc, char *argv[])
    {
    klass klass1; // Initiera ett objekt, "Klass", av klassen Klass
    int SEL;
    cout << "Menu selection:\n1.Add student data\n2.Randomize student data\n3.Exit" << endl;
    cin >> SEL;
    switch(SEL)
    {
    case 1:
    klass1.addstudent();
    break;
    case 2:
    break;
    case 3:
    return 0;
    default:
    cout << "ERROR" << endl;
    }
    
    klass1.collect();
    klass1.calcaverage();
    cout << "de " << klass1.numBo() << " killarnas medel \x84r " << klass1.AvGradeBoys() << "\n\n";
    cout << "de " << klass1.numGi() << " tjejernas medel \x84r " << klass1.AvGradeGirls() << "\n\n";
    cout << "de " << (klass1.numGi()+klass1.numBo()) << " studenternas medel \x84r " << klass1.averagegrade() << endl;
    
    cin.ignore(255,'\n');
    cin.get();
    return 0;
    }
    calc.cpp
    Code:
    #include "calc.h"
    
    klass::klass() : numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0)
    {
    }
    
    klass::~klass()
    {
    }
    calc.h
    Code:
    #ifndef CALC_H
    #define CALC_H
    
    #include "student.h"
    #define students 20
    
    
    class klass{
    public:
    klass();
    ~klass();
    float AvGradeGirls(){return AvGradeG;};// medelbetyg för tjejer
    float averagegrade(){return totGrade;}; // medelbetyg
    float AvGradeBoys(){return AvGradeB;}; // medelbetyg för killar
    void addstudent();
    int numBo(){return numB;}; // Antal B
    int numGi(){return numG;}; // Antal G
    int totalStudents(){return numG+numB;}; // Totala antalet studneter
    void collect();
    void calcaverage();
    protected:
    private:
    student stud[students];
    int Ggrade;
    int Bgrade; // Tjejsor och killsars sammanlagda betyg (ej medelvärde)
    int numG;
    int numB;
    double AvGradeB; // Gossars betyg
    double AvGradeG; // Flickors betyg
    double totGrade; // Totala betyget
    };
    
    void klass::collect()
    {
    
    for(int i=(numG+numB);i>0;i--)
    {
    if(stud.getGender()) // om getGender() är sann = tösabete...
    {
    Ggrade=Ggrade+stud.getGrade();
    numG++;
    }
    else // ...annars en hane
    {
    Bgrade=Bgrade+stud.getGrade();
    numB++;
    }
    }
    }
    void klass::calcaverage()
    {
    if(numG>0) // Inte dela med noll
    AvGradeG=Ggrade/numG;
    if(numB>0) // inte dela med noll
    AvGradeB=Bgrade/numB;
    if((numB+numG)>0) // inte dela med noll
    totGrade=(Bgrade+Ggrade)/(numB+numG);
    }
    #endif // CALC_H
    Student.h
    Code:
    /* Skapar elever, tilldelar dem kön samt betyg */
    
    
    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include <iostream>
    
    class student
    {
    public:
    student();
    virtual ~student();
    void setGender(); // FALSE=boy, TRUE=girl
    void giveGrade();
    float getGrade(){return grade;};
    bool getGender(){return gender;};
    protected:
    private:
    bool gender;
    double grade;
    };
    
    
    
    // Funktioner
    
    void student::setGender()
    {
    bool sex;
    std::cout << "Specify sex of student; 1 (girl) or 0 (boy): ";
    std::cin >> sex;
    gender=(sex=0?0:1);
    }
    
    void student::giveGrade()
    {
    std::cout << "Specify student grade: (1-5) ";
    std::cin >> grade;
    }
    
    #endif // STUDENT_H

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    In your main( ) funcition:
    Code:
    #include "calc.cpp"
    You do not need to include the .cpp implementation of your class. Your compiler will automatically look for and include the .cpp file with the same name as the .h header file (if one exists)


    Also, in calc.h:
    Code:
    #include "student.h"
    I do not see a reason you need to include student.h
    Last edited by The Brain; 06-07-2005 at 03:20 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    noted and fixed. However, some errors still remain.
    have a look :
    www.falconvent.com/error.png

    Quote Originally Posted by The Brain
    Also, in calc.h:
    Code:
    #include "student.h"
    I do not see a reason you need to include student.h
    Look here: (calc.h)
    Code:
    private:
    student stud[students];
    I am trying to use nested classes to learn, but up untill now, its been all trouble.
    Last edited by jeffeklund; 06-07-2005 at 03:26 PM.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I notice a lack using namespace in your .h header files

    [edit]Ok, you used std:: [/edit]
    Last edited by The Brain; 06-07-2005 at 06:15 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    Quote Originally Posted by The Brain
    I notice a lack using namespace in your .h header files
    Why do i need them? Look at the picture i took. Those three problems are the oonly ones left. How can I solve those?

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Your student.h file includes iostream.. and is chocked full of cin and cout operations.. which are derived from the standard namespace.

    Also, I don't think 'student' is working for you as a datatype in your calc class... because I don't think the student class is compiling.. mainly due to this error:

    Code:
    float getGrade(){return grade;};
    bool getGender(){return gender;};
    [edit]I just checked it out and my compiler will also accept a ; after an inlined function, so no error there[/edit]
    Last edited by The Brain; 06-07-2005 at 04:09 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    Yeah, but I've declared all teh cout and cin operations as std::...
    What can I do about the error? I don't even see that that is an error!

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I kind of re-arranged things here.. I put all the classes in a header file by themselves, replaced inline functions.. I commented out or removed any functions that caused a linker error. Everything here compiles. Take a look and compare to what ye' got and slowly work up to what you want from code that compiles.


    student.h
    Code:
    /* Skapar elever, tilldelar dem kön samt betyg */
    
    
    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include <iostream>
    
    using std::cin;
    using std::cout;
    
    class student
    {
    public:
    
         //student();
         //~student();
         void setGender(); // FALSE=boy, TRUE=girl
         void giveGrade();
         float getGrade();//{return grade;}
         bool getGender();//{return gender;}
    
    private:
    
         bool gender;
         double grade;
    };
    
    #endif // STUDENT_H

    student.cpp
    Code:
    #include "student.h"
    using namespace std;
    // Funktioner
    
    void student::setGender()
    {
         bool sex;
         std::cout << "Specify sex of student; 1 (girl) or 0 (boy): ";
         std::cin >> sex;
         gender=(sex=0?0:1);
    }
    
    void student::giveGrade()
    {
         std::cout << "Specify student grade: (1-5) ";
         std::cin >> grade;
    }
    
    float student::getGrade()//{return grade;}
    {
    	return grade;
    }
    
    bool student::getGender()//{return gender;}
    {
    	return gender;
    }

    calc.h
    Code:
    #ifndef CALC_H
    #define CALC_H
      
    #include"student.h"
    #define students 20
    
    
    
    
    class klass : public student 
    {
    public:
    
         klass();
         ~klass();
         float AvGradeGirls();//{return AvGradeG;} medelbetyg för tjejer
         float averagegrade();//{return totGrade;} medelbetyg
         float AvGradeBoys();//{return AvGradeB;}  medelbetyg för killar
         //void addstudent(); //This function was never defined
         int numBo();//{return numB;}  Antal B
         int numGi();//{return numG;}  Antal G
         int totalStudents(){return numG+numB;} // Totala antalet studneter
         void collect();
         void calcaverage();
    
    
    private:
    
         student stud[students];
         int Ggrade;
         int Bgrade; // Tjejsor och killsars sammanlagda betyg (ej  medelvärde)
         int numG;
         int numB;
         double AvGradeB; // Gossars betyg
         double AvGradeG; // Flickors betyg
         double totGrade; // Totala betyget
    };
    
    #endif // CALC_H

    calc.cpp
    Code:
    #include "calc.h"
    #include "student.h"
    using namespace std;
    
    
    klass::klass() : numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0)
    {
    }
    
    klass::~klass()
    {
    }
    
    void klass::collect()
    {	
    
    	for(int i=(numG+numB);i>0;i--)
    	{
    		if(getGender()) // om getGender() är sann = tösabete...
    		{
    			Ggrade=Ggrade+getGrade();
    			numG++;
    		} 
    
    		else // ...annars en hane
    		{
    			Bgrade=Bgrade+getGrade();
    			numB++;  
    		}
    	}
    }
    
    
    void klass::calcaverage()
    {
    	if(numG>0) // Inte dela med noll
    		AvGradeG=Ggrade/numG;
    
    	if(numB>0) // inte dela med noll
    		AvGradeB=Bgrade/numB;
    
    	if((numB+numG)>0) // inte dela med noll
    		totGrade=(Bgrade+Ggrade)/(numB+numG);
    }
    
    float klass::AvGradeGirls()//{return AvGradeG;}// medelbetyg för tjejer
    {
    	return AvGradeG;
    }
    
    float klass::averagegrade()//{return totGrade;} // medelbetyg
    {
    	return totGrade;
    }
    
    
    float klass::AvGradeBoys()//{return AvGradeB;} // medelbetyg för killar
    {
    	return AvGradeB;
    }
    
    int klass::numBo()//{return numB;}  Antal B
    {
    	return numB;
    }
    
    int klass::numGi()//{return numG;}  Antal G
    {
    	return numG;
    }

    main.cpp
    Code:
    #include <iostream>
    #include "calc.h"
    #include "student.h"
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    bool menu();
    
    int main(int argc, char *argv[])
    {
         int SEL;
    
         klass klass1; // Initiera ett objekt, "Klass", av klassen Klass
    
         cout << "Menu selection:\n1.Add student data\n2.Randomize    student data\n3.Exit" << endl;
    
         cin >> SEL;
    
         switch(SEL)
         {
              case 1:  //klass1.addstudent(); //ERROR HERE
              break;
    
              case 2:
              break;
    
              case 3:
              return 0;
    
              default:   cout << "ERROR" << endl;
         }
    
         klass1.collect();
         klass1.calcaverage();
         cout << "de " << klass1.numBo() << " killarnas medel \x84r " <<   klass1.AvGradeBoys() << "\n\n";
         cout << "de " << klass1.numGi() << " tjejernas medel \x84r " << klass1.AvGradeGirls() << "\n\n";
         cout << "de " << (klass1.numGi()+klass1.numBo()) << " studenternas medel \x84r " << klass1.averagegrade() << endl;
    
         cin.ignore(255,'\n');
         cin.get();
         return 0;
    }


    --------------------Configuration: main - Win32 Debug--------------------
    Compiling...
    calc.cpp
    student.cpp
    main.cpp
    Linking...

    main.exe - 0 error(s), 0 warning(s)
    Attached Images Attached Images  
    Last edited by The Brain; 06-07-2005 at 06:47 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You don't have to change things around like The Brain showed you. All you need to do is add the array subscript when you use the stud array. So instead of
    Code:
    stud.getGrade()
    it should be
    Code:
    stud[i].getGrade()
    Or similar.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    Okey, I'm back at it now.
    This is how everything looks at this moment:

    main.cpp
    Code:
    #include <iostream>
    #include "student.h"
    #include "calc.h"
    //#include "calc.cpp"
    using std::cin; 
    using std::cout;
    using std::endl;
    
    bool menu();
    
    int main(int argc, char *argv[])
    {   
        int SEL;
        klass klass1;        // Initiera ett objekt, "Klass", av klassen Klass    
        cout << "Menu selection:\n1.Add student data\n2.Randomize student data\n3.Exit" << endl;
        cin >> SEL;
        switch(SEL)
        {
            case 1: 
                klass1.addstudent();
            break;
            case 2:
                cout << "not implemented yet!";
            break;
            case 3:
                return 0;
            default:
                cout << "ERROR" << endl;
    	}
    	klass1.collect();
    	klass1.calcaverage();
    	cout << "Boys average score:\n----------------------\nNumber of boys:" << klass1.numBo() << "\nAverage score:" << klass1.AvGradeBoys() << endl << endl;
    	cout << "Girls average score:\n----------------------\nNumber of girls:" << klass1.numGi() << "\nAverage score:" << klass1.AvGradeGirls() << endl << endl;
    	cout << "Total average score:\n----------------------\nNumber of students:" << (klass1.numGi()+klass1.numBo()) << "\nAverage score:" << klass1.averagegrade() << endl;
    	
        cin.ignore(255,'\n');
        cin.get();
        return 0;
    }
    calc.cpp
    Code:
    #include "calc.h"
    #include "student.h"
    using namespace std;
    
    
    klass::klass() : numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0)
    {
    }
    
    klass::~klass()
    {
    }
    
    void klass::collect()
    {	
    
    	for(int i=(numG+numB);i>0;i--)
    	{
    		if(getGender())             // om getGender() är sann = tösabete...
    		{
    			Ggrade=Ggrade+getGrade();
    			numG++;
    		} 
    
    		else                        // ...annars en hane
    		{
    			Bgrade=Bgrade+getGrade();
    			numB++;  
    		}
    	}
    }
    
    
    void klass::calcaverage()
    {
    	if(numG>0)                      // Inte dela med noll
    		AvGradeG=Ggrade/numG;
    
    	if(numB>0)                      // inte dela med noll
    		AvGradeB=Bgrade/numB;
    
    	if((numB+numG)>0)               // inte dela med noll
    		totGrade=(Bgrade+Ggrade)/(numB+numG);
    }
    
    float klass::AvGradeGirls()         // medelbetyg för tjejer
    {
    	return AvGradeG;
    }
    
    float klass::averagegrade()         // medelbetyg
    {
    	return totGrade;
    }
    
    int klass::totalStudents()
    {
        return numG+numB;
    }
    
    
    float klass::AvGradeBoys()          // medelbetyg för killar
    {
    	return AvGradeB;
    }
    
    int klass::numBo()                  //  Antal B
    {
    	return numB;
    }
    
    int klass::numGi()                  // Antal G
    {
    	return numG;
    }
    
    void klass::addstudent()
    {
    	numofstud++;
    	stud[numofstud].setGender();
    	stud[numofstud].giveGrade();
    }
    student.cpp
    Code:
    #include "student.h"
    using namespace std;
    // Funktioner
    
    void student::setGender()        // FALSE=boy, TRUE=girl
    {   
        bool done = false;
        while (!done)
        {
            int num;
            std::cout << "Specify sex of student; 1 (girl) or 0 (boy): ";
            std::cin >> num;
            switch( num )
            {
                 case 0:
                     gender = false;
                     done = true;
                     break;
                 case 1:
                      gender = true;
                      done = true;
                      break;
                  default:
                      std::cout << "You must enter 0 or 1: " << endl;
            }
        }
    }
    
    void student::giveGrade()
    {
         std::cout << "Specify student grade: (1-5) ";
         std::cin >> grade;
    }
    
    float student::getGrade()
    {
    	return grade;
    }
    
    bool student::getGender()
    {
    	return gender;
    }
    calc.h
    Code:
    #ifndef CALC_H
    #define CALC_H
      
    #include"student.h"
    #define students 20
    
    
    
    
    class klass : public student 
    {
    public:
    
         klass();
         ~klass();
         float AvGradeGirls();      //{return AvGradeG;} medelbetyg för tjejer
         float averagegrade();      //{return totGrade;} medelbetyg
         float AvGradeBoys();       //{return AvGradeB;}  medelbetyg för killar
         void addstudent(); 
         int numBo();               // Antal B
         int numGi();               // Antal G
         int totalStudents();       // Totala antalet studneter
         void collect();
         void calcaverage();
    
    
    private:
    
         student stud[students];
         int Ggrade;
         int Bgrade;                // Tjejsor och killsars sammanlagda betyg (ej  medelvärde)
         int numofstud;
         int numG;
         int numB;
         double AvGradeB;           // Gossars betyg
         double AvGradeG;           // Flickors betyg
         double totGrade;           // Totala betyget
    };
    
    #endif // CALC_H
    student.h
    Code:
    /* Skapar elever, tilldelar dem kön samt betyg */
    
    
    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include <iostream>
    
    using std::cin;
    using std::cout;
    
    class student
    {
    public:
    
         //student();
         //~student();
         void setGender();      // FALSE=boy, TRUE=girl
         void giveGrade();
         float getGrade();      //{return grade;}
         bool getGender();      //{return gender;}
    
    private:
    
         bool gender;
         double grade;
    };
    
    #endif // STUDENT_H
    My program compiles at this point, but when I try to run it, I get into the menu, start adding data for a new student, and when I've entered the gender, the program crashes! Help, anyone?

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    what do you see happening when you step through you code with the debugger..?
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    This is the state now; I can start it, add students and everything is fine, but when I have added a few students, and go check on the statistics, the stats are all blank!
    Example run:
    Menu selection:
    1.Add student data
    2.Randomize student data
    3.show statistics
    4.Exit
    1
    Specify sex of student; 1 (girl) or 0 (boy): 0
    Specify student grade: (1-5) 5
    Menu selection:
    1.Add student data
    2.Randomize student data
    3.show statistics
    4.Exit
    1
    Specify sex of student; 1 (girl) or 0 (boy): 0
    Specify student grade: (1-5) 1
    Menu selection:
    1.Add student data
    2.Randomize student data
    3.show statistics
    4.Exit
    3
    Boys average score:
    ----------------------
    Number of boys:0
    Average score:0

    Girls average score:
    ----------------------
    Number of girls:0
    Average score:0

    Total average score:
    ----------------------
    Number of students:0
    Average score:0
    My current code is:
    main.cpp
    Code:
    #include <iostream>
    #include "student.h"
    #include "calc.h"
    using std::cin; 
    using std::cout;
    using std::endl;
    
    int main(int argc, char *argv[])
    {   
        int SEL, loop=1, x;
        klass klass1;        // Initiera ett objekt, "Klass", av klassen Klass    
        while(loop==1)
        {
            cout << "Menu selection:\n1.Add student data\n2.Randomize student data\n3.show statistics\n4.Exit" << endl;
            cin >> SEL;
            switch(SEL)
            {
                case 1: 
                    klass1.addstudent();
                break;
                case 2:
                    cout << "not implemented yet!";
                break;
                case 3:
                    klass1.collect();
                    klass1.calcaverage();
                    cout << "Boys average score:\n----------------------\nNumber of boys:" << klass1.numBo() << "\nAverage score:" << klass1.AvGradeBoys() << endl << endl;
                    cout << "Girls average score:\n----------------------\nNumber of girls:" << klass1.numGi() << "\nAverage score:" << klass1.AvGradeGirls() << endl << endl;
                    cout << "Total average score:\n----------------------\nNumber of students:" << (klass1.numGi()+klass1.numBo()) << "\nAverage score:" << klass1.averagegrade() << endl;
                    cin.ignore(255,'\n');
                    cin.get();
                case 4:
                    return 0;
                default:
                    cout << "ERROR" << endl;
            }
        }
    	/*klass1.collect();
    	klass1.calcaverage();
    	cout << "Boys average score:\n----------------------\nNumber of boys:" << klass1.numBo() << "\nAverage score:" << klass1.AvGradeBoys() << endl << endl;
    	cout << "Girls average score:\n----------------------\nNumber of girls:" << klass1.numGi() << "\nAverage score:" << klass1.AvGradeGirls() << endl << endl;
    	cout << "Total average score:\n----------------------\nNumber of students:" << (klass1.numGi()+klass1.numBo()) << "\nAverage score:" << klass1.averagegrade() << endl;
    	*/
        cin.ignore(255,'\n');
        cin.get();
        return 0;
    }
    calc.cpp
    Code:
    #include "calc.h"
    #include "student.h"
    using namespace std;
    
    
    klass::klass() : numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0), numofstud(0), Ggrade(0), Bgrade(0)
    {
    }
    
    klass::~klass()
    {
    }
    
    void klass::collect()
    {	
    
    	for(int i=(numG+numB);i>0;i--)
    	{
    		if(getGender())             // om getGender() är sann = tösabete...
    		{
    			Ggrade=Ggrade+getGrade();
    			numG++;
    		} 
    
    		else                        // ...annars en hane
    		{
    			Bgrade=Bgrade+getGrade();
    			numB++;  
    		}
    	}
    }
    
    
    void klass::calcaverage()
    {
    	if(numG>0)                      // Inte dela med noll
    		AvGradeG=Ggrade/numG;
    
    	if(numB>0)                      // inte dela med noll
    		AvGradeB=Bgrade/numB;
    
    	if((numB+numG)>0)               // inte dela med noll
    		totGrade=(Bgrade+Ggrade)/(numB+numG);
    }
    
    float klass::AvGradeGirls()         // medelbetyg för tjejer
    {
    	return AvGradeG;
    }
    
    float klass::averagegrade()         // medelbetyg
    {
    	return totGrade;
    }
    
    int klass::totalStudents()
    {
        return numG+numB;
    }
    
    
    float klass::AvGradeBoys()          // medelbetyg för killar
    {
    	return AvGradeB;
    }
    
    int klass::numBo()                  //  Antal B
    {
    	return numB;
    }
    
    int klass::numGi()                  // Antal G
    {
    	return numG;
    }
    
    void klass::addstudent()
    {
    	stud[numofstud].setGender();
    	stud[numofstud].giveGrade();
        numofstud++;
    }
    student.cpp
    Code:
    #include "student.h"
    using namespace std;
    // Funktioner
    
    void student::setGender()        // FALSE=boy, TRUE=girl
    {   
        bool done = false;
        while (!done)
        {
            int num;
            std::cout << "Specify sex of student; 1 (girl) or 0 (boy): ";
            std::cin >> num;
            switch( num )
            {
                 case 0:
                     gender = false;
                     done = true;
                     break;
                 case 1:
                      gender = true;
                      done = true;
                      break;
                  default:
                      std::cout << "You must enter 0 or 1: " << endl;
            }
        }
    }
    
    void student::giveGrade()
    {
         std::cout << "Specify student grade: (1-5) ";
         std::cin >> grade;
    }
    
    float student::getGrade()
    {
    	return grade;
    }
    
    bool student::getGender()
    {
    	return gender;
    }
    calc.h
    Code:
    #ifndef CALC_H
    #define CALC_H
      
    #include"student.h"
    #define students 20
    
    
    
    
    class klass : public student 
    {
    public:
    
         klass();
         virtual ~klass();
         float AvGradeGirls();      //{return AvGradeG;} medelbetyg för tjejer
         float averagegrade();      //{return totGrade;} medelbetyg
         float AvGradeBoys();       //{return AvGradeB;}  medelbetyg för killar
         void addstudent(); 
         int numBo();               // Antal B
         int numGi();               // Antal G
         int totalStudents();       // Totala antalet studneter
         void collect();
         void calcaverage();
    
    
    private:
    
         student stud[students];
         int Ggrade;
         int Bgrade;                // Tjejsor och killsars sammanlagda betyg (ej  medelvärde)
         int numofstud;
         int numG;
         int numB;
         double AvGradeB;           // Gossars betyg
         double AvGradeG;           // Flickors betyg
         double totGrade;           // Totala betyget
    };
    
    #endif // CALC_H
    student.h
    Code:
    /* Skapar elever, tilldelar dem kön samt betyg */
        
        
    #ifndef STUDENT_H
    #define STUDENT_H
        
    #include <iostream>
        
    using std::cin;
    using std::cout;
        
    class student
    {
    public:
        
        //student();
        //~student();
        void setGender();      // FALSE=boy, TRUE=girl
        void giveGrade();
        float getGrade();      //{return grade;}
        bool getGender();      //{return gender;}
        
    private:
     
        bool gender;
        double grade;
    };
        
    #endif // STUDENT_H

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  3. MinGW Linking GUI Program
    By Tonto in forum Tech Board
    Replies: 19
    Last Post: 08-23-2006, 03:28 PM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21