Results 1 to 5 of 5

Thread: C++ Derived classes

  1. #1
    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!

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: C++ Derived classes

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

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    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.

  4. #4
    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.

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    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.

Similar Threads

  1. Replies: 4
    Last Post: 08-01-2011, 07:52 AM
  2. What are an Autoloading Classes in PHP?
    By Flaco in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 06:49 AM
  3. Replies: 5
    Last Post: 12-02-2010, 06:23 PM
  4. C # using classes in C + +
    By klite in forum Software Development
    Replies: 3
    Last Post: 01-10-2009, 10:15 AM
  5. Classes in VB.NET
    By Samir_1 in forum Software Development
    Replies: 2
    Last Post: 10-04-2009, 12:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,442,148.42831 seconds with 17 queries