Thread: Class problem

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Class problem

    Here I have made a piece of code that SHOULD display "firstname lastname"
    The problem I have is that I does compile but the program itself doesnt work.
    I dont see the problem but maybe you guys do.

    Code:
    #include <iostream.h>
    #include <string.h>
    class Student
    {
        friend void display(Student* s);
    public:
    	
    	int marks[5];
        
    	
    	Student(char*fname,char*lname)
    	{
    		strcpy(fname,firstname);
    		strcpy(lname,lastname);
    	}
    	~Student()
    	{
    	}
    protected:
    	int average[5];
    	char firstname[10];
    	char lastname[10];
    };
    void display(Student* s);
    int main()
    {
    	Student I("firstname","lastname");
    	display(&I);
    	
    	return 0;
    }
    
    void display(Student* s)
    {
    	cout<<s->firstname;
    	cout<<" ";
    	cout<<s->lastname;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    Your code should read as follows:

    Code:
    	Student(char*fname,char*lname)
    	{
    		strcpy(firstname, fname);
    		strcpy(lastname, lname);
    	}
    zMan

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    In general for a class like this do something like this...
    Code:
    class student
    {
        public:
             Student(char*fname,char*lname)
    	{
    		strcpy(firstname, fname);
    		strcpy(lastname, lname);
    	}
    
              ~Student()
    	{
    	}
            
               std::ostream& Display( std::ostream& strm)
                {
                    strm << fname << " "<< lname ;
                    return strm;
                 }
       private: 
             // private members
    };
    
    std::ostream& operator << ( std::ostream& strm, const Student& stud )
    { 
         return stud.Display(strm);
    }
    Now no friendship need be granted and you have the option of making Display virtual should you want to derive from this class. In general if you can use a member func and not have to grant friendship then its the best way of doing it.
    Now you can do...

    Student student( "fred","bloggs");
    cout << student<<endl;
    Last edited by Stoned_Coder; 03-11-2003 at 01:30 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM