Class display
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Class display

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Posts
    4

    Class display

    Hey guys, i'm having an issue with the my class, when i go to cout in the main, all i receive is jibberish, any help

    Here is my code: @ = file names

    @person.h



    Code:
    #ifndef	PERSON_H
    #define	PERSON_H	
    #include <string>
    class person
    {
    private:
    
    	char first[20];
    	char last[20];
    	char middle;
    	int	 year;
    	int	 month;
    	int	 day;
    
    public: 
    
    	void setFname(char *);
    	void setLname(char *);
    	void setmiddle(char);
    	void setBirthyear(int);
    	void setBirthmonth(int);
    	void setBirthday(int);
    	const char *getFname() const;
    	const char *getLname() const;
    	char getmiddle() const;
    	int	 getBirthyear() const;
    	int  getBirthmonth() const;
    	int	 getBirthday() const;
    
    };
    
    #endif
    
    
    @persontypeimpl.cpp
    
    
    
    
    #include "person.h"
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    
    void person::setFname(char *first)
    
    {
    	if (first == NULL)
    
    	{
    		cout << " Invalid First Name";
    			exit(EXIT_FAILURE);
    	}
    }
    
    
    void person::setLname(char *last)
    
    {
    	if (last == NULL)
    
    	{
    		cout << " Invalid Last Name";
    			exit(EXIT_FAILURE);
    	}
    }
    
    
    void person::setmiddle(char)
    
    {
    	if (middle == NULL)
    
    	{
    		cout << " Invalid middle initial";
    			exit(EXIT_FAILURE);
    	}
    }
    
    
    void person::setBirthyear(int y)
    
    {
    	if (year >= 0)
    		year = y;
    	else
    	{
    		cout << " Invalid Birth Year";
    			exit(EXIT_FAILURE);
    	}
    }
    
    
    void person::setBirthmonth(int m)
    
    {
    	if (month >= 0)
    		month = m;
    	else
    	{
    		cout << " Invalid Birth month";
    			exit(EXIT_FAILURE);
    	}
    }
    
    
    void person::setBirthday(int d)
    
    {
    	if (day >= 0)
    		day = d;
    	else
    	{
    		cout << " Invalid Birth Day";
    			exit(EXIT_FAILURE);
    	}
    }
    
    
    
    const char *person:: getFname() const
    
    {
    	return first;
    }
    
    
    const char *person:: getLname() const
    
    {
    	return last;
    }
    
    
    char person:: getmiddle() const
    
    {
    	return middle;
    } 
    
    
    int person:: getBirthyear() const
    
    {
    	return year;
    }
    
    
    
    int person:: getBirthmonth() const
    
    {
    	return month;
    }
    
    
    int person:: getBirthday() const
    
    {
    	return day;
    }
    
    
    
    
    
    
    @program.cpp
    
    
    
    #include <iostream>
    #include "person.h"  
    #include <string>
    using namespace std;
    
    
    int main()
    {
    
    	person *name;
    	name = new person;
    
    
    	name->setFname("Shimar");
    	cout << name->getFname();
    
    	delete name;
    	
    
    
    	system("pause");
    	return 0;
    }
    Last edited by Marc G; October 27th, 2010 at 05:18 AM. Reason: added code tags

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Class display

    You aren't actually doing anything useful in setFname().

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Class display

    Code:
    exit(EXIT_FAILURE);
    DON'T use exit(). It doesn't close your program gracefully, but it nukes it without freeing memory, calling destructors, etc...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center