CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    about class's and 'events'

    in hedear file(.h):
    Code:
    class test{
    private:
        bool blVisible;
    public:
        void Show();
        bool Visible()
        {
            return blVisible;
        }
        void Visible(bool value)
        {
            blVisible=value;
            if (value==true) Show();//call the event
        }
    };
    and in main.cpp:
    Code:
    #include <iostream>
    #include "test.h"
    #include <conio.h>
    #include <fstream>
    
    
    using namespace std;
    
    
    test a;
    
    
    void test::Show()
    {
        cout << "showed";
    }
    
    
    int main()
    {
        //read data
        bool b;
        cin >> b;
        a.Visible(b);
        cin.clear();
        cin >> b;
        a.Visible(b);
        getch();
        return 0;
    }
    just see these 2 lines:
    Code:
    test a;
    
    
    void test::Show()
    imagine that we do another: 'test c;'
    the 'void test::Show()' will be the same and do the same cout
    how can i work like these:
    'void varname::Show()'??

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: about class's and 'events'

    Maybe it's just me, but the question doesn't seem clear. Are you trying to do this?

    Test a;
    a.Show();

    Test b;
    b.Show();

    If not, can you clarify your question.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about class's and 'events'

    I agree with GCDEF that zour question is not clear.

    Besides:
    Quote Originally Posted by Cambalinho View Post
    Code:
    class test{
        ...
        void Visible(bool value)
        {
            blVisible=value;
            if (value==true) Show();//call the event
        }
    };
    It is not a good style:
    1. to place more than one statement in a line;
    2. to compare boolean values with true;

    Better style would be:
    Code:
    class test{
        ...
        void Visible(bool value)
        {
            blVisible = value;
            if (value) 
                Show();//call the event
        }
    };
    Victor Nijegorodov

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about class's and 'events'

    Quote Originally Posted by GCDEF View Post
    Maybe it's just me, but the question doesn't seem clear. Are you trying to do this?

    Test a;
    a.Show();

    Test b;
    b.Show();

    If not, can you clarify your question.
    see again the 'correct' main.cpp:
    Code:
    #include <iostream>
    #include "test.h"
    #include <conio.h>
    #include <fstream>
    
    
    using namespace std;
    
    
    test a;
    
    
    void a::Show()
    {
        cout << "showed";
    }
    
    
    int main()
    {
        //read data
        bool b;
        cin >> b;
        a.Visible(b);
        cin.clear();
        cin >> b;
        a.Visible(b);
        getch();
        return 0;
    }
    see these:
    Code:
    test a;
    test c;
    
    void a::Show()
    {
        cout << "showed";
    }
    
    void c::Show()
    {
       cout << "show anotherone";
    }
    now that sub only works for 'a' and anotherone for 'c'. can i do these?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about class's and 'events'

    Quote Originally Posted by Cambalinho View Post
    see these:
    Code:
    test a;
    test c;
    
    void a::Show()
    {
        cout << "showed";
    }
    
    void c::Show()
    {
       cout << "show anotherone";
    }
    What is a? Is it a class or an instance of class test?
    If former - then what does this line
    Code:
    test a;
    mean?
    If latter - then what does this "implementation"
    Code:
    void a::Show()
    {
        cout << "showed";
    }
    mean?

    The same question - for c
    Victor Nijegorodov

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about class's and 'events'

    Quote Originally Posted by VictorN View Post
    What is a? Is it a class or an instance of class test?
    If former - then what does this line
    Code:
    test a;
    mean?
    If latter - then what does this "implementation"
    Code:
    void a::Show()
    {
        cout << "showed";
    }
    mean?

    The same question - for c
    the 1st post
    ok. i will repeat:
    test.h:
    Code:
    #include <iostream>
    class test
    {
    private:
        bool blVisible;
    public:
        void Show();
        bool Visible()
        {
            return blVisible;
        }
        void Visible(bool value)
        {
            blVisible=value;
            if (value==true) Show();//call the event
        }
    };
    now in main.cpp:


    Code:
    #include "test.h"
    
    test a;
    test c;
    
    void a::Show()
    {
        cout << "showed";
    }
    
    void c::Show()
    {
       cout << "show anotherone";
    }
    
    int main()
    {
        //read data
        bool b;
        cin >> b;
        a.Visible(b);
        cin.clear();
        cin >> b;
        a.Visible(b);
        getch();
        return 0;
    }
    test is the class name
    if i use:
    Code:
    void test::Show()
    these sub will be the same for all variables. that's why i'm asking if i can do these:
    Code:
    void a::Show()
    so the cout cound be diferent
    can i do these?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about class's and 'events'

    Quote Originally Posted by Cambalinho View Post
    the 1st post
    ok. i will repeat:
    Oh, please do NOT do it! Don't repost your fantasies!

    Quote Originally Posted by Cambalinho View Post
    test is the class name
    if i use:
    Code:
    void test::Show()
    these sub will be the same for all variables.
    Correct!

    Quote Originally Posted by Cambalinho View Post
    ... that's why i'm asking if i can do these:
    Code:
    void a::Show()
    so the cout cound be diferent
    can i do these?
    No.
    But you could derive a bew class(es) from test and overload or override this method in there.
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about class's and 'events'

    Quote Originally Posted by VictorN View Post
    Oh, please do NOT do it! Don't repost your fantasies!

    Correct!

    No.
    But you could derive a bew class(es) from test and overload or override this method in there.
    how can i do it?
    (you can give me a link for learn, please?)

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about class's and 'events'

    If you do NOT know the basic of C++ then why do you ask it here in the Forum?
    First read books, tutorials, blogs for the beginners. Test examples...

    As for the link - Google is your friend.
    And note that CG-Forum in not a grade school!
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about class's and 'events'

    Quote Originally Posted by VictorN View Post
    If you do NOT know the basic of C++ then why do you ask it here in the Forum?
    First read books, tutorials, blogs for the beginners. Test examples...

    As for the link - Google is your friend.
    And note that CG-Forum in not a grade school!
    i know overload, what i don't is how i can do what you sugest

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about class's and 'events'

    Quote Originally Posted by Cambalinho View Post
    i know overload, what i don't is how i can do what you sugest
    Are you kidding?
    Victor Nijegorodov

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: about class's and 'events'

    Quote Originally Posted by Cambalinho View Post
    i know overload, what i don't is how i can do what you sugest
    No matter how many times you repeat it, your code makes no sense, therefore your question makes no sense. Look at what I posted earlier. If that's now what you're trying to do, explain using words, not code, exactly what you're trying to do.

  13. #13
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about class's and 'events'

    Quote Originally Posted by GCDEF View Post
    No matter how many times you repeat it, your code makes no sense, therefore your question makes no sense. Look at what I posted earlier. If that's now what you're trying to do, explain using words, not code, exactly what you're trying to do.
    sorry something
    what i'm trying to do is events with class's. thats why i use variables class for use that function.
    sorry my english sorry for something
    but i understand that i need learn much more about C++. i'm relearn it. sorry VictorN, but i'm not kidding with you. i understand the forum isn't a school, but the book don't bring all
    sorry something
    Last edited by Cambalinho; September 6th, 2013 at 06:03 AM.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about class's and 'events'

    how can i do it?
    This is an example

    Code:
    #include <iostream>
    using namespace std;
    
    class c1 {
    public:
    	virtual void show();
    	
    };
    
    class c2 : public c1 {
    public:
    	virtual void show();
    };
    
    void c1::show()
    {
    	cout << "show in c1" << endl;
    }
    
    void c2::show()
    {
    	cout << "show in c2" << endl;
    }
    
    void cshow(c1& c)
    {
    	c.show();
    }
    
    int main()
    {
    c1 i1;
    c2 i2;
    
    	cshow(i1);
    	cshow(i2);
    
    	return 0;
    }
    It produces the output

    Code:
    show in c1
    show in c2

    This is basic c++ class stuff. Study the book you're got!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about class's and 'events'

    Quote Originally Posted by 2kaud View Post
    This is an example

    Code:
    #include <iostream>
    using namespace std;
    
    class c1 {
    public:
    	virtual void show();
    	
    };
    
    class c2 : public c1 {
    public:
    	virtual void show();
    };
    
    void c1::show()
    {
    	cout << "show in c1" << endl;
    }
    
    void c2::show()
    {
    	cout << "show in c2" << endl;
    }
    
    void cshow(c1& c)
    {
    	c.show();
    }
    
    int main()
    {
    c1 i1;
    c2 i2;
    
    	cshow(i1);
    	cshow(i2);
    
    	return 0;
    }
    It produces the output

    Code:
    show in c1
    show in c2

    This is basic c++ class stuff. Study the book you're got!
    so i must use virtual functions. thanks for all to all
    realy thanks.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured