Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 17-04-2009
Member
 
Join Date: Oct 2008
Posts: 37
C++ Derived classes

Sit and tinker with that derived classes and I have encountered some problems..

Here are the classes:
Code:
Class Person ( 

   public: 

     Person (const string & n): name (n) (); 

     const string & units () const; 

     void andra_namn (const string & n); 

     void skriv_info () const; 

   private: 

     string name; 

   ); 



  class Course; 



  class Student: public Person ( 

  public: 

    Student (const string & n): Person (n) (); 

    void borja_kurs (Course & k); 

    void sluta_kurs (Course & k); 

    void skriv_info () const; 

  private: 

    vector <Kurs *> courses; 

  ); 



  class Course ( 

  public: 

    Course (const string & name): knamn (name) (); 

    const string & course name () (return knamn;) 

    void ny_student (Student & s); 

    void student_bort (Student & s); 

    void exit (); 

    void skriv_info () const; 

  public: 

    string knamn; 

    vector <Student *> stud; 
  );
And here are some features:
Code:
  const string & Privacy:: units () const 

   (Return name;)  

   void Person:: skriv_info () const 

   (Court << "Name:" <<name <<Endla;) 

  void Student:: borja_kurs (Course & k) 

  ( 

    for (int i = 0; i <kurser.size (); i + +) 

      if (rates [i] == & k) 

        return; 

    kurser.push_back (& k); 

    k.ny_student (* this); 

  )
But how do you do then to call on these features inside the main?
Help appreciated!
Reply With Quote
  #2  
Old 17-04-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: C++ Derived classes

Try this code,
Code:
 int main () 
  ( 
     Personal sven ( "Sven"); 
     sven.skriv_info (); 

     return 0; 
  )
Reply With Quote
  #3  
Old 17-04-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: C++ Derived classes

If you plan to use the polymorphism it gets a little trickier, but to say I can (or some other safe) try to explain.
Reply With Quote
  #4  
Old 17-04-2009
Member
 
Join Date: Oct 2008
Posts: 37
Re: C++ Derived classes

Code:
string first name; 
      string Lastname; 
      Court << "Your first name:"; 
      cin>> first name; 
      Court << "Your Surname:"; 
      cin>> last name; 
      Person's name (first name + "" + last name); 
      namn.skriv_info ();
Is this the easiest way for users to insert something into a class
or is there something better?

Thank you for taking the time.
Reply With Quote
  #5  
Old 17-04-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: C++ Derived classes

Better / easier is a bit of a matter of taste and situation dependent. Want to change the name after you created the item? Then you need a set function. Otherwise, if it is not needed, or if you absolutely do not want it to change in circumstances, it is best to handle it in.

Code:
  string name; 
	  std:: Court << "Name:"; 
	  std:: cin>> name; 

	  / / Alt 1: 
	  Person obj = Person (name); 

	  / / Alt 2: 
	  Person obj; 
	  obj.setName (name);
Pick your poison.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "C++ Derived classes"
Thread Thread Starter Forum Replies Last Post
Which class should be instantiated in from base or derived class ? VAIJAYI Software Development 4 08-01-2011 07:52 AM
What are an Autoloading Classes in PHP? Flaco Software Development 5 06-03-2010 06:49 AM
How to initialize a derived class with an instance of the base class in c#? KAIRU26 Software Development 5 12-02-2010 06:23 PM
C # using classes in C + + klite Software Development 3 01-10-2009 10:15 AM
Classes in VB.NET Samir_1 Software Development 2 10-04-2009 12:07 AM


All times are GMT +5.5. The time now is 12:14 AM.