my first oop based program

This is a discussion on my first oop based program within the C++ Programming forums, part of the General Programming Boards category; i'd like to know how to make the same pointer points to differents types of vars, if you read the ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    my first oop based program

    i'd like to know how to make the same pointer points to differents types of vars,
    if you read the code, you'll see the comment I NEED A SOLUTION FOR THIS, and thats where im stuck...

    Code:
    #include <iostream>
    using namespace std;
    
    class person
    {
    
        public:
    
        person();
    
        string name;
        int age;
        bool sex; // true means male
        string country;
    
        string sexString()
        {
        string returnString;
        returnString = (this->sex == true) ? "guy" : "woman" ;
        return returnString;
        }
    
    };
    
    person::person()
    {
        this->name = "Unnamed Person";
        this->age = 50;
        this->sex = true;
        this->country = "United States";
    }
    
    void introduce(person person)
    {
        cout<<"Hi, my name is "<<person.name<<". I'm a "<<person.age<<" years old "<<person.sexString()<<", and I live in "<<person.country<<"."<<endl;
    }
    
    void callMenu(person& person)
    {
    
        int option;
        string optionIdentifier;
        string input;
        string * pointer;
    
        cout<<"\n WHAT WOULD LIKE TO CHANGE?\n";
        cout<<"1. NAME\n";
        cout<<"2. AGE\n";
        cout<<"3. SEX\n";
        cout<<"4. COUNTRY\n";
        cout<<"5. NOTHING\n";
    
        cout<<"\nCOMMAND: ";
        cin>>option;
    
        switch(option)
        {
            case 1:
            optionIdentifier = "NAME";
            pointer = &person.name;
            break;
    
            case 2:
            optionIdentifier = "AGE";
            //pointer = &person.age;   I NEED A SOLUTION FOR THIS
            break;
    
            case 3:
            optionIdentifier = "SEX";
            //pointer = &person.sex; I NEED A SOLUTION FOR THIS
            break;
    
            case 4:
            optionIdentifier = "COUNTRY";
            pointer = &person.country;
            break;
        }
    
        cout<<"TYPE YOUR NEW "<<optionIdentifier<<": ";
        cin>>input;
    
        *pointer = input;
    
        introduce(person);
    
    }
    
    int main()
    {
    
    person someone;
    
    introduce(someone);
    
    callMenu(someone);
    
    cin.get();
    
    }
    btw, advices for making the code cleaner are welcome

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Just write directly to the variable you are attempting to change. For example:

    Code:
    case 2:
    optionIdentifier = "AGE";
    cin >> person.age;
    break;
    Also, in general it's not a good idea to make a classes member variables have public access. Read up on "encapsulation" for the reasons why.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    I think the program is neat but isn't really OOP.

    Using your current Person class I could make another program that introduces a person like this:

    Code:
    person myperson;
    int option;
    
    cout << "1 - Change name\n";
    cout << "2 - Change age\n"
    cout << "3 - Change gender\n";
    cout << "4 - Change country\n"
    cout << "5 - Change nothing\n";
    cout << "Choose an option - ";
    
    cin >> option;
    
    switch (option) {
    case 1: {
       cout << "Enter a new name: ";
       cin >> myperson.name;
    }
    break;
    case 2: {
       cout << "Enter a new age: ";
       cin >> person.age;
    }
    break;
    case 3: {
       string gender;
       cout << "Enter a new gender ("guy" or "woman");
       cin >> gender;
       myperson.sex = gender == "guy";
    }
    break;
    case 4: {
       cout << "Enter a new country: ";
       cin >> myperson.country;
    }
    break;
    case 5:
    default:
    break;
    }
    
    introduce (myperson); 
    cin.get (); // for a pause
    You'll get to change one thing about him before you meet him, which is exactly how your program seems to work now.

    There's nothing particularly object oriented about this, anyway, but it is rather strange that person is a class that exposes its data to the rest of the program. The code sample doesn't need to be found in a main() function. I could change the details of a person anywhere, and in any way I wanted.

    My advice for the program is to throw it away. My advise for you is to learn more about OOP. There are no shortage of tutorials (or entire books), just STFW.
    Last edited by whiteflags; 08-05-2010 at 03:54 PM.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    I've upgraded my code, i'll read about encapsulation later, thanks.

    I still haven't managed to make the pointer work on multiple data types though...

    Code:
    #include <iostream>
    using namespace std;
    
    class person
    {
    
        public:
    
        person();
    
        string name;
        int age;
        bool sex; // true means male
        string country;
    
        string sexString()
        {
        string returnString;
        returnString = (this->sex == true) ? "guy" : "woman" ;
        return returnString;
        }
    
    };
    
    person::person()
    {
        this->name = "Unnamed Person";
        this->age = 50;
        this->sex = true;
        this->country = "United States";
    }
    
    void introduce(person person)
    {
        cout<<"\nHi, my name is "<<person.name<<". I'm a "<<person.age<<" years old "<<person.sexString()<<", and I live in "<<person.country<<"."<<endl;
        cin.get();
    
    }
    
    void callMenu(person& person)
    {
    
        int option;
        string optionIdentifier;
        string input;
        string * pointer;
    
        int numberOfChanges = 0;
    
        do{
        cout<<"\n WHAT WOULD YOU LIKE TO CHANGE?\n";
        cout<<"1. NAME\n";
        cout<<"2. AGE\n";
        cout<<"3. SEX\n";
        cout<<"4. COUNTRY\n";
        cout<<"5. NOTHING\n";
    
        cout<<"\nCOMMAND: ";
        cin>>option;
    
        switch(option)
        {
            case 1:
            optionIdentifier = "NAME";
            pointer = &person.name;
            break;
    
            case 2:
            optionIdentifier = "AGE";
            //pointer = &person.age;
            break;
    
            case 3:
            optionIdentifier = "SEX";
            //pointer = &person.sex;
            break;
    
            case 4:
            optionIdentifier = "COUNTRY";
            pointer = &person.country;
            break;
        }
    
        if(option != 5)
        {
        cout<<"TYPE YOUR NEW "<<optionIdentifier<<": ";
        cin>>input;
    
        *pointer = input;
    
        numberOfChanges++;
    
        }
    
        }while(option != 5);
    
    
        introduce(person);
    
    }
    
    int main()
    {
    
    person someone;
    
    introduce(someone);
    
    callMenu(someone);
    
    cin.get();
    
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Do what I suggested in my post...
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    You can't do it your way since the data aren't all the same type. You could do it your way with a more advanced technique, but then, what is the point?
    bithub's example is easier, so I'd suggest you use that instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Other suggestions:


    #1.
    Code:
    #include <iostream>
    using namespace std;
    
    class person
    {
        ...
    
        string name;
    
        ...
    
        string country;
    
        ...
    Make sure you've also got #include <string> in addition to the <iostream> header. Your implementation may pull in the necessary header for the string object to work but you really do not want to rely on this. Since you're using string containers, include the string header.



    #2.
    Code:
    class person
    {
    
        public:
    
        person();
    
        ...
    
    };
    
    person::person()
    {
        this->name = "Unnamed Person";
        this->age = 50;
        this->sex = true;
        this->country = "United States";
    }
    For such a constructor, the preferred method of initialization is to use something a bit different, for example:
    Code:
    class person
    {
    
        public:
    
        person() : name("Unnamed Person"), age(50), sex(true), country("United States") {};
    
        ...
        
    };


    #3.
    Code:
    class person
    {
        ...
    
        string sexString()
        {
        string returnString;
        returnString = (this->sex == true) ? "guy" : "woman" ;
        return returnString;
        }
    
    };
    Member functions that do not alter the class (change the value of any of its member variables) should be declared const. The function can also be simplified a bit:
    Code:
    class person
    {
        ...
    
        string sexString() const
        {
            return sex ? "guy" : "woman" ;
        }
    
    };


    #4.
    Code:
    void introduce(person person)
    {
        cout<<"\nHi, my name is "<<person.name<<". I'm a "<<person.age<<" years old "<<person.sexString()
            <<", and I live in "<<person.country<<"."<<endl;
        cin.get();
    
    }
    When passing objects into function, prefer to use a reference parameter. Also, since the function does not do anything to alter the member variables of the passed in object, it should be passed in as const:
    Code:
    void introduce(const person& person)
    {
        cout<<"\nHi, my name is "<<person.name<<". I'm a "<<person.age<<" years old "<<person.sexString()
            <<", and I live in "<<person.country<<"."<<endl;
        cin.get();
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Based Square Root Program...
    By matt.s in forum C Programming
    Replies: 11
    Last Post: 11-09-2009, 04:19 PM
  2. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 09:32 AM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. OOP program example
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2006, 05:53 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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