OO Programming/Classes

This is a discussion on OO Programming/Classes within the C++ Programming forums, part of the General Programming Boards category; Ive just started doing OO programming as opposed to function orientated and its caused a whole load of new problems ...

  1. #1
    rpincher
    Guest

    OO Programming/Classes

    Ive just started doing OO programming as opposed to function orientated and its caused a whole load of new problems for me.

    I was given a piece of code and asked to add functionality for a user to add his/her details. I cannot get the the class to accept my information and use the getters that were specified for example the one for the date of birth.

    Help appreciated


    Code:
    ///////////////////////////////////////////////////////////////////////////////
    //	CP2026 - Workshop 1
    //	Filename:	person.cpp
    //	Author:		Ryan Pincher
    
    #include <iostream.h>
    #include <conio.h>
    #include <cstring.h>
    #include <date.h>		// ensure that the 'include' path also points to classlib
    #include <stdio.h>
    
    ///////////////////////////////////////////////////////////////////////////////
    //	Class Declarations
    
    class Person
    {
    	private:
    	// Attributes
    	string  theName;
    	string  theAddress;
    	TDate   theDateOfBirth;
    	char    theGender;
    	string  theTelNo;
    
    	public:
    	// Methods
    	Person();
    	Person(string N, string A, TDate D, char G, string T);
    	void setAddress(string A);
    	void setTelNo(string T);
    	void Print(void);
    	TDate getDateOfBirth(void);
    }; // end Person class declaration
    
    ///////////////////////////////////////////////////////////////////////////////
    //	Class Definitions
    
    //	Default Constructor
    // As this will create an undefined 'person' we should make sure that
    // all attributes are either cleared or set to invalid values
    Person::Person()
    {
    	theName = "";
    	theAddress = "";
    	theDateOfBirth = TDate(0,1,0);	// ensures date is invalid
    	theGender = '?';						// ensures gender is invalid
    	theTelNo = "";
    }; // end default constructor
    
    //	Paramaterised Constructor
    Person::Person(string N, string A, TDate D, char G, string T)
    {
    	theName = N;
    	theAddress = A;
    	theDateOfBirth = D;
    	theGender = G;
    	theTelNo = T;
    	theName.to_upper();
    }; // end constructor
    
    // Setter for address attribute
    void Person::setAddress(string A)
    {
    	theAddress = A;
    }; // end setAddress()
    
    // Setter for telephone number attribute
    void Person::setTelNo(string T)
    {
    	theTelNo = T;
    }; // end setTelNo()
    
    // Print method that produces formatted output of all attributes
    void Person::Print(void)
    {
    	TDate::SetPrintOption(TDate::European);
    	cout << "\nName     : " << theName << endl;
    	cout << "Address  : " << theAddress << endl;
    	cout << "Telephone: " << theTelNo << endl;
    	cout << "Date of Birth : " << theDateOfBirth;
    	cout << "\tGender: " << theGender << endl;
    }; // end Print()
    
    // Getter for date of birth
    TDate Person::getDateOfBirth(void)
    {
    	return theDateOfBirth;
    };	// end getDateOfBirth()
    
    ///////////////////////////////////////////////////////////////////////////////
    //	Main Program
    
    
    void main(void)
    {
    string sName;
    
    	// create a 'default' person object
       Person P1;
       Person P2;
       //Get user info
       cout<<"Please Enter first name: ";
       cin>>sName;
       endl;
       cout<<"DOB: ";
       TDate Person::getDateOfBirth(void);
    
    
    
    
    	// create a specific person object
    	//Person P2("Fred", "The Larches", TDate(12, 4, 1985), 'M', "01902 321000");
    
    	// Now print them both using the Print method
    	P1.Print();
    	P2.Print();
    
    	getch();
    
    	// To Do:
    	// add to the above program to allow the user to enter the details of
    	// a new person then create the person object and print it out.
    	// Add code to demonstrate how you would use the other methods available
    	// in the Person class.
    
    }	// end main
    Code Tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    I don't quite understand exactly what you are trying to do ... or what you started with and what you added but let me tell you what I see. You have no method to set the person objects name. You have functions to set the address and the telephone number but not for the name. So if you create a person using the default constructor, like you do in Main(), you have no way to change the name to something other than "". You need a Person::setName() function. You should have a 'get' and a 'set' function for each of the private data members that you want access to.

    So, looking at main(), you read in the name but you can't do anything with it because you don't have a setName() function. You also need a setDOB() function. I'm guessing that this line "TDate Person::getDateOfBirth(void);" is intended to call the getDateOfBirth() function for one of the person objects. You need to call it specific to the object like

    TDate pDOB;
    pDOB = P1.getDateOfBirth();

    But you can only do this after you set the date for that person. I hope this is of some help. If there is something else I can help clear up let me know.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Is there a TDate in C++? Sounds like Pascal to me, but maybe there is one.
    Truth is a malleable commodity - Dick Cheney

  5. #5
    rpincher
    Guest
    mepaco: i realise there is no way to add the name and no functions, this is because im not entirely sure how id' go about this, hoiw would i use the getter for the DOB or use the Person() function to fill in the details. I have looked for most of this morning and afternoon for things regarding setting user entered object data on the internet and have found nothing at all.

  6. #6
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Originally posted by rpincher
    mepaco: i realise there is no way to add the name and no functions, this is because im not entirely sure how id' go about this, hoiw would i use the getter for the DOB or use the Person() function to fill in the details. I have looked for most of this morning and afternoon for things regarding setting user entered object data on the internet and have found nothing at all.
    Well, you have example functions to follow already. You have a setAddress and setTelNo function to go by. Simply make the same thing for name like this:
    Code:
    // Setter for name attribute
    void Person::setName(string N)
    {
    	theName = N;
    }; // end setName()
    Then just add the declaration for the function to the class definition like this:
    Code:
    ///////////////////////////////////////////////////////////////////////////////
    //	Class Declarations
    
    class Person
    {
    	private:
    	// Attributes
    	string  theName;
    	string  theAddress;
    	TDate   theDateOfBirth;
    	char    theGender;
    	string  theTelNo;
    
    	public:
    	// Methods
    	Person();
    	Person(string N, string A, TDate D, char G, string T);
    	void setAddress(string A);
    	void setTelNo(string T);
            void setName(string N);
    	void Print(void);
    	TDate getDateOfBirth(void);
    }; // end Person class declaration
    Do the same thing for any of the other members you need to set. For the 'get' functions just base them off of the 'getDateOfBirth()'. Add the functions and the function declarations in the class. Does this help? If you need a little more detail let me know.

  7. #7
    rpincher
    Guest
    Cheers for that, im sorta struggling on calling them up, as i tried to do it for the date setter that was already there, without too much luck.

  8. #8
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    When using a class, you have defined your own data type. When you want to use a function from the class you have to use it with the object of the class you want the function to 'operate' on. I'm going to assume you have the functions made that set the member data and I will take your code and show you how to use them. You call functions or access data members of a class object with the '.' operator like this:
    Code:
    void main(void)
    {
       string sName;
    
       // create a 'default' person object which will call the default constructor
       Person P1;
       //Get user info
       cout<<"Please Enter first name: ";
       cin>>sName;
       //After the string is read in you have to set it to the data member
       P1.setName(sName);
       //Before you can output theDateOfBirth you have to set it first so the next line is bad
       //If you had set it, though, you could output it like this
       cout << "DOB: " << P1.getDateOfBirth() << endl;
    
    
    
    
       // create a specific person object
       Person P2("Fred", "The Larches", TDate(12, 4, 1985), 'M', "01902 321000");
    
    
    
       // Now print them both using the Print method
       P1.Print();
       P2.Print();
       getch();
    
       // To Do:
       // add to the above program to allow the user to enter the details of
       // a new person then create the person object and print it out.
       // Add code to demonstrate how you would use the other methods available
       // in the Person class.
    
    }	// end main
    Do you see how you access functions and data for classes? Anything that is declared as public can be accessed with the '.' operator. If any of the data members, like 'theGender', were declared as public then you could access them like that too (i.e. P1.theGender = 'M'). The private data and functions cannot be accessed like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global functions and OO
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 12:00 PM
  2. array bounds overflow
    By BendingUnit in forum C Programming
    Replies: 3
    Last Post: 06-18-2006, 11:45 PM
  3. "Modern C++ and Basics of OO Programming" (e-learning course)
    By Erhard Henkes in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2004, 04:01 PM
  4. OO in C
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-05-2002, 11:02 PM
  5. OO programming
    By iwod in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2001, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21