Thread: structs and classes problem!!!!

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    structs and classes problem!!!!

    hi, i am trying to write a program, using classes and structs and inheritence…

    the program kind of represents a university system where tutors teach only specific modules and students are only enrolled on specific modules.

    To store information about students and their modules.

    the user is prompted to enter the number of modules they undertake and then according to the number they enter they get the option to store information about these modules.

    for example if user enters 1 module only 1 struct is called to store the information

    but if user enters 3 modules, the program should allow three struct objects to be entered..

    i can’t get three structs to come if the user enters three….

    and is using classes and struct the best option to solve this problem?

    any help please….

  2. #2
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Do you have any code? You should post that so we have something to go off of. But in c++ you might be interested in dynamic memory allocation.

    Cprogramming.com Tutorial: Linked Lists

    Cprogramming.com Tutorial: Inheritance Overview

    Cprogramming.com Tutorial: Inheritance Syntax
    -- Will you show me how to c++?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Code:
    //this is header file  StudentModule.h
    struct StudentModDetail
    {
    	string moduleCode;
    	string moduleName;
    	unsigned creditPoints;
    };
    
    class StudentModules
    {
    public:
    	StudentModules (unsigned noOfModules,
    		        StudentModDetail mod_detail);
    
    	virtual void print() const;
    
    private:
    	unsigned _noOfModules;
    	StudentModDetail _mod_detail;
    };
    
    //this is .cpp file StudentModule.cpp
    #include "stdafx.h"
    #include "StudentModule.h"
    #include <iostream>
    
    using namespace std;
    
    StudentModules::StudentModules ( unsigned noOfModules,
       	                         StudentModDetail mod_detail)
    	                                     
                                    : _noOfModules (noOfModules)
    		                , _mod_details (mod_details)
        
    {
    
    }
    
    void StudentModules::print() const
    {
    	cout << "No of Modules: " << _noOfModules << endl;
    	cout << "Module Code: " << _mod_details.moduleCode << endl;
    	cout << "Module Name: " << _mod_details.moduleName << endl;
    	cout << "Credit Points: " << _mod_details.creditPoints << endl;
    
    }
    
    
    //this is main file Student.cpp
    #include "stdafx.h"
    #include "StudentModule.h"
    #include <iostream>
    using namespace std;
    int main()
    {
    	
    	StudentModDetail sModD = {"Database Engineering", "ISYDS2001", 20};
    
    	StudentModules sM1 (2,sModD);
    
    	sM1.print();
    
    	system("pause");
    	return(0);
    }
    thanks for getting back to me...

    in the example above user wants to enter 2 modules i.e. 2 structs but only one is implemented...

    so i dont know how to get more than one structs when user enters multiple values...

    thanks in advance!!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see any user input at all, so I am unsure what you message means...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Code:
    StudentModDetail sModD = {"Database Engineering", "ISYDS2001", 20};
    
    	StudentModules sM1 (2,sModD);
    the above corresponds as the user input...

    this will need further development into MFC or somesort...

    at the moment the data is stored in the class using the above code...


    StudentModules sM1 sets the values .... where 2 represents the no of modules.. and the sModD represents the struct data regarding the module...


    hope that helps!!
    Last edited by faisal791; 02-16-2010 at 04:50 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am still unsure of what you mean, but what is stopping you from doing
    StudentModules sM1 (2,sModD,sMod2,...);
    ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    StudentModules sM1 (2,sModD,sMod2,...);

    thats what i would like to do

    but i get the error message

    'StudentModules::StudentModules' : no overloaded function takes 3 arguments..

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    i would like the constructor arguments to adjust with the no of Modules enters...

    suppose if no of modules = 3 then i should be able to enter 3 sets of struct objects...

    i dont know what to do so the constructor arguments adjust automatically...


    can i use dynamic constructor arguments? is that even possible?!!
    Last edited by faisal791; 02-16-2010 at 05:12 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The easiest way is to make it take a vector.
    Ie:
    StudentModules::StudentModules(const std::vector<StudentModDetail>& students)

    Then just create a vector with the objects and pass it along.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Thanks for your speedy replys.. i will try that in the morning... really tired now and my eyes are burning...

    i shall get back you to tomorrow... Thanks once again..!!

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    hi, i am fairly new to vectors and having trouble implementing this...

    would it be possible for you to show me how to implement the whole thing?!!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    StudentModules::StudentModules(const std::vector<StudentModDetail>& students)
    {
    //...
    }
    
    // ...in main somewhere...
    std::vector<StudentModDetail> students;
    students.push_back( StudentModDetail("Database Engineering", "ISYDS2001", 20) );
    // ...more push backs...
    StudentModules modules(students);
    The example assumes that StudentModDetail has a constructor that takes all necessary parameters to construct it and that StudentModules is a class which has a constructor as shown in the first lines.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    sorry i dont get it.. im using a struct to hold all the necessary information about the module. i cannot pass three arguments to

    Code:
    students.push_back( StudentModDetail("Database Engineering", "ISYDS2001", 20) );
    it doesn't work.. and i need to store how many the modules a student is doing and their details for example..

    if the student does 2 modules

    (2,struct object, struct object);

    where 2 represents 2 modules for student

    struct objects represent the module details

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    class StudentModuleInfo
    {
        StudentModuleInfo(/* parameters here */);
        // ...
    };
    
    class StudentModule
    {
    public:
        StudentModule(const StudentModuleInfo& info);
    protected:
        StudentModuleInfo m_info;
    };
    
    class Student
    {
    public:
        Student(const std::vector<StudentModule>& modules)
    };
    
    int main()
    {
        std::vector<Student> students;
        std::vector<StudentModule> modules;
        modules.push_back( StudentModule(StudentModuleInfo( /* parameters here */) );
        // Add more modules
        students.push_back( Student(modules) );
    
        // Repeat above for more students
    }
    In this example, there are n students, and each students can have m modules each with some information describing the module.
    All you need is to nest some vectors. A vector can store an infinite amount of objects, and keeps track of its own size, so you can see at any time how many students or modules there are, for example.
    Just retrofit it to your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Thanks for that.. so we're not using struct anymore!!

    it works thou so thats good...

    just one more thing.. how do i print it to the screen.. do i need a print function in the StudentModule class or Student class??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  4. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 03:21 PM
  5. Classes or Structs faster for Lists
    By White Rider in forum C++ Programming
    Replies: 24
    Last Post: 04-05-2002, 03:57 PM