i build these code. it's very cool:
in test.h:

class test
{
private:
    bool blVisible;
    int x=0,y=0;
public:
    void Show();
    bool Visible()
    {
        return blVisible;
    }
    void Visible(bool value)
    {
        blVisible=value;
        if (value==true) Show();//call the event
    }
};

in main.cpp:

include <iostream>
#include "test.h"

using namespace std;

test a;

void test::Show()//the 'event' procedure
{
    cout << "showed";
}

int main()
{
    //read data

    bool b;
    cin >> b;
    a.Visible(b);
    cin >> b;
    a.Visible(b);
    return 0;
}

these code works fine. but imagine that you have 2 test varables, we get the same cout:(
so, how can i change these line:

void test::Show()//the 'event' procedure

for be used with diferent variables names?
(like these 'void a::Show()')

Recommended Answers

All 11 Replies

what do u mean by having two test variables?? two classes having the same name or two different classes having the same function name....

like these:

test a;
test b;

void a::Show()
{
   cout << "hello a";
}

void b::Show()
{
   cout << "hello b";
}

like you see, i use the var name and not the class name;)
is what i need to do.

I believe what you are meaning here is Polymorphism?

Here is an example:

class Base {

    public:

        Base() { }
        virtual std::string speak() { return 0; } 
};

class Dog : public Base {
    public:
    Dog() { }
    std::string speak() {
        return "woof";  
    }
};

class Cat : public Base {
    public:
    Cat() { }

    std::string speak() {
        return "meow";
    }
};
int main(int argc, char *argv[]) {

    Dog d;

    cout << d.speak(); 

    Cat c; 

    cout << c.speak();
}

Depending on which object I define, determines the output and therefore will change.

Also, it is not good standard to have cout << in any class members.

Hope this helps

sorry we can't do for a variable?
(like my code)
i need 2 variables with same class, but we can change the function cout. why??? i'm trying simulate the events with class

I suppose you could do the following:

class Test {

    public:

        Test() {

        }

        Test(std::string val)
        {
            value = val;
        }

        std::string show(){
            return  value;
        }
    protected:

        std::string value;

};
int main(int argc, char *argv[]) {

    std::string vala = "Hello a";
    std::string valb = "Hello b";

    Test a(vala);
    Test b(valb);

    cout << a.show() << endl;
    cout << b.show();
}

What you're asking here is:

test a;
test b;

void a::Show()
{
   cout << "hello a";
}

void b::Show()
{
   cout << "hello b";
}

It is basically saying that there is a class called a and a class called b and these are therefore not obects you would create in main. For example: What if you had an array of objects going from a, b, c.... then would you therefore need to invoke a member class for each object? No, this isn't object oriented programming. You could have classes that call different member functions depending on the object that is called, this, however, is called Polymorphasim.

commented: thanks +0
commented: Keep it up! +14

thanks for correct me. thanks.

i'm 100% new with these forum:
- i know mark topic resolved;
- is there any tool for give you 'points'\rate?
- or these forum works in diferent way?

Hey,

So you need to mark this as solved, if your question has been answered. (There should be an option 'mark this thread as solved) and then, at the side of each of the members username, there is an arrow up and down click the up arrow if someone has helped you, or the down arrow if you feel the post is bad or just generally didn't like it.

Hope this helps :)

commented: thanks +0

for now, i don't go mark resolved;)
(unless i can came back, if i need it????)

these is the best i came with:

class person
{
private:
    string name;
public:
    virtual void changed()
    {
        cout <<"";
    }
    person()
    {
        Name.setContainer(this);//these is from my property header file
    }
    void setname(string value)
    {
        name=value;
        changed();
    }

    string getname()
    {
        return name;
    }
    property <person,string,&person::setname,&person::getname> Name;//these is from my property header file
};

class joana : public person
{
    void changed()
    {
        cout << "joana name changed" << endl;
    }
}joana;

class ana : public person
{
    void changed()
    {
        cout << "ana name changed" << endl;
    }
}ana;

int main()
{

    joana.Name="ana";
    cout <<(string) joana.Name<< endl;
    ana.Name="joana";
    cout <<(string) ana.Name<< endl;

    //person2.Name="joaquim";
    //cout <<(string) person2.Name;
    return 0;
}

but i need ask anotherthing:
when i do:

class classname
{
     //class members
}classname;

why i can't do objects from classname?
(these is best solution that i found for events)
when i do:

person2 f;

i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\testevents\main.cpp|84|error: expected ';' before 'f'|"

if i build the class in these way:

class classname
{
     //class members
}classname, object1,object2,objectx;

now you can use the class with class name(like a 'static' class) and others objects. now who use these code, don't forget the global variables problems;)
these type of class can't be declared in other place... or you declare the objects before ';' or in other place of code(1 place or another). anotherthing: in other place you can't declare 1 object name with class name.
i hope these help anyone too;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.