GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 15-May-2007, 00:03
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve
Thumbs up

Re: Inheritance Suspicious Problem


From my experience, normally i will ask user to input from console in C. I don't know what wrong with it because i am a newbie C++ programmer.

Do i violate the encapsulation concept ?

I want some flexibility. therefore, i don't want to instantiate the object during the class instantiate.

I want to initialize it during runtime and compile time which is much more flexible.

I don't know the correct methods implement in C++. I hope someone who kind enough to help me in this problem.

Thanks for your valuable information and opinion.

Your help and opinion is greatly appreciated by me and others noob.

A billion thanks to you.
  #12  
Old 15-May-2007, 14:35
davis
 
Posts: n/a

Re: Inheritance Suspicious Problem


It isn't that you don't want to stop asking the user for input in your applications, rather, that you don't want to make your class dependent on the I/O method. I'll try to explain in code:

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;


class Foo
{
public:
    Foo() : m_name( "Foo" ){}
    Foo( std::string const& name ) : m_name( name ) {}
    std::string getName() { return m_name; }
    void setName( std::string const& name ) { m_name = name; }
private:
    std::string m_name;
};

class Bar
{
public:
    Bar() : m_name(){}
    void getName()
    {
        cout << m_name << endl;
    }
    void setName()
    {
        cin >> m_name;
    }
private:
    std::string m_name;
};

int main()
{
    Foo f1;
    cout << f1.getName() << endl;

    Foo f2( "Master Foo" );
    cout << f2.getName() << endl;

    f2.setName( "Masterful Foo to you!" );
    cout << f2.getName() << endl;

    std::string str = f2.getName(); // can use str for other work
    str += "abcdefghijklmnopqrstuvwxyz";
    cout << str << endl;

    Bar b1;
    b1.getName(); // can't use this for anything but to print to stdout

    Bar b2;
    b2.setName();
    b2.getName();

    return 0;
}


:davis:
  #13  
Old 15-May-2007, 23:01
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve
Thumbs up

Re: Inheritance Suspicious Problem


I probably understand what you have mentioned here.

You mean make the class have many (constructor) method of instantiated the object to enhance flexibility and not any ask from user to initialize the object.

I have learn from mistake. Many books also wrote this methods but i thought my methods is better. Therefore, i continue using it.

If i have make some mistake, please correct me.

Thanks for your remind.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 13:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 11:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03
inheritance problem ap6118 C++ Forum 8 11-Apr-2005 12:24
Inheritance problem CaptnB C++ Forum 2 27-Jan-2005 09:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:20.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.